Originally published on proxymint.

Send Node's built-in fetch through a proxy: undici ProxyAgent, NODE_USE_ENV_PROXY, encoded auth, SOCKS5, rotation and the errors each mistake throws.

TL;DR

  • Pass a ProxyAgent as the dispatcher. fetch(url, { dispatcher: new ProxyAgent("http://USERNAME:PASSWORD@HOST:PORT") }) routes HTTP and HTTPS through the proxy. Import fetch and ProxyAgent from the same undici package.
  • Built-in fetch ignores HTTP_PROXY by default. Node reads the proxy variables only with NODE_USE_ENV_PROXY=1 or --use-env-proxy, available from Node 24.0.0 and 22.21.0.
  • The undici versions must match. An npm undici 8 ProxyAgent passed to the built-in fetch on Node 22 or 24 fails with invalid onRequestStart method; on Node 22, importing undici 8 also switches the environment proxy off without an error.
  • Encode credentials or pass a token. A raw / or # in the password makes the ProxyAgent constructor throw invalid url; encodeURIComponent each part or use the token option.
  • Use undici 7.28.0 or 8.2.0 and later for SOCKS5. Socks5ProxyAgent arrived in 7.23.0, but releases before the fix can send a request to the wrong origin (CVE-2026-6734). It takes socks5:// URLs, sends host names to the proxy, and is marked experimental.

Key takeaways

  • Check process.versions.undici before copying ProxyAgent code. Keep the agent and fetch from one implementation, call install(), or pin the undici major your Node bundles.
  • Walk err.cause to read proxy failures. fetch wraps them in TypeError: fetch failed, and a refused tunnel shows its 407 two causes deep.
  • Scrub proxy URLs from logs. Node's ERR_PROXY_TUNNEL message prints the configured proxy URL with the password.
  • Test rotation with reused and fresh agents. An agent reuses tunnels per origin, and each concurrent HTTPS request opens its own CONNECT.
  • Set a timeout on every proxied call. Fetch has no overall timeout; pass AbortSignal.timeout() and retry 429, 5xx and timeouts, never 407 or 403.

Read the full guide on proxymint, with the commands, tables and sources.