Index Values

Learn how to get index data for a single day for a specific location.

The Indices API allows us to retrieve index values for a particular location. It provides detailed information, including the name of the index, its time, its value, and its category.

Endpoints for getting index values

In this lesson, we'll learn how to retrieve the index data of a particular location for a single day and for a period of 5 days. These endpoints provide the index data for all indices for the specified location.

Note: The availability of index values varies by location.

Index values for a single day

To get the index values for a single day for a location of our choice, we make a GET request to the following URL:

Press + to interact
http://dataservice.accuweather.com/indices/v1/daily/1day/{locationKey}

Index values for 5 days

Similarly, we make a GET request to the following URL to get the index data for a period of 5 days for a particular location:

Press + to interact
http://dataservice.accuweather.com/indices/v1/daily/5day/{locationKey}

Request parameters

The table below shows the list of input parameters for the aforementioned 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.

Its default value is en-us.

This is a query parameter.

details

boolean

optional

This is set to true to include the complete indices object in the response and is set to false to include a curtailed one.

Its default value is false.

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 queryParams1All and queryParams5All objects on lines 20–22 and 24–26 to fine-tune your results for the index values for a single day and five days, respectively.

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 url1All = new URL(`http://dataservice.accuweather.com/indices/v1/daily/1day/${locationKey.toString()}`);
const url5All = new URL(`http://dataservice.accuweather.com/indices/v1/daily/5day/${locationKey.toString()}`);
// Define header parameters here
const headerParameters = {
contentType: 'application/json',
};
// Define query parameters here
const queryParams1All = new URLSearchParams({
apikey: API_KEY,
});
const queryParams5All = new URLSearchParams({
apikey: API_KEY,
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function getIndexValues(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 getDailyIndexValues() {
await getIndexValues(url1All, queryParams1All);
await getIndexValues(url5All, queryParams5All);
}
// Calling function to make API call
getDailyIndexValues();

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 1-day and 5-day index values.

  • Lines 20–26: We define the query parameters for 1-day and 5-day index values endpoints.

  • Lines 35–46: We define a custom function getIndexValues() 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.

  • Lines 48–51: We define a custom function getDailyIndexValues() to invoke the getIndexValues() function for 1-day and 5-day index values.

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

The code above displays the index data for all indices by the specified location for a single day and for a period of 5 days. The detailed response fields are given in the table below. In case of failure, an appropriate error message is displayed.

Response fields

Parameter

Type

Description

Name

string

This is the name of the index.

ID

integer

This is the ID of the index.

This value may be null.

Ascending

boolean

This is the order of the index values.

If this is set to true, the best value is 10 and the poorest is 0. If false, then the best value is 0 and the poorest is 10.

LocalDateTime

string

This is the date and time of the index.

EpochDateTime

integer

This is the number of seconds that have elapsed since January 1, 1970.

Value

decimal

This is the value of the index. The range is 0.0 to 10.0.

This value may be null.

Category

string

This is the level of the index value.

CategoryValue

integer

This is the integer that indicates the level of the index value. Its range is 1 to 5.

It should be used in conjunction with the Ascending value.

Text

string

This is the description of the index value and category.

MobileLink

string

This is the URL of AccuWeather's mobile site to indices for the specified location.

Link

string

This is the URL of AccuWeather's website to indices for the specified location.