Current Weather Data
Learn to retrieve current weather data for a specific location.
We'll cover the following
This API provides current weather data for any given location on the globe. These locations include more than 200,000 cities worldwide. This API collects and processes weather data from a number of sources. These sources include local and global weather models, satellites, radars, and a wide network of weather stations. We can retrieve data from this API in the JSON, XML, and HTML formats.
Get current weather data
We’ll make a GET
request to the following URI to get the current weather data for a location of our choosing:
https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={APP_ID}
Request parameters
Parameter | Type | Category | Description |
| string | required | This is our unique API key. |
| decimal | required | This is the location’s latitude. |
| decimal | required | This is the location’s longitude. |
| string | optional | This refers to the format of the API response. Its default value is Other possible values are |
| string | optional | This refers to the units of measurement. Its default value is |
| string | optional | This allows us to retrieve the output in our desired language. |
Lines 16–17: We enter 47.6038321
and -122.3300624
, the geographical coordinates for Seattle, United States, for the required parameters lat
and lon
, respectively.
Lines 19–23: We may enter other optional parameter(s) listed in the table above to the queryParameters
object to fine-tune our results.
Note: Head over to the Geocoding API lesson to learn how to get the geographical coordinates (latitude and longitude) of a location by specifying its name.
Click the “Run” button in the widget below to see the output.
// Importing libraries hereimport fetch from "node-fetch";// Define APP ID hereconst APP_ID = '{{APP_ID}}';// Define endpoint URL hereconst url = new URL(`https://api.openweathermap.org/data/2.5/weather`);// Define header parameters hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst lat = 47.6038321;const lon = -122.3300624;const queryParameters = new URLSearchParams({lat: lat.toString(),lon: lon.toString(),appid: APP_ID});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function getCurrentWeather() {try {url.search = queryParameters;const response = await fetch(url, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callgetCurrentWeather();
Let’s take a quick look at the code given in the widget above:
- Line 8: We define the URL for the current weather data endpoint.
- Lines 32–43: We define a custom function
getCurrentWeather()
to make an API call using fetch and handle any exception if it occurs. TheprintResponse()
andprintError()
are custom functions to print the API response and errors, respectively. - Line 46: We invoke the
getCurrentWeather()
function.
The output of the code above displays the current weather data for the location corresponding to the specified geographical coordinates.
Response fields
Let’s dig into the response fields generated by this endpoint.
Parameter | Type | Description |
| decimal | This is the location’s latitude. |
| decimal | This is the location’s longitude. |
| integer | This is the weather condition ID. |
| string | This specifies the weather parameters' group. Its sample values are |
| string | This is the description of weather conditions. |
| string | This is the weather icon ID. |
| decimal | This specifies the actual temperature. For |
| decimal | This specifies the temperature as per human perception. For |
| integer | This specifies the default atmospheric pressure at sea level. Its unit is hPa. |
| integer | This is the percentage of humidity. Its unit is percentage (%). |
| integer | This specifies the average visibility. Its unit is meters. |
| decimal | This is the wind speed. For |
| integer | This is the percentage of cloudiness. Its unit is percentage (%). |
| integer | This is the volume of rain over the last hour. Its unit is millimeters. This value may be |
| integer | This is the volume of snow over the last hour. Its unit is millimeters. This value may be |
| integer | This is the time at which the data was calculated. Its units are Unix and UTC. |
| integer | This is the time of sunrise. Its units are Unix and UTC. |
| integer | This is the time of sunset. Its units are Unix and UTC. |
| integer | This is the city's ID. |
| string | This is the city's name. |
Note: The above list of response fields is not exhaustive. Head over to the Additional Response Fields lesson in the Appendix for the remaining response fields for this endpoint.