Filter Response
Learn how to filer responses of the REST Countries API
We'll cover the following
The REST Countries API allows us to specify the output fields of a request made to an API endpoint. This can be especially useful when working with large datasets because it allows us to narrow down the results and focus on the most relevant information. For instance, if we are querying the REST Countries API database and only need to see a few specific fields, we can specify which fields we want to include in the response of our request. This can help to streamline our work by reducing the amount of data we need to process, making it easier to find the information we need.
We can specify the required fields in the REST Countries API request by using the fields
parameter. We can append this query parameter with the URLs of the endpoints discussed earlier to filter the response.
Here are some examples of how to specify the required fields. In these examples, we will specify the fields and check the effect on the response.
Filter a single field
In this example, we’ll only specify a single field to be returned in the response.
We’ll use the URL of the All endpoint, which returns the information about all available countries. The URL for this endpoint is https://restcountries.com/v3/all
. We’ll append fields=name
as a query parameter with this URL to get only the name
field in the response.
Click the “Run” button to fetch the specified field.
// Import Librariesimport fetch from 'node-fetch';// Enpoint URLconst endpointUrl = new URL('https://restcountries.com/v3/all?fields=name');// Request optionsconst options = {method: 'GET'};// Function to make the API callasync function searchByCurrency() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}// Calling the functionsearchByCurrency();
In the code widget above:
- Line 2: We import the required library.
- Line 5: We assign the base URL of the All endpoint to the
endpointUrl
variable and setfields
toname
. - 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.
The API call will return a JSON response containing only the name
field.
Filter multiple fields
In this example, we’ll look at how to filter multiple response fields.
Just like the previous request, we’ll use the URL of the All endpoint, which returns the list of all countries. We’ll append the fields
query parameter with the URL and set it to the required fields separated by a comma.
Click the “Run” button to fetch the specified fields.
// Import Librariesimport fetch from 'node-fetch';// Enpoint URLconst endpointUrl = new URL('https://restcountries.com/v3/all?fields=name,currencies');// Request optionsconst options = {method: 'GET'};// Function to make the API callasync function searchByCurrency() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}// Calling the functionsearchByCurrency();
In the code widget above:
- Line 2: We import the required library.
- Line 5: We assign the base URL of the All endpoint to the
endpointUrl
variable and appendfields=name,currencies
with it to filter thename
andcurrencies
fields. - 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.
The API call will return a JSON response containing only the name
and currencies
fields.