undici ships three proxy dispatcher classes.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.
ProxyAgent routes requests through an HTTP or HTTPS proxy. Socks5ProxyAgent tunnels through a SOCKS5 proxy. EnvHttpProxyAgent reads proxy configuration from the process environment, making it a drop-in way to honour standard proxy environment variables without changing application code.
All three extend Dispatcher and can be used per-request via the dispatcher option, or globally via setGlobalDispatcher.
ProxyAgent
ProxyAgent routes requests through an HTTP/HTTPS proxy server. For secure targets (https:), it always establishes a CONNECT tunnel. For plain HTTP targets, it sends requests directly to the proxy with the full origin URL prepended to the path (unless proxyTunnel: true is set).
new ProxyAgent(options) or new ProxyAgent(url)
You can pass the proxy URI directly as a string/URL, or as an options object.
The proxy server URI (e.g.
http://proxy.example.com:8080). When passing
options as a plain string or URL, this is the entire first argument.Value for the
proxy-authorization header. Typically a Basic or Bearer
token string.Deprecated. Use
token instead.TLS options for the connection to the proxy server. Extends
ConnectOptions.TLS options for the connection to the target origin through the proxy tunnel.
Extends
ConnectOptions.When
true, establishes a CONNECT tunnel even for unencrypted
(HTTP) proxy/endpoint connections. When false (default), plain
HTTP requests are forwarded directly to the proxy without tunneling.body.clientFactory
(origin: URL, opts: object) => Dispatcher
default:"(origin, opts) => new Pool(origin, opts)"
Custom factory for the per-origin dispatcher used to send requests through
the proxy.
AgentOptions are accepted (except connect, which is replaced by proxyTls/requestTls).
When
connections is set to a non-zero value, ProxyAgent adds the
non-standard proxy-connection: keep-alive header to requests.Methods
ProxyAgent implements the full Dispatcher interface. The most commonly used methods are:
proxyAgent.request(options)— same signature asDispatcher.request.proxyAgent.close()— gracefully closes all underlying pools and clients.proxyAgent.dispatch(options, handler)— low-level dispatch.
Code examples
- Global dispatcher
- Per-request
- With fetch
Route all requests through a proxy
Proxy with Basic authentication
Custom TLS for proxy and target
Socks5ProxyAgent
Socks5ProxyAgent tunnels HTTP and HTTPS requests through a SOCKS5 proxy. DNS resolution happens on the proxy server (preventing DNS leaks), and HTTPS traffic remains encrypted end-to-end.
new Socks5ProxyAgent(proxyUrl, options?)
SOCKS5 proxy URL. Must use
socks5:// or socks:// scheme.
Credentials can be embedded in the URL
(socks5://user:pass@host:1080) or provided via username/password
options.SOCKS5 authentication username. Overrides credentials in the URL.
SOCKS5 authentication password. Overrides credentials in the URL.
Additional headers to include with proxy connections.
TLS options for the proxy connection itself (when connecting to a SOCKS5
server over TLS).
Custom connector function for the proxy connection.
PoolOptions are accepted and forwarded to the underlying pool.
Code examples
Basic SOCKS5 proxy
SOCKS5 with authentication
Connection pooling through a SOCKS5 tunnel
Supported SOCKS5 features
- Authentication: no-auth (
0x00) and username/password per RFC 1929 (0x02) - Address types: IPv4, domain name (recommended), IPv6
- Command: CONNECT only (UDP is not supported)
EnvHttpProxyAgent
EnvHttpProxyAgent automatically reads proxy configuration from environment variables, making it easy to respect system-wide proxy settings without modifying application code.
| Environment variable | Purpose |
|---|---|
HTTP_PROXY / http_proxy | Proxy for HTTP requests |
HTTPS_PROXY / https_proxy | Proxy for HTTPS requests |
NO_PROXY / no_proxy | Comma/space-separated hostnames to bypass |
http_proxy is set, it is used for both HTTP and HTTPS requests. Lowercase variables take precedence over uppercase when both are set. no_proxy supports leading wildcards (e.g. *.internal). Setting no_proxy=* bypasses the proxy for all requests.
new EnvHttpProxyAgent(options?)
Overrides
HTTP_PROXY/http_proxy from the environment.Overrides
HTTPS_PROXY/https_proxy from the environment.Overrides
NO_PROXY/no_proxy from the environment.AgentOptions are accepted.
Code examples
- Global dispatcher
- Per-request
- Override env vars
Honour system proxy settings globally