Use this file to discover all available pages before exploring further.
Undici provides first-class support for proxying HTTP and HTTPS traffic through several agent classes. Whether your environment requires an HTTP/HTTPS forward proxy, a SOCKS5 proxy, or you simply want to pick up standard proxy environment variables automatically, undici has a dedicated class for each approach. All proxy agents implement the Dispatcher interface, so they work seamlessly as global dispatchers or as per-request dispatcher options.
ProxyAgent is the primary class for routing requests through an HTTP or HTTPS proxy server. Pass the proxy URL as a string, URL object, or options object.
1
Install and import
import { ProxyAgent, request, setGlobalDispatcher } from 'undici'
2
Create a ProxyAgent
// Simplest form — pass the proxy URL as a stringconst proxyAgent = new ProxyAgent('http://my.proxy.server:8080')// Or as a URL objectconst proxyAgent = new ProxyAgent(new URL('http://my.proxy.server:8080'))// Or with explicit optionsconst proxyAgent = new ProxyAgent({ uri: 'http://my.proxy.server:8080' })
Supply credentials via the token option. Use a Basic token (Base64-encoded user:pass) or a Bearer token.
Authenticated proxy
import { setGlobalDispatcher, request, ProxyAgent } from 'undici'const proxyAgent = new ProxyAgent({ uri: 'http://my.proxy.server:8080', token: `Basic ${Buffer.from('username:password').toString('base64')}`})setGlobalDispatcher(proxyAgent)const { statusCode, body } = await request('http://localhost:3000/foo')console.log('response received', statusCode) // response received 200
Do not pass proxy-authorization in request headers. Undici throws an InvalidArgumentError if you do — always provide credentials through the ProxyAgent constructor instead.
The SOCKS5 agent resolves domain names on the proxy server, which avoids DNS leaks. HTTPS traffic remains encrypted end-to-end — the SOCKS5 proxy only handles the TCP tunnel.
EnvHttpProxyAgent automatically reads standard proxy environment variables and applies them without requiring any manual configuration in code.
Environment variable
Purpose
HTTP_PROXY / http_proxy
Proxy for plain HTTP requests
HTTPS_PROXY / https_proxy
Proxy for HTTPS requests
NO_PROXY / no_proxy
Comma-separated list of hosts to bypass
Automatic environment proxy
import { EnvHttpProxyAgent, setGlobalDispatcher, request } from 'undici'const envHttpProxyAgent = new EnvHttpProxyAgent()setGlobalDispatcher(envHttpProxyAgent)const { statusCode, body } = await request('http://localhost:3000/foo')console.log('response received', statusCode) // response received 200
Lowercase environment variables take precedence over their uppercase equivalents. If both http_proxy and HTTP_PROXY are set, the lowercase value is used.