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.
Client maps each instance to a single TCP or TLS connection to a given origin. Pipelining is disabled by default (pipelining: 1), meaning each connection handles one request at a time. You can raise pipelining to send multiple requests over the same connection concurrently, or use Pool when you need multiple connections to the same origin.
Client is a concrete implementation of Dispatcher. All methods
(request, stream, pipeline, dispatch, connect, upgrade,
close, destroy) are inherited from Dispatcher — see the
Dispatcher reference for full method signatures.Constructor
new Client(url, options?)
The origin URL. Should include only the protocol, hostname, and port —
no path, query string, or fragment.
Configuration object. All fields are optional.
Basic instantiation
Constructor options
Timeout options
Milliseconds to wait for the complete HTTP headers while not sending. Set
to
0 to disable.Milliseconds of inactivity between body data chunks. Resets each time data
is received. Set to
0 to disable.Keep-alive options
Milliseconds before an idle socket is closed. May be overridden by
server
keep-alive hints.Maximum milliseconds for keep-alive, even if the server hints a higher
value.
Milliseconds subtracted from server keep-alive hints to account for
transport latency.
Pipelining and concurrency
Number of concurrent requests to send over the single connection per
RFC 7230. Set to
0
to disable keep-alive connections entirely.Size limits
Maximum request header size in bytes.
Maximum response body size in bytes. Set to
-1 to disable.Connection options
TLS/socket configuration. Pass an options object to configure TLS, or a
custom connector function for full control over the socket.All Node.js TLS options are also accepted.
Throw an error when the
content-length header does not match the actual
body length.Address selection
Enable Happy Eyeballs (RFC 8305 §5) to auto-select between IPv4 and IPv6.
Milliseconds to wait for each address family attempt when
autoSelectFamily
is enabled.HTTP/2 options
Enable HTTP/2 via ALPN negotiation over TLS. The server must prefer HTTP/2
over HTTP/1.1.
Maximum concurrent streams per HTTP/2 session. Can be overridden by the
server’s
SETTINGS frame.HTTP/2 stream-level flow-control window size in bytes
(
SETTINGS_INITIAL_WINDOW_SIZE). Higher than Node.js defaults for better
throughput on high-bandwidth networks.HTTP/2 connection-level flow-control window size in bytes.
Milliseconds between HTTP/2 PING frames. Set to
0 to disable. Emits a
ping event on the client with the round-trip duration.HTTP/2 (
allowH2) requires a TLS connection. The server must negotiate
HTTP/2 via ALPN. Pseudo-headers (:path, :method, :scheme,
:authority) are set automatically and cannot be overridden.WebSocket options
Properties
true after client.close() has been called.true after client.destroy() has been called or client.close() has
fully completed.Read/write. The current pipelining factor. Changing this takes effect for
future requests.
Live statistics for this client.
Events
Emitted when a socket connects. Parameters:
origin: URL, targets: Dispatcher[].Emitted when the socket disconnects. The client reconnects automatically
when
client.size > 0. Parameters: origin: URL, targets: Dispatcher[], error: Error.Emitted when the request pipeline is no longer busy. Parameters:
origin: URL.Emitted for user-level errors such as exceptions thrown in
onResponseError.Code examples
- ESM
- CJS
Basic request
Pipelining — multiple concurrent requests on one connection
Custom TLS connector
H2CClient
H2CClient is a specialised Client variant that enforces HTTP/2 cleartext (h2c) over plain TCP — no TLS required. It accepts the same options as Client except those specific to TLS negotiation.
H2CClient basic usage
H2CClient supports the same maxConcurrentStreams, pingInterval, headersTimeout, bodyTimeout, keepAliveTimeout, keepAliveMaxTimeout, pipelining, strictContentLength, and connect options as Client.