How to make an API request in Node.js?
Tags: node, javascript
Published: 2021-07-17 10:52:45

---
title: How to make an API request in Node.js?
published: true
date: 2021-03-29 00:00:00 UTC
tags: node, javascript
canonical_url: https://www.mariokandut.com/how-to-make-an-API-request-in-Node-javascript/
---

Making HTTP requests is a core functionality for modern language and a daily task for a developer. One task you’ll encounter often in Node.js is making HTTP requests to an external API from a server.

Let's take a look at three options on how to make an HTTP request, there are many more available.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#3-ways-to-make-http-requests-in-nodejs)3 Ways to Make HTTP Requests in Node.js

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#1-http--the-default)1. HTTP – the default

The default HTTP module is the built-in way to make HTTP requests in Node.js. The module can just be required without installing it, which is a big benefit if you don't want to add more dependencies to your project.

The HTTP module unfortunately also has a few downsides. You’re required to receive response data in chunks, rather than just providing a callback function to be executed when the data is fully received, and you have to parse the data manually. Yes, if the data is JSON formatted it is very simple `json()`, but still, it's an extra step. Currently, the module supports HTTP by default and needs to be required for https, so `const http = require('http');` or `const https=require('https');`.

Let's look at a code example to request a list of todos from a placeholder API.

```
const https = require('https');
const options = {
  hostname: 'jsonplaceholder.typicode.com',
  port: 443,
  path: '/todos',
  method: 'GET',
};

const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode}`);

  res.on('data', d => {
    process.stdout.write(d);
  });
});

req.on('error', error => {
  console.error(error);
});
```

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#2-fetch-node-fetch)2. Fetch (node-fetch)

`node-fetch` is a lightweight module that enables us to use `fetch()` in Node. The package is very similar to `window.fetch()` in native JavaScript, but has a few differences (see [node-fetch docs](https://github.com/node-fetch/node-fetch/blob/master/docs/v3-LIMITS.md)).

Let's look at an example:

Create a project folder.

```
mkdir node-api-fetch
```

Initialize project with `npm init -y` to be able to install node packages.

```
cd node-api-fetch
npm init -y
```

Install `node-fetch` to make fetch requests.

```
npm install node-fetch
```

Create an `index.js` file.

```
touch index.js
```

Add code.

```
// import node-fetch
const fetch = require('node-fetch');
// set url as constant
const URL = 'https://jsonplaceholder.typicode.com/todos';

fetch(URL)
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(err => console.error(err));
```

The `res.body` in `node-fetch` is a readable stream, so decoding can be handled independently, which is very convenient. The downside is that it only supports `res.text()`, `res.json()`, `res.blob()`, `res.arraybuffer()`, and `res.buffer()`. There is no caching built-in and no server-side cookie store, so `Set-Cookie` headers have to extracted manually.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#3-axios)3. Axios

[Axios](https://github.com/axios/axios) is a Promise based HTTP client. It can be used in the browser (frontend, SPA, etc.) and in Node.js. Using Promises is a way to deal with asynchronous code. Have a look at [Understanding Promises in Node.js](https://www.mariokandut.com/understanding-promises-in-node-js/) for more details on Promises in Node.js.

Let's look at an example:

Create a project folder.

```
mkdir node-api-axios
```

Initialize project with `npm init -y` to be able to install node packages.

```
cd node-api-axios
npm init -y
```

Install `axios` to make fetch requests.

```
npm install axios
```

Create an `index.js` file.

```
touch index.js
```

Add code.

```
// import node-fetch
const axios = require('axios');
// set url as constant
const URL = 'https://jsonplaceholder.typicode.com/todos';

axios
  .get(URL)
  .then(response => {
    console.log(response);
  })
  .catch(error => {
    console.log(error);
  });
```

A convenient feature of Axios is that JSON gets parsed by default. There are many other options Axios provides, please have a look at the official [Axios Docs](https://axios-http.com/docs/intro).

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- Node.js has a built-in module to make API requests `http`
- The concept of handling asynchronous code has to be understood to make API requests.
- There are many utilities available to make API requests convenient.
- The module **node-fetch** implements the Fetch-API for Node.js.
- **Axios** is another utility module and automatically parses JSON data.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#references-and-big-thanks)References (and Big thanks):

[Sam Agnew](https://www.twilio.com/blog/2017/08/http-requests-in-node-js.html),[Nodejs.dev](https://nodejs.dev/learn/making-http-requests-with-nodejs),[node-fetch](https://www.npmjs.com/package/node-fetch),[MDN - Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API),[Axios](https://github.com/axios/axios)