Text Search
Learn how to get a global list of cities and locations by using text search.
We'll cover the following
In this lesson, we'll learn how to search for locations worldwide by using text search.
Search for global locations
The Text Search endpoint allows us to retrieve a list of all locations around the globe that match our given text. It provides detailed information for each location, including, but not limited to, its geographical position, timezone, and population. We can also limit the number of results retrieved as per our requirements.
For this purpose, we make a GET request to the following URL.
http://dataservice.accuweather.com/locations/v1/search
Request parameters
Parameter | Type | Category | Description |
| string | required | This is our API key. This is a query parameter. |
| string | required | This is the search text. This is a query 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. |
| integer | optional | This is the number of results that we have to return. The default value is This is a query parameter. |
| string | optional | This indicates when to include alias locations in the results. By default, alias locations are returned only in case of no exact match with the search text. Allowable values include This is a query parameter. |
In the code below, we enter Seattle
as our search text for the parameter q
on line 16. You can try adding the optional parameter(s) to the queryParameters
object on lines 18–21 to fine-tune your search.
Click the "Run" button to see the output.
// Importing libraries hereimport fetch from "node-fetch";// Define API key hereconst API_KEY = '{{API_KEY}}';// Define endpoint URL hereconst url = new URL(`http://dataservice.accuweather.com/locations/v1/search`);// Define header parameters hereconst headerParameters = {contentType: 'application/json',};// Define query parameters hereconst q = 'Seattle';const queryParameters = new URLSearchParams({apikey: API_KEY,q});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function getLocations() {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 callgetLocations();
Let's take a quick look at the code given in the widget above:
Line 8: We define the endpoint URL.
Lines 16–21: We define the query parameters.
Lines 30–41: We define a custom function
getLocations()
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
getLocations()
function.
The code above displays a list of all, or the specified number of, global locations that match our given text, along with 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 location's country, including its code, and its localized and English names. |
| object | This contains information regarding the location's designated timezone. |
| object | This contains information regarding the location'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.