Hourly Forecasts

Learn how to get hourly weather forecast information for a specific location.

Hourly forecasts provide the latest weather updates during the day. We can use the endpoints discussed in this lesson to remain up-to-date with the changing weather over the course of the next few hours.

Hourly forecast endpoints

In this lesson, we'll learn how to retrieve the hourly weather forecast data for a particular location for the next hour and the next 12 hours.

1 hour of hourly forecasts

To get hourly forecasts for the next hour, we make a GET request to the following URL:

Press + to interact
http://dataservice.accuweather.com/forecasts/v1/hourly/1hour/{locationKey}

12 hours of hourly forecasts

Similarly, for hourly forecasts for the next 12 hours, we make a GET request to the following URL:

Press + to interact
http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/{locationKey}

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 location’s key. 

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.

metric

boolean

optional

This is set to true to display metric values 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 on line 8 as the locationKey for Seattle, United States.

Note: Please refer to the appendix for a list of some well-known cities along with their location keys.

You can try adding some optional parameter(s) to the queryParam1Hour object on lines 20–22 to fine-tune your results for 1-hour forecasts. You can do the same to the queryParam12Hour object on lines 24–26 for 12-hour forecasts.

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 url1Hour = new URL(`http://dataservice.accuweather.com/forecasts/v1/hourly/1hour/${locationKey.toString()}`);
const url12Hour = new URL(`http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/${locationKey.toString()}`);
// Define header parameters here
const headerParameters = {
contentType: 'application/json',
};
// Define query parameters here
const queryParams1Hour = new URLSearchParams({
apikey: API_KEY,
});
const queryParams12Hour = new URLSearchParams({
apikey: API_KEY,
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function getForecasts(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 getHourlyForecasts() {
await getForecasts(url1Hour, queryParams1Hour);
await getForecasts(url12Hour, queryParams12Hour);
}
// Calling function to make API call
getHourlyForecasts();

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

  • Line 8: We define the path parameter locationKey.

  • Line 11–12: We define the endpoint URLs for 1-hour and 12-hour forecasts.

  • Lines 20–26: We define the query parameters for 1-hour and 12-hour forecasts endpoints.

  • Lines 35–46: We define a custom function getForecasts() 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 48–51: We define a custom function getHourlyForecasts() to invoke the getForecasts() function for 1-hour and 12-hour forecasts.

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

The code above displays the hourly forecast data for the specified location for the next 1 hour and the next 12 hours. The output includes detailed weather information, including the response fields given in the table below. In case of failure, an appropriate error message is displayed.

Response fields

Parameter

Type

Description

DateTime

string

This is the date and time of the forecast.

HasPrecipitation

boolean

This indicates whether there is any precipitation or not.

IsDaylight

boolean

This indicates whether it is daytime or not.

Temperature

object

This provides temperature information.

RealFeelTemperature

object

This provides “feels like” temperature information.

Wind

object

This contains wind speed and wind direction values.

RelativeHumidity

integer

This provides the humidity value.

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.