Search by Name

Learn how to search for a country by name using the REST Countries API.

When searching any country, searching by name is the best way to go about it. The REST Countries API offers the feature to do this. In return, we get all the available information about that country. We have the option to include the entire name or only a portion of it in our query.

Search by partial name

The Name endpoint is used to search through all available countries. This endpoint gives us access to the complete database of the REST Countries API. The URL for this endpoint is https://restcountries.com/v3.1/name/{name}.

Request parameters

While calling the Name endpoint, we simply need to replace {name} in the endpoint URL with the name of the country we want to look up.

Change the name in our query below by clicking the “Edit” button. Click the “Run” button to list all the countries whose names partially match our search query.

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

In the code widget above:

  • Line 2: We import the required library.
  • Line 5: We assign the base URL of the Name endpoint to the endpointUrl variable.
  • Lines 8–10: We set the request option.
  • 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 will 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 countries whose names partially match the query we provide. 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 full name

The REST Countries API offers an endpoint specifically to search for countries by their full name. This endpoint is more precise than the Name endpoint, which returns all countries whose names partially match the query. To use the Full Name endpoint, we need to specify the name of the country we want to look up in the API request URL and set the fullText query parameter to true. The URL for this endpoint is https://restcountries.com/v3.1/name/{name}?fullText=true.

Request parameters

When making the API call, we need to replace the {name} parameter with the full name of the country we want to look up.

Go ahead and change the name in the query below by clicking the “Edit” button. Click the “Run” button to get the available information about the country whose name matches the query.

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

In the code widget above:

  • Line 2: We import the required library to make the API call.
  • Line 5: We assign the base URL of the Full Name endpoint to the endpointUrl variable.
  • Lines 8–10: We set the request type to GET.
  • 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 will 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 name matches our query. The information in the responses will be the same as the details explained in the An Overview of the REST Countries API lesson. If there is no country whose full name matches the provided name, we’ll get the status 404.