An HTTP/1.1 client, written from scratch for Node.js
An HTTP/1.1 client, written from scratch for Node.js.
Undici means eleven in Italian. 1.1 -> 11 -> Eleven -> Undici. It is also a Stranger Things reference.
Have a question about using Undici? Open a Q&A Discussion or join our official OpenJS Slack channel.
Looking to contribute? Start by reading the contributing guide
npm i undiciThe benchmark is a simple getting data example using 50 TCP connections with a pipelining depth of 10 running on Node 24.14.1.
…Using benchmark-https.js against an h1-over-TLS server (50 connections, pipelining depth 10, Node 24.14.1).
…Using benchmark-http2.js against an h2 server (50 connections, pipelining depth 10, Node 24.14.1).
…Node.js includes a built-in fetch() implementation powered by undici starting from Node.js v18. However, there are important differences between using the built-in fetch and installing undici as a separate module.
Node.js's built-in fetch is powered by a bundled version of undici:
// Available globally in Node.js v18+
const response = await fetch('https://api.example.com/data');
const data = await response.json();
// Check the bundled undici version
console.log(process.versions.undici); // e.g., "5.28.4"Pros:
Cons:
TypeError)Installing undici as a separate module gives you access to the latest features and APIs:
npm install undiciimport { request, fetch, Agent, setGlobalDispatcher } from 'undici';
// Use undici.request for maximum performance
const { statusCode, headers, body } = await request('https://api.example.com/data');
const data = await body.json();
// Or use undici.fetch with custom configuration
const agent = new Agent({ keepAliveTimeout: 10000 });
setGlobalDispatcher(agent);
const response = await fetch('https://api.example.com/data');Pros:
request, stream, pipeline)undici.requestProxyAgent, Socks5Agent, MockAgentCons:
ProxyAgent, Socks5Agent, MockAgent, etc.)undici.request for maximum speed)Based on benchmarks, here's the typical performance hierarchy:
undici.request() - Fastest, most efficientundici.fetch() - Good performance, standard compliancehttp/https - Baseline performanceIf you're currently using built-in fetch and want to migrate to undici:
// Before: Built-in fetch
const response = await fetch('https://api.example.com/data');
// After: Undici fetch (drop-in replacement)
import { fetch } from 'undici';
const response = await fetch('https://api.example.com/data');
// Or: Undici request (better performance)
import { request } from 'undici';
const { statusCode, body } = await request('https://api.example.com/data');
const data = await body.json();fetch and FormData togetherWhen you send a FormData body, keep fetch and FormData from the same
implementation.
Use one of these patterns:
// Built-in globals
const body = new FormData()
body.set('name', 'some')
await fetch('https://example.com', {
method: 'POST',
body
})// undici module imports
import { fetch, FormData } from 'undici'
const body = new FormData()
body.set('name', 'some')
await fetch('https://example.com', {
method: 'POST',
body
})If you want the installed undici package to provide the globals, call
install() first:
import { install } from 'undici'
install()
const body = new FormData()
body.set('name', 'some')
await fetch('https://example.com', {
method: 'POST',
body
})install() replaces the global fetch, Headers, Response, Request, and
FormData implementations with undici's versions, so they all match. It also
installs undici's WebSocket, CloseEvent, ErrorEvent, MessageEvent, and
EventSource globals.
Avoid mixing a global FormData with undici.fetch(), or undici.FormData
with the built-in global fetch().
You can check which version of undici is bundled with your Node.js version:
console.log(process.versions.undici);Installing undici as a module allows you to use a newer version than what's bundled with Node.js, giving you access to the latest features and performance improvements.
import { request } from 'undici'
const {
statusCode,
headers,
trailers,
body
} = await request('http://localhost:3000/foo')
console.log('response received', statusCode)
console.log('headers', headers)
for await (const data of body) { console.log('data', data) }
console.log('trailers', trailers)Undici provides a powerful HTTP caching interceptor that follows HTTP caching best practices. Here's how to use it:
…Cache-Control and Expires headersETag and Last-Modified validationUndici provides an install() function to add fetch-related and other web API classes to globalThis, making them available globally:
import { install } from 'undici'
// Install undici's global web APIs
install()
// Now you can use fetch classes globally without importing
const response = await fetch('https://api.example.com/data')
const data = await response.json()
// All classes are available globally:
const headers = new Headers([['content-type', 'application/json']])
const request = new Request('https://example.com')
const formData = new FormData()
const ws = new WebSocket('wss://example.com')
const eventSource = new EventSource('https://example.com/events')The install() function adds the following classes to globalThis:
fetch - The fetch functionHeaders - HTTP headers managementResponse - HTTP response representationRequest - HTTP request representationFormData - Form data handlingWebSocket - WebSocket clientCloseEvent, ErrorEvent, MessageEvent - WebSocket eventsEventSource - Server-sent events clientWhen you call install(), these globals come from the same undici
implementation. For example, global fetch and global FormData will both be
undici's versions, and WebSocket and EventSource will also come from
undici, which is the recommended setup if you want to use undici through
globals.
This is useful for:
The body mixins are the most common way to format the request/response body. Mixins include:
[!NOTE] The body returned from
undici.requestdoes not implement.formData().
[!WARNING] Calling
body.formData()on a fetch response causes undici to buffer and parse the entire body. Since this is dictated by the spec,body.formData()must only be called on responses from trusted servers.
Example usage:
import { request } from 'undici'
const {
statusCode,
headers,
trailers,
body
} = await request('http://localhost:3000/foo')
console.log('response received', statusCode)
console.log('headers', headers)
console.log('data', await body.json())
console.log('trailers', trailers)Note: Once a mixin has been called then the body cannot be reused, thus calling additional mixins on .body, e.g. .body.json(); .body.text() will result in an error TypeError: unusable being thrown and returned through the Promise rejection.
Should you need to access the body in plain-text after using a mixin, the best practice is to use the .text() mixin first and then manually parse the text to the desired format.
For more information about their behavior, please reference the body mixin from the Fetch Standard.
This section documents our most commonly used API methods. Additional APIs are documented in their own files within the docs folder and are accessible via the navigation list on the left side of the docs site.
For the top-level APIs below, the url argument supplies the request origin and
path. Do not pass origin or path in the second options argument. The linked
Dispatcher option types include those fields because dispatcher methods are
lower-level APIs that do not receive a separate url argument.
undici.request([url, options]): PromiseArguments:
string | URL | UrlObjectRequestOptionsDispatcher - Default: getGlobalDispatcherString - Default: PUT if options.body, otherwise GETReturns a promise with the result of the Dispatcher.request method.
Calls options.dispatcher.request(options).
See Dispatcher.request for more details, and request examples for examples.
undici.stream([url, options, ]factory): PromiseArguments:
string | URL | UrlObjectStreamOptionsDispatcher - Default: [getGHTTP/2 idle reaper crashes the process: InformationalError: socket idle timeout is emitted on a ClientHttp2Stream with no 'error' listener
build: build milo through the same WASM build infrastructure as llhttp
Fix `test:typescript` script for Windows compatibility
SSRF protection in undici / native-node-fetch
Importing undici v8 silently breaks Node's bundled fetch via the legacy globalDispatcher bridge (v8's stricter core Request rejects opts the bundled v6 core accepts — e.g. request-set Content-Length)
HTTP/1.1 304 with Content-Length closes the connection
HTTP2 Requests hang upon receiving 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
undici-types 6.21.x: a trusted-publisher re-release would unblock current `@types/node@20/22` under pnpm `trustPolicy`
`FormData` body with an errored `Blob` part crashes the process with an unhandled rejection instead of erroring the body stream (regression in 7.20.0)
Should Fetch retry reusable request bodies after HTTP/2 GOAWAY?