Historical Weather Conditions

Learn how to get historical current conditions data for a specific location.

Historical weather conditions data provides valuable insights into a particular location's weather patterns. It also helps us predict what the weather may be like given a certain weather pattern that has existed over the last few hours.

Historical current conditions endpoints

In this lesson, we'll learn how to retrieve the historical current conditions data for a specific location for the past 6 hours and the past 24 hours.

Past 6 hour conditions

We make a GET request to the following URL to get the historical weather conditions data for the past six hours for a location of our choice:

Press + to interact
http://dataservice.accuweather.com/currentconditions/v1/{locationKey}/historical

Past 24 hour conditions

Similarly, we make a GET request to the following URL to get the historical weather conditions data for the past 24 hours for a location of our choice:

Press + to interact
http://dataservice.accuweather.com/currentconditions/v1/{locationKey}/historical/24

Request parameters

The table below shows the list of input parameters for both endpoints.

Parameter

Type

Category

Description

API_KEY

string

required

This is our API key.

This is a query parameter.

locationKey

integer

required

This is the key of the location.

This is a path parameter.

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.

In the code below, we enter 351409, which is the location key for Seattle, United States, for the parameter locationKey on line 8.

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 queryParams6Hour and queryParams24Hour objects on lines 20–22 and 24–26 to fine-tune your results for the past 6 hours and the past 24 hours, 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 parameter here
const locationKey = 351409;
// Define endpoint URL here
const url6Hour = new URL(`http://dataservice.accuweather.com/currentconditions/v1/${locationKey.toString()}/historical`);
const url24Hour = new URL(`http://dataservice.accuweather.com/currentconditions/v1/${locationKey.toString()}/historical/24`);
// Define header parameters here
const headerParameters = {
contentType: 'application/json',
};
// Define query parameters here
const queryParams6Hour = new URLSearchParams({
apikey: API_KEY,
});
const queryParams24Hour = 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 getHistoricalConditions() {
await getConditions(url6Hour, queryParams6Hour);
await getConditions(url24Hour, queryParams24Hour);
}
// Calling function to make API call
getHistoricalConditions();

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

  • Line 8: We define the path parameter locationKey.

  • Lines 11–12: We define the endpoint URLs for 6-hour and 24-hour conditions.

  • Lines 20–26: We define the query parameters for 6-hour and 24-hour conditions endpoints.

  • Lines 35–46: 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.

  • Lines 48–51: We define a custom function getHistoricalConditions() to invoke the getConditions() function for 6-hour and 24-hour conditions.

  • Line 54: We invoke the getHistoricalConditions() function.

The above code displays the historical weather conditions data for the specified location for the past 6 hours and for the past 24 hours, including some of the important response fields given in the table below. In case of failure, an appropriate error message is displayed.

Response fields

Parameter

Type

Description

HasPrecipitation

boolean

This indicates whether there is any precipitation or not.

IsDaylight

boolean

This indicates whether it's daytime or not.

Temperature

object

This provides the temperature information.

RealFeelTemperature

object

This provides the “feels like” temperature information.

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.

Note: The list of response fields above is not exhaustive. Please refer to the appendix for details of the response.