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:

Press + to interact
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:

Press + to interact
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

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.

The 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.

The 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.

The 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 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.

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 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 here
const headerParameters = {
contentType: 'application/json',
};
// Define query parameters here
const queryParams1Day = new URLSearchParams({
apikey: API_KEY,
});
const queryParams5Day = 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 getDailyForecasts() {
await getForecasts(url1Day, queryParams1Day);
await getForecasts(url5Day, queryParams5Day);
}
// Calling function to make API call
getDailyForecasts();

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 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 getDailyForecasts() to invoke the getForecasts() 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

Headline

object

This is the most important weather description over the specified duration.

DailyForecasts.Day

object

This refers to the weather forecast information during the day.

DailyForecasts.Night

object

This refers to the weather forecast information during the night.

DailyForecasts.Temperature.Maximum

object

This tells us the maximum temperature.

DailyForecasts.Temperature.Minimum

object

This tells us the minimum temperature.

DailyForecasts.Link

string

This is the URL of AccuWeather's website for a detailed forecast.

DailyForecasts.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.