Getting a List of Locations
Learn how to get a list of top cities worldwide and countries within a given region.
The Locations API allows us to get detailed information about places around the globe. Among other information, it provides the unique key for each location as well. This key is important since we'll use it in the coming lessons to invoke other APIs to get various kinds of weather data.
In this lesson, we'll learn how to get a specified number of top cities around the globe with detailed information. Moreover, we'll also learn how to get a list of all countries from a given region, along with their basic information.
List the top cities in the world
The Top Cities List endpoint allows us to retrieve a list of the top cities in the world. The ranking of the cities is based on a number of factors including, but not limited to, the population of the city, its political importance, and its geographic size. This endpoint provides detailed information for each city, including its geographical position, timezone, and population. Using this endpoint, we can retrieve information for the top 50, 100, or 150 cities worldwide.
Let's make a GET request to the following URL to get a list of a certain number of top cities around the world:
http://dataservice.accuweather.com/locations/v1/topcities/{group}
Request parameters
Parameter | Type | Category | Description |
| string | required | This is our API key. This is a query parameter. |
| integer | required | This is the number of cities we have to retrieve. The allowable values are 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 set the parameter group
to 50
on line 8. You can try adding the optional parameters, language
and details
, to the queryParameters
object on lines 19–21 for fine-tuning the results.
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 group = 50;// Define endpoint URL hereconst url = new URL(`http://dataservice.accuweather.com/locations/v1/topcities/${group}`);// Define header parameters hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst queryParameters = new URLSearchParams({apikey: API_KEY,});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function getTopCities() {try {url.search = queryParameters;const response = await fetch(url, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callgetTopCities();
Let's take a quick look at the code given in the widget above:
Line 8: We define the path parameter
group
.Line 11: We define the endpoint URL.
Lines 19–21: We define the query parameters.
Lines 30–41: We define a custom function
getTopCities()
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 44: We invoke the
getTopCities()
function.
The code above displays the specified number of top cities around the globe and their detailed 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 unique location key of the city. |
| integer | This is the rank designated on the basis of population, political importance, and geographic size. |
| string | This is the name of the location in the requested language. Its default language is |
| object | This contains information regarding the city's country, including its code, and its localized and English names. |
| object | This contains information regarding the city's designated timezone. |
| object | This contains information regarding the city's geographical position, including its coordinates. |
Note: The list of response fields above is not exhaustive. Please refer to the appendix for further details of the response.
List countries from a specific region
The Country List endpoint allows us to retrieve a list of all countries from a specific region. It also provides some basic information about each country, including its
We make a GET request to the following URL to get a list of all countries from a region of our choice:
http://dataservice.accuweather.com/locations/v1/countries/{regionCode}
Request parameters
Parameter | Type | Category | Description |
| string | required | This is our API key. This is a query parameter. |
| string | required | This specifies the region to get all the countries from. Allowable values: 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. |
In the code below, we enter NAM
, which is the region code for North America, for the parameter regionCode
on line 8. You can try adding the optional parameter language
to the queryParameters
object on lines 19–21 to get the results in a language of your choice.
Click the "Run" button to see the output.
Note: Please refer to the appendix for all the regions and their corresponding codes.
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst API_KEY = '{{API_KEY}}';// Define path parameter hereconst regionCode = 'NAM';// Define endpoint URL hereconst url = new URL(`https://dataservice.accuweather.com/locations/v1/countries/${regionCode}`);// Define header parameters hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst queryParameters = new URLSearchParams({apikey: API_KEY,});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function getCountries() {try {url.search = queryParameters;const response = await fetch(url, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callgetCountries();
Let's take a quick look at the code given in the widget above:
Line 8: We define the path parameter
regionCode
.Line 11: We define the endpoint URL.
Lines 19–21: We define the query parameters.
Lines 30–41: We define a custom function
getCountries()
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 44: We invoke the
getCountries()
function.
The code above displays a list of all countries from the specified region. For each country, it provides the information given in the table below. In case of failure, an appropriate error message is displayed.
Response fields
Parameter | Type | Description |
| string | This is the unique ISO country code. |
| string | This is the country name in the local language. Its default language is |
| string | This is the country name in the English language. |