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:
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:
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 |
| 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. |
| 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. |
| boolean | optional | This is set to Its default value is 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.
// 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 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 hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst queryParams1Hour = new URLSearchParams({apikey: API_KEY,});const queryParams12Hour = new URLSearchParams({apikey: API_KEY,});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function getForecasts(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 getHourlyForecasts() {await getForecasts(url1Hour, queryParams1Hour);await getForecasts(url12Hour, queryParams12Hour);}// Calling function to make API callgetHourlyForecasts();
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 usingfetch
and handle any exception if it occurs. The custom functionsprintResponse()
andprintError()
are used to print the API response and errors, respectively.Line 48–51: We define a custom function
getHourlyForecasts()
to invoke thegetForecasts()
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 |
| string | This is the date and time of the forecast. |
| boolean | This indicates whether there is any precipitation or not. |
| boolean | This indicates whether it is daytime or not. |
| object | This provides temperature information. |
| object | This provides “feels like” temperature information. |
| object | This contains wind speed and wind direction values. |
| integer | This provides the humidity value. |
| 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.