Get a List of Locations
Learn how to get a list of top cities worldwide and countries within a given region.
Overview
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. 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 learn how to get a specified number of top cities around the globe with their detailed information. Moreover, we also learn how to get a list of all countries in 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. We can retrieve information for the top 50, 100, or 150 cities worldwide using this endpoint.
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}
Input 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 50, 100, and 150. This is a path parameter. |
| 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. |
| boolean | optional | This is set to "True" to include full details in the response, otherwise it is set to "False." Its default value is "False." This is a query parameter. |
Sample code
In the code below, we set the parameter group
to 50
on line 1. You can try adding the optional parameters, language
and details
, to the URL on lines 3–4 for fine-tuning the results.
Click the "Run" button to see the output.
group = 50url = 'http://dataservice.accuweather.com/locations/v1/topcities/' \+ str(group) + '?apikey={{API_KEY}}}'response = requests.get(url)printResponse(response)
The code above displays the specified number of top cities around the globe and their detailed information, including the response parameters given in the table below. In case of failure, an appropriate error message is displayed.
Response parameters
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 "en-us." |
| 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 parameters above is not exhaustive. Please refer to the appendix for details of the response.
List countries in a specific region
The Country List endpoint allows us to retrieve a list of all countries in 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 in a region of our choice:
http://dataservice.accuweather.com/locations/v1/countries/{regionCode}
Input 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 countries from. Allowable values: "ASI," "MEA," "EUR," "AFR," "OCN," "NAM," "CAC," "SAM," "ARC," "ANT." This is a path parameter. |
| 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. |
Sample code
In the code below, we enter NAM
, which is the region code for North America, for the parameter regionCode
on line 1. You can try adding the optional parameter language
to the URL on lines 3–4 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.
regionCode = 'NAM'url = 'https://dataservice.accuweather.com/locations/v1/countries/' \+ regionCode + '?apikey={{API_KEY}}'response = requests.get(url)printResponse(response)
The code above displays a list of all countries in 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 parameters
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 "en-us". |
| string | This is the country name in the English language. |