Search by Capital City and Calling Code

Learn how to search for a country by its capital city and calling code using the REST Countries API.

The capital city of a country is often the political and administrative center of the country, and it may be the largest and most populous city in the country as well. The calling code is the code that is used to make phone calls to a specific country.

Searching for a country by its capital city and calling code can be helpful in several ways. For example, if you are trying to make a phone call to someone in a specific country, you can use the calling code to determine the correct way to dial the number. Additionally, knowing the capital city can be a good starting point for your research if you are trying to find information about a specific country.

Search by the capital city

The Capital City endpoint is helpful when we know a capital city’s name and want to collect information about its country. The URL for this endpoint is https://restcountries.com/v3.1/capital/{capital}.

Request parameters

We’ll replace {capital}, which is in the endpoint URL, with the name of the capital we want to look up and then call that URL.

Go ahead and change the capital in the query below by clicking the “Edit” button. Click the “Run” button to get information about the country to which the specified capital city belongs.

Press + to interact
// Import Libraries
import fetch from 'node-fetch';
// Enpoint URL
const endpointUrl = new URL('https://restcountries.com/v3.1/capital/{{Capital_City}}');
// Request options
const options = {
method: 'GET'
};
// Function to make the API call
async function searchByCapitalCity() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
// Calling the function
searchByCapitalCity();

In the code widget above:

  • Line 2: We import the required library.
  • Line 5: We assign the base URL of the Capital City endpoint to the endpointUrl variable.
  • Lines 8–10: We set the request parameters.
  • Lines 13–20: We create a function that makes the API call. This function will try to fetch the required data using the specified URL and print the response. In case of an error, it’ll print an error message.
  • Line 23: We call the function that makes the API call.

Response fields

This API call will return a JSON response containing detailed information about the country whose capital city’s name matches the query we provided. The information in the responses will be the same as the details explained in the An Overview of the REST Countries API lesson.

Search by calling code

When trying to identify the location of an unknown phone number, searching for the country by calling code can help determine where the number is located. The base URL for this endpoint is https://restcountries.com/v2/callingcode/{callingcode}.

Request parameters

We will replace {callingcode} , which is in the endpoint URL, with the calling code of the country we want to look up and then call that URL.

Go ahead and change the calling code in the query below by clicking the “Edit” button. Click the “Run” button to get the country to which the specified calling code belongs.

Press + to interact
// Import Libraries
import fetch from 'node-fetch';
// Enpoint URL
const endpointUrl = new URL('https://restcountries.com/v2/callingcode/{{Calliing_code}}');
// Request options
const options = {
method: 'GET'
};
// Function to make the API call
async function searchByCallingCode() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
// Calling the function
searchByCallingCode();

In the code widget above:

  • Line 2: We import the required library.
  • Line 5: We assign the base URL of the Calling Code endpoint to the endpointUrl variable.
  • Lines 8–10: We set the request parameters.
  • Lines 13–20: We create a function that makes the API call. This function will try to fetch the required data using the specified URL and print the response. In case of an error, it’ll print an error message.
  • Line 23: We call the function that makes the API call.

Response fields

This API call will return a JSON response containing detailed information about the country whose calling code matches the query we provided. The information in the responses will be the same as the details explained in the An Overview of the REST Countries API lesson.