Undici exposes a set of top-level dispatch functions that use the global dispatcher under the hood. Each function accepts a URL as the first argument and an options object, so you don’t need to instantiate aDocumentation 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.
Client or Pool manually. The global dispatcher is an Agent instance by default, but you can replace it with setGlobalDispatcher.
All response bodies returned by
request must be fully consumed or explicitly destroyed. Failing to do so will exhaust the connection pool and stall subsequent requests.undici.request(url, options)
Performs an HTTP request and returns a promise that resolves with the full response. Non-idempotent requests are not pipelined; idempotent requests are automatically retried on indirect pipeline failures (except when the request body is a stream).
Parameters
The request URL. Accepts a plain string, a WHATWG
URL object, or a Node.js
UrlObject (the shape returned by url.parse()). When an UrlObject is
used, origin is derived from protocol, auth, hostname, and port;
path is derived from pathname and search.Request configuration. All fields are optional unless noted.
Return value
ReturnsPromise<ResponseData>.
HTTP response status code.
HTTP status message, e.g.
'OK' or 'Not Found'.Response headers. All keys are lower-cased, e.g.
'content-type'. Header
values are strings; headers that appear more than once become string arrays.Response body as a Node.js
Readable. Also implements the
Fetch body mixin:The body cannot be consumed twice. Calling json() after text() throws
a TypeError.Starts as an empty object and is populated with HTTP trailers after the body
'end' event fires.The value passed in via
options.opaque.Internal dispatcher context forwarded from the connection.
Examples
- GET with JSON
- POST with JSON
- Abort
GET with JSON body
undici.stream(url, options, factory)
A faster alternative to request that writes the response body directly into a
Writable stream returned by the factory function. This avoids creating an
intermediate Readable and is well-suited for proxying responses (e.g. into a
Fastify or Express response object).
Basic stream example
Parameters
Same as
request. See the URL parameter description above.Same options as
request. The method field is required for stream.Called once response headers are received. Must return a
stream.Writable
that the response body is piped into.Return value
ReturnsPromise<StreamData>.
The value passed via
options.opaque.HTTP trailers received after the body ends.
Internal dispatcher context.
undici.pipeline(url, options, handler)
Designed for use with Node.js stream.pipeline. Returns a Duplex stream: writes to it become the request body, and reads from it produce the response body. The handler function receives response metadata and must return a Readable.
Pipeline echo example
Parameters
Same as
request.Extends
RequestOptions with one additional field:Called when headers are received. Must validate the response, throw on error,
and return a
Readable that the caller reads from.Return value
Returnsstream.Duplex. The duplex is both the request body writer and the
response body reader. Wire it up with stream.pipeline to propagate errors
automatically.
undici.connect(url, options)
Opens an HTTP CONNECT tunnel to the target. Used for proxying, TLS
passthrough, or any other two-way byte-stream protocol. Returns a raw
stream.Duplex socket.
CONNECT tunnel
Parameters
URL of the HTTP server that handles
CONNECT. The path in options becomes
the tunnel target address sent in the CONNECT request line.Return value
ReturnsPromise<ConnectData>.
Should be
200 Connection established on success.Response headers from the proxy.
The raw bidirectional socket to the tunnel target.
Value from
options.opaque.undici.upgrade(url, options)
Sends an HTTP upgrade request to switch protocols (e.g. WebSocket, HTTP/2
cleartext). Returns the upgraded raw socket along with the server’s 101 Switching Protocols headers.
WebSocket upgrade handshake
Parameters
Base URL of the HTTP server.
Return value
ReturnsPromise<UpgradeData>.
Response headers including
upgrade and connection.The raw upgraded socket. The protocol handshake is complete; you can begin
the wire protocol immediately.
Value from
options.opaque.