Daily Forecasts
Learn how to get daily weather forecast information for a specific location.
The Forecast API allows us to get weather forecast information for a particular location. This API provides detailed weather information, including temperature readings, air and pollen readings, wind speed, and rain probability during the day and night.
Daily forecast endpoints
In this lesson, we'll learn how to retrieve the daily weather forecast data for a particular location for a single day and for a period of five days.
1 day of daily forecasts
To retrieve the daily weather forecast data for a single day for a given location, we make a GET request to the following URL:
http://dataservice.accuweather.com/forecasts/v1/daily/1day/{locationKey}
5 days of daily forecasts
For daily weather forecast data for the next five days of a particular location, we make a GET request to the following URL:
http://dataservice.accuweather.com/forecasts/v1/daily/5day/{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 key of the location. This is a path parameter. |
| string | optional | This is the language in which we have to return the results. The default value is This is a query parameter. |
| boolean | optional | This is set to The default value is This is a query parameter. |
| boolean | optional | This is set to The 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 queryParams1Day
object on lines 20–22 to fine-tune your results for 1-day forecasts. You can do the same to the queryParams5Day
object on lines 24–26 for 5-day 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 url1Day = new URL(`http://dataservice.accuweather.com/forecasts/v1/daily/1day/${locationKey.toString()}`);const url5Day = new URL(`http://dataservice.accuweather.com/forecasts/v1/daily/5day/${locationKey.toString()}`);// Define header parameters hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst queryParams1Day = new URLSearchParams({apikey: API_KEY,});const queryParams5Day = 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 getDailyForecasts() {await getForecasts(url1Day, queryParams1Day);await getForecasts(url5Day, queryParams5Day);}// Calling function to make API callgetDailyForecasts();
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-day and 5-day forecasts.
Lines 20–26: We define the query parameters for 1-day and 5-day 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
getDailyForecasts()
to invoke thegetForecasts()
function for 1-day and 5-day forecasts.Line 54: We invoke the
getDailyForecasts()
function.
The code above displays the forecast data for the specified location for a single day and five days. The output includes the response fields specified in the table below, with additional information. In case of failure, an appropriate error message is displayed.
Response fields
Parameter | Type | Description |
| object | This is the most important weather description over the specified duration. |
| object | This refers to the weather forecast information during the day. |
| object | This refers to the weather forecast information during the night. |
| object | This tells us the maximum temperature. |
| object | This tells us the minimum temperature. |
| 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.