Integrations

Node.js proxies with fetch, axios and SOCKS agents

Use proxies in Node.js: fetch with undici's ProxyAgent, axios with https-proxy-agent and SOCKS5 with socks-proxy-agent, with working code and authentication.

3 min read Updated

On this page

Node.js has several HTTP clients, and each one takes a proxy differently. This guide covers the three setups people use most: fetch with undici, axios with a proxy agent, and SOCKS5 through socks-proxy-agent. The examples use ES modules and top-level await, so save them as .mjs files or set "type": "module" in package.json. Use a current Node.js LTS release.

fetch with undici's ProxyAgent

Node's built-in fetch is powered by undici, and undici's ProxyAgent adds proxy support:

npm install undici
import { fetch, ProxyAgent } from 'undici';

const dispatcher = new ProxyAgent('http://USERNAME:PASSWORD@HOST:PORT');

const res = await fetch('https://api.ipify.org?format=json', { dispatcher });
console.log(await res.json());

await dispatcher.close();

ProxyAgent reads the username and password from the URL and sends them to the proxy. If you'd rather keep them out of the URL, pass a token instead:

const dispatcher = new ProxyAgent({
  uri: 'http://HOST:PORT',
  token: `Basic ${Buffer.from('USERNAME:PASSWORD').toString('base64')}`,
});

Import fetch from the same undici package as ProxyAgent. Node's global fetch uses its own bundled copy of undici, which may be a different version from the one you installed. To send all undici requests through the proxy, call setGlobalDispatcher(dispatcher) once at startup.

Without extra packages

Recent Node.js versions can take proxy settings from the environment. Built-in fetch supports this from Node.js 22.21 and 24.0, and the http and https modules from 22.21 and 24.5:

NODE_USE_ENV_PROXY=1 HTTPS_PROXY="http://USERNAME:PASSWORD@HOST:PORT" node app.mjs

This applies one proxy to the whole process. That's fine for scripts, and less suited to apps that need different proxies for different tasks.

axios with https-proxy-agent

npm install axios https-proxy-agent http-proxy-agent
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';

const proxyUrl = 'http://USERNAME:PASSWORD@HOST:PORT';

const client = axios.create({
  httpsAgent: new HttpsProxyAgent(proxyUrl), // for https:// websites
  httpAgent: new HttpProxyAgent(proxyUrl),   // for http:// websites
  proxy: false, // turn off axios's own proxy handling so the agents are used
  timeout: 30000,
});

const { data } = await client.get('https://api.ipify.org?format=json');
console.log(data);

axios also has a built-in proxy option, but the way it handles HTTPS websites has changed between versions. Agents with proxy: false are the long-standing setup that behaves the same across axios releases. The same agents also work with Node's http and https modules and with other clients that accept an agent option.

SOCKS5 with socks-proxy-agent

npm install axios socks-proxy-agent
import axios from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';

const agent = new SocksProxyAgent('socks5h://USERNAME:PASSWORD@HOST:PORT');

const { data } = await axios.get('https://api.ipify.org?format=json', {
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false,
  timeout: 30000,
});
console.log(data);

Use the SOCKS5 port from your dashboard. With socks5h://, the proxy resolves domain names; with socks5://, socks-proxy-agent resolves them locally first.

The agent works with the built-in https module too:

import https from 'node:https';
import { SocksProxyAgent } from 'socks-proxy-agent';

const agent = new SocksProxyAgent('socks5h://USERNAME:PASSWORD@HOST:PORT');

https.get('https://api.ipify.org', { agent }, (res) => {
  res.setEncoding('utf8');
  res.on('data', (chunk) => process.stdout.write(chunk));
});

SOCKS5 and fetch

fetch doesn't accept Node agents, so socks-proxy-agent doesn't work with it. Recent undici releases (7.23 and later) include a Socks5ProxyAgent that works as a fetch dispatcher, but it is marked experimental. If you use it, keep undici up to date. For production code, axios or the https module with socks-proxy-agent is the safer choice today, or use the HTTP endpoint with ProxyAgent.

Rotation and connection reuse

undici keeps connections open and reuses them. HTTPS requests that reuse a connection share its tunnel, so several requests through one ProxyAgent can leave from the same IP, even on a rotating line. For a stable IP, use a sticky line from the generator. For several sticky sessions at once, create one agent per sticky line. Rotating vs sticky sessions explains both modes.

Common errors

Error What it means
undici: a RequestAbortedError mentioning Proxy response (407) The proxy rejected the username or password
axios: Request failed with status code 407 The proxy rejected the username or password
ECONNREFUSED or ETIMEDOUT Wrong host or port, or the port is blocked
EPROTO with wrong version number https:// in front of a plain proxy port
Socks5 Authentication failed Wrong SOCKS5 username or password

For browser automation, see the Puppeteer and Playwright guides. Troubleshooting covers the errors above in more detail.