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:
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:
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 |
| string | required | This is our API key. This is a query parameter. |
| integer | required | This is the key of the location. This is a path parameter. |
| 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. |
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.
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst API_KEY = '{{API_KEY}}';// Define path parameter hereconst locationKey = 351409;// Define endpoint URL hereconst 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 hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst queryParams6Hour = new URLSearchParams({apikey: API_KEY,});const queryParams24Hour = 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 getHistoricalConditions() {await getConditions(url6Hour, queryParams6Hour);await getConditions(url24Hour, queryParams24Hour);}// Calling function to make API callgetHistoricalConditions();
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 usingfetch
and handle any exception if it occurs. The custom functionsprintResponse()
andprintError()
are used to print the API response and errors, respectively.Lines 48–51: We define a custom function
getHistoricalConditions()
to invoke thegetConditions()
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 |
| boolean | This indicates whether there is any precipitation or not. |
| boolean | This indicates whether it's daytime or not. |
| object | This provides the temperature information. |
| object | This provides the “feels like” temperature information. |
| 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. |
Note: The list of response fields above is not exhaustive. Please refer to the appendix for details of the response.