VOOZH about

URL: https://blog.logrocket.com/using-needle-send-http-requests-nodejs/

⇱ Using Needle to send HTTP requests in Node.js - LogRocket Blog


2021-11-18
1049
#node
Atharva Deosthale
77849
👁 Image

See how LogRocket's Galileo AI surfaces the most severe issues for you

No signup required

Check it out

While dealing with backends, there are situations in which we need to make HTTP requests to other APIs for information. There are a lot of different packages in Node.js that help with that, such as node-fetch and Axios.

👁 Image

Today we are going to look at another package called Needle, and see why we should use it instead of something like Axios.

Needle is a HTTP client made for Node that helps us send HTTP requests to external sources to retrieve data. This should be used in the backend in situations where you don’t want to expose sensitive information such as API keys to the client application.

Let’s see how this package actually works. In this tutorial, we will be using a fake REST API (JSONPlaceholder) to serve data through our server with Needle to make the HTTP request.

🚀 Sign up for The Replay newsletter

The Replay is a weekly newsletter for dev and engineering leaders.

Delivered once a week, it's your curated guide to the most important conversations around frontend dev, emerging AI tools, and the state of modern software.

Why you should use Needle

The reason I would choose Needle rather than Axios is that Needle is made for the backend, while Axios is made for both the frontend and the backend. Not that Axios is a bad package; I’ve used both of the packages quite a lot and both of them are awesome. But Needle’s specific use case resides in the backend, as it’s tagline reads, “the leanest and most handsome HTTP client in the Nodelands”.

Needle is made out of Node-specific libraries, which means it won’t work in the browser at the frontend. If we take a closer look at the User-Agent header Needle sets by default in its requests, it’s set to the running Node platform and version. This means that Needle is meant to run on Node and uses smaller-sized Node packages to perform requests. This makes this package less bloated for backend.

Also, Needle’s package size is smaller in comparison to Axios. While package size may not matter as much these days, smaller package sizes like Needle are great for optimization purposes. If package size is a concern for you, it makes sense to use Needle in the backend and Axios in the frontend.

When you are working with the backend, you know for sure that the code is not going to be used in the frontend in any way, so why not save some space and use Needle instead of Axios?

Using Axios and Needle is quite similar. You won’t find performance differences in a normal usage, but when you really have to optimize things to make your server faster, using lightweight packages like Needle is a good option.

Creating a sample Node.js server

Let’s see Needle in action. Our first step is to set up a sample Node and Express server.

To do that, go to a safe project workspace folder and type the following command to set up the package.json for our project:

npm init -y

Now let’s install the Needle and Express packages like so:

npm install needle express

Next, create a new file called index.js and use node index in your terminal to run it. I highly recommend using nodemon, because it auto reloads the server once you make changes to a file.

Install it using the following command (you need to install it just once per machine):

npm install -g nodemon

Setting up a sample Express server

Now, go to the index.js file, and use the following code to set up a bare bones Express app. We will also import Needle here for further use:

const express = require("express");
const needle = require("needle");
const app = express();
const PORT = process.env.PORT || 8080;

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

In the code above, process.env.PORT || 8080 means that if there is no port provided in environment variables, the server will run on port 8080. This is quite useful when you are deploying on services like Heroku and you need some way to test the server locally.

Setting up a route and working with Needle

Now let’s create a route that, when sent a request, will return a response from a fake API. This will be a simple GET request:

app.get("/posts", async (req, res) => {
 try {
 const response = await needle(
 "get",
 "https://jsonplaceholder.typicode.com/posts"
 );
 console.log(response.body);
 res.json(response.body);
 } catch (err) {
 console.log(err);
 res.status(500).json({ message: "Internal server error" });
 }
});

Let’s look at what are we doing in the above code. First, we are setting up an Express route at the route /posts and using an asynchronous callback function. Then, we are making a GET request from Needle, and specifying the URL to which we are sending the HTTP request.

Our response is stored in response.body and we send this response back to the user using res.json().


Over 200k developers use LogRocket to create better digital experiences

👁 Image
Learn more →

We use the catch block if anything goes wrong in making the request, and send an error message as a response if so.

Now, if you hit http://localhost:8080/posts using an HTTP client like Postman or simply using a browser, you will get a response with a lot of dummy posts in JSON format.

We can do many more things, like checking the HTTP status code. Let’s return the status code we got from the request back as a response:

res.status(response.statusCode).json(response.body);

In the above snippet, we are just adding the .status() method from express to return a status code as a response.

Similarly, you can pass headers to requests to send request data such as API Keys

const response = await needle(
 "get",
 "https://jsonplaceholder.typicode.com/posts",
 { headers: { "x-my-custom-key": "abc123" } }
);

And you can parse response headers like this:

console.log(response.headers);

Conclusion

Needle has been around for many years, even before Axios. Make sure to try it out once for yourself! More and more people are starting to use Needle this year, which indicates that devs want more optimization. The first step towards it is using packages with smaller sizes.

However, if you are just a student or you are working on a simple project that does not necessarily require optimization, I can’t see a difference between using Axios over Needle. I think the optimization part is more useful in large scale, production-level apps where developers need to keep things as clean as possible.

200s only 👁 Image
Monitor failed and slow network requests in production

Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third-party services are successful, try LogRocket.

👁 LogRocket Network Request Monitoring

LogRocket lets you replay user sessions, eliminating guesswork around why bugs happen by showing exactly what users experienced. It captures console logs, errors, network requests, and pixel-perfect DOM recordings — compatible with all frameworks.

LogRocket's Galileo AI watches sessions for you, instantly identifying and explaining user struggles with automated monitoring of your entire product experience.

LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. Start monitoring for free.

👁 Image
👁 Image
👁 Image

Stop guessing about your digital experience with LogRocket

Get started for free

Recent posts:

Debug Next.js apps with AI agents and next-browser

Learn how next-browser gives AI agents runtime context for debugging Next.js apps, including React props, hydration, PPR, forms, and performance.

👁 Image
Emmanuel John
Jun 17, 2026 ⋅ 9 min read

Stop hardcoding LLM SDKs: Dynamic LLM routing with OpenRouter and Next.js

Build dynamic LLM routing in Next.js with OpenRouter, TanStack AI, task classification, model fallbacks, and cost-aware routing.

👁 Image
Chizaram Ken
Jun 16, 2026 ⋅ 13 min read

What is TSRX?: What JSX would look like if it were designed today

TSRX adds first-class control flow, conditional hooks, and scoped styles to React via a TypeScript compiler extension — no new framework required.

👁 Image
Ikeh Akinyemi
Jun 12, 2026 ⋅ 6 min read

How to add authentication to a React Native app with Better Auth

Learn how to build a full React Native auth system using Better Auth and Expo — with email/password login, Google OAuth, session persistence, and protected routes.

👁 Image
Chinwike Maduabuchi
Jun 9, 2026 ⋅ 13 min read
View all posts

Would you be interested in joining LogRocket's developer community?

Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

Sign up now