Current Weather Conditions

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:

Press + to interact
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:

Press + to interact
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

API_KEY

string

required

This is our API key.

This is a query parameter.

locationKey

integer

required

This is the location’s key.

This is a path parameter only for the specific location endpoint.

group

integer

required

This is the number of cities.

The allowable values are 50, 100, and 150.

This is a path parameter only for the top cities endpoint.

language

string

optional

This is the language in which we have to return the results. 

Its default value is en-us.

This is a query parameter.

details

boolean

optional

This is set to true to include full details in the response. Otherwise, it is set to false.

Its default value is false.

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.

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define API key here
const API_KEY = '{{API_KEY}}';
// Define path parameters here
const locationKey = 351409;
const group = 50;
// Define endpoint URL here
const 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 here
const headerParameters = {
contentType: 'application/json',
};
// Define query parameters here
const queryParamsSpecificLoc = new URLSearchParams({
apikey: API_KEY,
});
const queryParamsTopCities = new URLSearchParams({
apikey: API_KEY,
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function getConditions(endpointUrl, queryParameters) {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
async function getCurrentConditions() {
await getConditions(urlSpecificLoc, queryParamsSpecificLoc);
await getConditions(urlTopCities, queryParamsTopCities);
}
// Calling function to make API call
getCurrentConditions();

Let's take a quick look at the code given in the widget above:

  • Lines 8–9: We define the path parameters locationKey and group.

  • 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 using fetch and handle any exception if it occurs. The custom functions printResponse() and printError() are used to print the API response and errors, respectively.

  • Line 49–52: We define a custom function getCurrentConditions() to invoke the getConditions() 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

HasPrecipitation

boolean

This indicates whether there is any precipitation or not.

IsDayTime

boolean

This indicates whether it's daytime or not.

Temperature

object

This is the temperature in Fahrenheit.

RealFeelTemperature

object

This is the “feels like” temperature in Fahrenheit.

Wind

object

This contains wind speed and wind direction values.

RelativeHumidity

integer

This provides the humidity value.

UVIndex

integer

This is the measure of the ultraviolet radiation from the sun.

This value may be null.

Visibility

object

This contains visibility information.

Link

string

This is the URL of AccuWeather's website for a detailed forecast.

MobileLink

string

This is the URL of AccuWeather's mobile site for a detailed forecast.

For the top cities endpoint

Parameter

Type

Description

Key

string

This is the unique location key of the city.

LocalizedName

string

This is the name of the location in the requested language.

Its default language is en-us.

Country

object

This contains information regarding the city's country, including its code and localized and English names.

Timezone

object

This contains information regarding the city's designated timezone.

Geoposition

object

This contains information regarding the city's geographical position, including its coordinates.

IsDayTime

boolean

This indicates whether it’s daytime or not.

Temperature

object

This provides temperature values in Fahrenheit and degrees Celsius.

HasPrecipitation

boolean

This indicates whether there is any precipitation or not.

Link

string

This is the URL of AccuWeather's website for a detailed forecast.

MobileLink

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.