undici makes it easy to perform fast HTTP requests in Node.js with a straightforward API. This guide walks you through installing the package, making your first request, consuming response bodies, and setting up connection pooling — all with real code patterns from the undici source.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
Installation
undici v8.x requires Node.js ≥ 22.19.0. If you are on an older Node.js release, install undici v6.x (
npm install undici@6) which supports Node.js ≥18.17.Making a basic request
Import request
undici ships as a CommonJS module with named exports. Use ESM
import or CJS require:- ESM
- CJS
ESM import
Call request()
Pass a URL string, The default HTTP method is
URL object, or a URL-like object. request() resolves with statusCode, headers, trailers, and body:Making a request
GET. If you pass a body option, the method defaults to PUT.Consume the response body
The Available body consumption methods:
body object implements the Fetch Standard body mixin. Use one of these methods to read it:Reading the response as JSON
| Method | Returns |
|---|---|
body.json() | Parses body as JSON |
body.text() | Reads body as a UTF-8 string |
body.arrayBuffer() | Returns body as an ArrayBuffer |
body.blob() | Returns body as a Blob |
body.bytes() | Returns body as a Uint8Array |
Always consume the response body
If you only need headers and want to discard the body, callbody.dump() or use the HEAD method:
Discarding the body safely
Making a fetch() request
undici also exports a standards-compliantfetch() function. It is a drop-in replacement for the built-in global fetch and works with the same options:
Using undici fetch
dispatcher option directly to fetch to control connection behavior:
fetch with a custom Agent
Using an Agent for connection pooling
By default, undici uses a globalAgent that manages connection pools automatically. You can configure it explicitly to tune keep-alive behavior, connection limits, and more:
Configuring a global Agent
Sending request bodies
Pass abody option to send data. The method defaults to PUT when a body is present — set it explicitly to POST for most APIs:
Sending a JSON body
Error handling
undici throws typed errors that you can inspect. The most common are network errors (UND_ERR_CONNECT_TIMEOUT, UND_ERR_SOCKET) and response errors when using the responseError interceptor:
Handling errors
Unlike built-in
fetch, undici.request() does not throw on 4xx or 5xx responses by default — it resolves with the statusCode. You must check it yourself or add the responseError interceptor.Aborting a request
Pass anAbortSignal via the signal option to cancel in-flight requests:
Aborting with a timeout
Next steps
Introduction
Learn about all of undici’s capabilities and APIs
undici vs. built-in fetch
Understand when to use the module vs. Node.js globals
Dispatchers
Deep dive into Client, Pool, Agent, and ProxyAgent
Mocking
Intercept HTTP requests in tests with MockAgent