OpenWeatherMap’s new API Key

A number of our tutorials use weather data from OpenWeatherMap’s website. It provides information about current conditions for anywhere in the world, with an easy to use API. Best of all, it’s free, so long as you do not make more than 60 calls a minute or 50,000 per day.

Recently, they added a requirement for an API Key to access their service. An API Key is a string you supply with your request containing your unique identifier. They’re free: you can get them at http://openweathermap.org/appid.

To use the API Key, you need to make a small change to the tutorials. Here’s how getting information from OpenWeatherMap looks now:

Dim city = encodeURIComponent(txtCity.value)
GetJSON("http://api.openweathermap.org/data/2.5/weather","q=" & city, weatherData)

To add the API Key to your code, modify it as follows:

Dim city = encodeURIComponent(txtCity.value)
Dim appid = "2de143494c0b295cca9337e1e96b00e0"
GetJSON("http://api.openweathermap.org/data/2.5/weather","q=" & city & "&appid=" & appid, weatherData)

If you have your own API Key, substitute that as the value for appid.

We are updating the samples in the AppStudio distribution to do this: builds after AppStudio 5.1.3 have this change already applied to all tutorials.

More Code Snippets

BASIC, also getting weather for the user’s current location:

Dim appid = "2de143494c0b295cca9337e1e96b00e0"
If flpCurrentLocation.value() = "Off" Then
  Dim city = encodeURIComponent(txtCity.value)
  GetJSON("http://api.openweathermap.org/data/2.5/weather","q=" & city & "&appid=" & appid, weatherData) 
Else   
  GetJSON("http://api.openweathermap.org/data/2.5/weather","lat=" & lat & "&lon=" & lon & "&appid=" & appid, weatherData)
End If

JavaScript, simple version:

  var city = encodeURIComponent(txtCity.value);
  var appid = "2de143494c0b295cca9337e1e96b00e0";
  GetJSON("http://api.openweathermap.org/data/2.5/weather", "q=" + city + "&appid=" + appid, 
};

JavaScript, also getting weather for the user’s current location:

var appid = "2de143494c0b295cca9337e1e96b00e0";
if (flpCurrentLocation.value() == "Off") {
   var city = encodeURIComponent(txtCity.value);
   GetJSON("http://api.openweathermap.org/data/2.5/weather", "q=" + city + "&appid=" + appid, weatherData);
 } else {
   GetJSON("http://api.openweathermap.org/data/2.5/weather", "lat=" + lat + "&lon=" + lon + "&appid=" + appid, weatherData);
 }