Current Weather Data

Learn to retrieve current weather data for a specific location.

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

APP_ID

string

required

This is our unique API key.

lat

decimal

required

This is the location’s latitude.

lon

decimal

required

This is the location’s longitude.

mode

string

optional

This refers to the format of the API response.

Its default value is json.

Other possible values are xml and html.

units

string

optional

This refers to the units of measurement.

Its default value is standard. Other possible values are metric and imperial.

lang

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.

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define APP ID here
const APP_ID = '{{APP_ID}}';
// Define endpoint URL here
const url = new URL(`https://api.openweathermap.org/data/2.5/weather`);
// Define header parameters here
const headerParameters = {
contentType: 'application/json',
};
// Define query parameters here
const lat = 47.6038321;
const lon = -122.3300624;
const queryParameters = new URLSearchParams({
lat: lat.toString(),
lon: lon.toString(),
appid: APP_ID
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function getCurrentWeather() {
try {
url.search = queryParameters;
const response = await fetch(url, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
getCurrentWeather();

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. The printResponse() and printError() 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

coord.lat

decimal

This is the location’s latitude.

coord.lon

decimal

This is the location’s longitude.

weather.id

integer

This is the weather condition ID.

weather.main

string

This specifies the weather parameters' group. Its sample values are Clear, Rain, Snow, and Extreme.

weather.description

string

This is the description of weather conditions.

weather.icon

string

This is the weather icon ID.

main.temp

decimal

This specifies the actual temperature.

For default, the value is in Kelvin. For metric, the value is in Celsius. For imperial, the value is in Fahrenheit.

main.feels_like

decimal

This specifies the temperature as per human perception.

For default, the value is in Kelvin. For metric, the value is in Celsius. For imperial, the value is in Fahrenheit.

main.pressure

integer

This specifies the default atmospheric pressure at sea level. Its unit is hPa.

main.humidity

integer

This is the percentage of humidity. Its unit is percentage (%).

visibility

integer

This specifies the average visibility. Its unit is meters.

wind.speed

decimal

This is the wind speed. For default and metric, the value is in meter/second. For imperial, the value is in miles/hour.

clouds.all

integer

This is the percentage of cloudiness. Its unit is percentage (%).

rain.1h

integer

This is the volume of rain over the last hour. Its unit is millimeters. This value may be NULL.

snow.1h

integer

This is the volume of snow over the last hour. Its unit is millimeters. This value may be NULL.

dt

integer

This is the time at which the data was calculated. Its units are Unix and UTC.

sys.sunrise

integer

This is the time of sunrise. Its units are Unix and UTC.

sys.sunset

integer

This is the time of sunset. Its units are Unix and UTC.

id

integer

This is the city's ID.

name

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.