Everything you need to know about Node.js fetch API

Fri Apr 07 2023

3 min read

In the past, it was difficult for developers to call APIs on the server because of the complex implementation of Node.js HTTP library.

Hence, everyone relied on third-party packages like Axios, got, etc. However, with Node.js v18, fetch API has been added to simplify the process of requesting APIs on Node servers.

In this guide, we will dive into everything you need to know about Node.js Fetch API and how it can streamline your development workflow.

Node.js Fetch API

Node.js Fetch API is a built-in module in Node.js that enables developers to make API requests and handle responses on the server. It also provides a simple way to set and retrieve HTTP headers and allows us to customize requests with options such as the request method, headers, and body.

Node.js Fetch API Methods & Interface

Let's take a look at all the methods the Node.js to fetch API offers:

fetch()

The fetch() method takes a required parameter, i.e., API endpoint URL, and an object as a second parameter that can be used to customize the request, such as setting headers, request method, or body.

js
fetch('API endpoint URl', {})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));

Headers

This interface is used to set and get HTTP headers. This interface takes an optional headers parameter that can be used to initialize a Headers object.

js
const headers = new Headers({
'Content-Type': 'application/json'
});

Request

This interface is used to create and customize HTTP requests. It takes a mandatory URL parameter and an options parameter that can be used to set request options, such as headers or request methods.

Response

Response interface is used to handle HTTP responses. It takes a required body parameter, and an options parameter.

js
const response = new Response(JSON.stringify({
message: 'Success'
}), {
status: 200,
headers: {
'Content-Type': 'application/json'
}
});

Using Node.js Fetch API

For this particular example, we'll be using the Facts API by API Ninjas from Rapid API Hub. Before you can start using it, please sign up on Rapid API Hub and get your Rapid API key.. This will enable you to send requests to any APIs present on Rapid API Hub if you are subscribed to them.

Loading component...

Let's take a look at how we can use the fetch() method to retrieve a random fact from the Facts API in our Node.js application.

js
const apiKey = 'YOUR_API_KEY';
const apiUrl = 'https://facts-by-api-ninjas.p.rapidapi.com/v1/facts';
fetch(apiUrl, {
headers: {
'X-RapidAPI-Key': apiKey,
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => console.log(data.fact))
.catch(error => console.error(error));

Wrap up

By understanding the basics of the Fetch API and its methods, you can easily integrate it into your Node.js projects and make the most of its capabilities. Whether you're fetching data from an API or uploading data to a server, the Fetch API is a valuable tool for any Node.js developer.