undici
Fast HTTP/1.1 client written for Node.js that powers the global fetch, adding connection pooling, streaming bodies, and interceptors.
Official Documentation ↗v8.12.3. Check the official docs for the latest API surface.Installation
npm install undicipnpm add undiciMethods
No methods match this filter.
Fast HTTP/1.1 client written for Node.js that powers the global fetch, adding connection pooling, streaming bodies, and interceptors.
Overview
undici is the HTTP client that powers the global fetch in Node.js. It speaks HTTP/1.1 with its own optimized parser and keeps sockets alive in per-origin pools, which removes the per-request connection cost that naive fetch loops pay.
The package exposes layers: a spec-compatible fetch at the top, a leaner request helper in the middle, and Client, Agent, and Pool primitives underneath for direct control over sockets, pipelining, and timeouts. Response bodies are Node streams with convenience json() and text() terminators, so large payloads can be consumed incrementally without buffering.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
request— Performs an HTTP request and resolves with the raw status, headers, and a body stream that offers json() and text() helpers.fetch— The WHATWG fetch implementation shipped by undici, usable standalone for portable request code inside Node.js.Agent— Creates a connection-pool dispatcher shared across requests through the dispatcher option to bound sockets per origin.stream— Streams a response body into a Writable returned by the factory, passing opaque through options to the factory callback.
Usage Notes
- Reuse one Agent instance as dispatcher across requests instead of creating a new client per call; pools only help when they are shared.
- Set headersTimeout and bodyTimeout per agent or per request so a slow upstream cannot hang a socket forever.
- Consume the body of every response, or call body.dump(), to return the connection to the pool in a clean state.
Common Pitfalls
- Creating a new Agent inside a request handler silently disables pooling and leads to socket exhaustion under load.
- The response body is a stream: reading it twice or mixing json() with manual reads throws.
- Global fetch in older Node versions lags behind the published undici package; import fetch from undici when you need current behavior.
Errors
The following points are quoted verbatim from the official documentation (https://undici.nodejs.org/). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Warning Calling body.formData() on a fetch response causes undici to buffer and parse the entire body. (https://undici.nodejs.org/)
- If a server responds with more than 5 content-encodings (e.g., Content-Encoding: gzip, gzip, gzip, gzip, gzip, gzip ), the fetch will be rejected with an error. (https://undici.nodejs.org/)
- This will cause Response.redirect , new Request() , and fetch to throw an error when a relative path is passed. (https://undici.nodejs.org/)
Security notes
Quoted from the official documentation (https://undici.nodejs.org/); this is not a complete threat model.
- OpenJS Foundation AI Coding Assistants Policy Bylaws Code of Conduct Cookie Policy Privacy Policy Security Policy Terms of Use Trademark List Trademark Policy (https://undici.nodejs.org/)
FAQ
- How does undici relate to the global fetch in Node.js?
- Node.js bundles undici internally to implement the global fetch API. Installing the package gives you the same fetch plus lower-level APIs such as request, Agent, and Client that expose pooling, pipelining, and raw body streams beyond the standard surface.
- When should I use request instead of fetch?
- request returns raw status, headers, and a body stream without the spec-mandated Response wrapper, which avoids spec overhead, and accepts a dispatcher for connection pooling. Use fetch for portable code and request for hot paths inside Node.js services.
- How do I bound connections to a single upstream?
- Create one Agent with connections and pipelining options, store it at module scope, and pass it as the dispatcher option on every request. Sharing the same agent applies the pool process-wide instead of per call.
Report a problem with this API docOpens a pre-filled issue on GitHub.