Search by Region and Subregion

Learn how to search for a country by region and subregion using the REST Countries API.

Searching for a country by region and subregion can be helpful for several reasons. For example, if you are planning a trip and want to learn more about the countries in a particular part of the world, searching by region and subregion can help you quickly and easily find the information you need.

Additionally, knowing the region and subregion a country is located in can provide valuable context and background information that can help understand the country’s history, culture, and other aspects.

Finally, searching by region and subregion can also be helpful for organizing and grouping countries in a way that makes it easier to compare and contrast them.

Press + to interact

Search by region

The Region endpoint is used to search the list of all the countries that belong to a specific region. The URL for this endpoint is https://restcountries.com/v3.1/region/{region}.

Request parameters

We simply need to replace {region}, which is in the endpoint URL, with the name of the region we want to look up and then call the URL.

Go ahead and change the region in the query below by clicking the “Edit” button. Click the “Run” button to get the list of countries in the specified region.

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

In the code widget above:

  • Line 2: We import the required library.
  • Line 5: We assign the base URL of the Region 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 countries situated in the specified region. The information in the responses will be the same as the details explained in the An Overview of the REST Countries API lesson. This endpoint allows us to easily and quickly access detailed information about multiple countries that match our query.

Search by subregion

Using the Subregion endpoint, we can search for all the countries that belong to a specific subregion. The base URL for this endpoint is https://restcountries.com/v3.1/subregion/{region}.

Request parameters

We simply need to replace {region}, which is in the endpoint URL, with the subregion of the country we want to look up, and then call for that URL.

Go ahead and change the subregion in the query below by clicking the “Edit” button. Click the “Run” button to get the list of countries in the specified subregion.

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

In the code widget above:

  • Line 2: We import the required library.
  • Line 5: We assign the base URL of the Subregion 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 countries situated in the specified subregion. The information in the responses will be the same as the details explained in the An Overview of the REST Countries API lesson. This endpoint allows us to easily and quickly access detailed information about multiple countries that match our query.