Search by Currency and Language

Learn how to search for a country by currency and language using the REST Countries API.

There are many reasons why someone might want to search for a country by its currency and language. For example, a traveler may want to know a country’s currency so that they can budget their trip and easily exchange money.

Knowing a country’s primary language can also be helpful for travelers because it can make communicating and understanding local customs easier.

Additionally, knowing a country’s language and currency can be useful for business purposes, such as facilitating international trade or making financial transactions.

Press + to interact

Search by currency

The Currency endpoint is used when we want to search the list of countries that use a specific currency. The URL for this endpoint is https://restcountries.com/v3.1/currency/{currency}.

Request parameters

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

Go ahead and change the currency in the query below by clicking the “Edit” button. Click the “Run” button to list all the countries that use the specified currency.

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

In the code widget above:

  • Line 2: We import the required library.
  • Line 5: We assign the base URL of the Currency 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 countries whose currency name matches 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. This endpoint allows us to easily and quickly access detailed information about multiple countries whose currencies match our query.

Search by language

Many different languages are spoken around the world. This endpoint will allow us to search using a language’s name and return all the countries that use the specific language. The URL for this endpoint is https://restcountries.com/v3.1/lang/{lang}.

Request parameters

To call this endpoint, we simply need to replace {lang} with the name of the language we want to look up, and then we call for that URL.
Let’s see the code for this endpoint below. When we execute the code, it will return the details about the country we want to search for.

Go ahead and change the language in our query by clicking the “Edit” button. Click the “Run” button to list all the countries that use the specified language.

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

In the code widget above:

  • Line 2: We import the required library.
  • Line 5: We assign the base URL of the Language 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 countries whose language name matches 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. This endpoint allows us to easily and quickly access detailed information about multiple countries whose languages match our query.