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:
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:
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 |
| 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. 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
, 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.
// 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 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 hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst queryParams1All = new URLSearchParams({apikey: API_KEY,});const queryParams5All = new URLSearchParams({apikey: API_KEY,});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function getIndexValues(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 getDailyIndexValues() {await getIndexValues(url1All, queryParams1All);await getIndexValues(url5All, queryParams5All);}// Calling function to make API callgetDailyIndexValues();
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 usingfetch
and handle any exception if it occurs. The custom functionsprintResponse()
andprintError()
are used to print the API response and errors, respectively.Lines 48–51: We define a custom function
getDailyIndexValues()
to invoke thegetIndexValues()
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 |
| string | This is the name of the index. |
| integer | This is the ID of the index. This value may be null. |
| boolean | This is the order of the index values. If this is set to |
| string | This is the date and time of the index. |
| integer | This is the number of seconds that have elapsed since January 1, 1970. |
| decimal | This is the value of the index. The range is This value may be null. |
| string | This is the level of the index value. |
| integer | This is the integer that indicates the level of the index value. Its range is It should be used in conjunction with the |
| string | This is the description of the index value and category. |
| string | This is the URL of AccuWeather's mobile site to indices for the specified location. |
| string | This is the URL of AccuWeather's website to indices for the specified location. |