Current Weather Conditions
Learn how to get current weather conditions data.
The Current Conditions API allows us to get the current weather conditions data for a specific location. It provides detailed information, including the current temperature, wind speed, precipitation summary, information on cloud cover, and atmospheric pressure.
Endpoints for current conditions
In this lesson, we'll learn how to retrieve the current weather conditions data for a specific location and for the top cities around the globe.
Current conditions for a specific location
We make a GET request to the following URL to get the present conditions data for a location of our choice:
http://dataservice.accuweather.com/currentconditions/v1/{locationKey}
Current conditions for top cities
We make a GET request to the following URL to get the present conditions data for the specified number of top cities around the world:
http://dataservice.accuweather.com/currentconditions/v1/topcities/{group}
Note: The ranking of the cities is based on a number of factors, including, but not limited to, the population of the city, its political importance, and its geographic size. We can retrieve information for the top 50, 100, or 150 cities worldwide using this endpoint.
Request parameters
Parameter | Type | Category | Description |
| string | required | This is our API key. This is a query parameter. |
| integer | required | This is the location’s key. This is a path parameter only for the specific location endpoint. |
| integer | required | This is the number of cities. The allowable values are This is a path parameter only for the top cities endpoint. |
| string | optional | This is the language in which we have to return the results. Its default value is This is a query parameter. |
| boolean | optional | This is set to Its default value is This is a query parameter only for the specific location endpoint. |
In the code below, we enter 351409
, which is the location key for Seattle, United States, for the parameter locationKey
on line 8 and 50
for the parameter group
on line 9.
Note: Please refer to the appendix for a list of some well-known cities along with their location keys.
You can try adding the optional parameters, language
and details
, to the queryParamsSpecificLoc
and queryParamsTopCities
objects on lines 21–23 and 25–27, where applicable, for fine-tuning the results relating to a given city and for top cities, respectively.
Click the "Run" button to see the output.
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst API_KEY = '{{API_KEY}}';// Define path parameters hereconst locationKey = 351409;const group = 50;// Define endpoint URL hereconst urlSpecificLoc = new URL(`http://dataservice.accuweather.com/currentconditions/v1/${locationKey.toString()}`);const urlTopCities = new URL(`http://dataservice.accuweather.com/currentconditions/v1/topcities/${group.toString()}`);// Define header parameters hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst queryParamsSpecificLoc = new URLSearchParams({apikey: API_KEY,});const queryParamsTopCities = new URLSearchParams({apikey: API_KEY,});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function getConditions(endpointUrl, queryParameters) {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}async function getCurrentConditions() {await getConditions(urlSpecificLoc, queryParamsSpecificLoc);await getConditions(urlTopCities, queryParamsTopCities);}// Calling function to make API callgetCurrentConditions();
Let's take a quick look at the code given in the widget above:
Lines 8–9: We define the path parameters
locationKey
andgroup
.Line 12–13: We define the endpoint URLs for conditions of a specific location and top cities.
Lines 21–27: We define the query parameters for the specific location and top cities endpoints.
Lines 36–47: We define a custom function
getConditions()
to make an API call usingfetch
and handle any exception if it occurs. The custom functionsprintResponse()
andprintError()
are used to print the API response and errors, respectively.Line 49–52: We define a custom function
getCurrentConditions()
to invoke thegetConditions()
function for conditions of a specific location and top cities.Line 55: We invoke the
getCurrentConditions()
function.
The above code displays the current conditions data for the specified location and for the specified number of top cities around the globe. Some of the important response fields are given in the tables below. In case of failure, an appropriate error message is displayed.
Response fields
For a specific location endpoint
Parameter | Type | Description |
| boolean | This indicates whether there is any precipitation or not. |
| boolean | This indicates whether it's daytime or not. |
| object | This is the temperature in Fahrenheit. |
| object | This is the “feels like” temperature in Fahrenheit. |
| object | This contains wind speed and wind direction values. |
| integer | This provides the humidity value. |
| integer | This is the measure of the ultraviolet radiation from the sun. This value may be null. |
| object | This contains visibility information. |
| string | This is the URL of AccuWeather's website for a detailed forecast. |
| string | This is the URL of AccuWeather's mobile site for a detailed forecast. |
For the top cities endpoint
Parameter | Type | Description |
| string | This is the unique location key of the city. |
| string | This is the name of the location in the requested language. Its default language is |
| object | This contains information regarding the city's country, including its code and localized and English names. |
| object | This contains information regarding the city's designated timezone. |
| object | This contains information regarding the city's geographical position, including its coordinates. |
| boolean | This indicates whether it’s daytime or not. |
| object | This provides temperature values in Fahrenheit and degrees Celsius. |
| boolean | This indicates whether there is any precipitation or not. |
| string | This is the URL of AccuWeather's website for a detailed forecast. |
| string | This is the URL of AccuWeather's mobile site for a detailed forecast. |
Note: The lists of response fields above are not exhaustive. Please refer to the appendix for details of the two responses.