Tutorials7 min readOct 11, 2026

Node.js proxy guide: undici fetch, axios and SOCKS5

Node's fetch has no proxy option. Use undici's ProxyAgent or axios with an HTTP or SOCKS5 agent, plus SOCKS5 for fetch, rotation and sticky sessions.

By crawlproxies
Node.js proxy guide: undici fetch, axios and SOCKS5

Node.js 18+ ships fetch, but it doesn't take a proxy setting. The fix is a dispatcher (for undici) or an agent (for axios and other HTTP clients). This guide shows both with authenticated crawlproxies credentials, over HTTP and SOCKS5, then covers rotation, sticky sessions and running many requests at once.

Get your username and password from the generator. The examples use the Residential Premium gateway prem.crawlproxies.com (HTTP port 3621; it has no SOCKS5 port).

Node versions: npm i undici installs undici 8, which needs Node 22.19 or newer. On Node 20 install undici@7, and on Node 18 undici@6. The current https-proxy-agent and socks-proxy-agent need Node 20; on Node 18 use https-proxy-agent@7 and socks-proxy-agent@8.

undici is the HTTP client that powers Node's own fetch. Install it and use its fetch together with a ProxyAgent:

bash
npm i undici
js
import { fetch, ProxyAgent } from "undici";

const agent = new ProxyAgent({
  uri: "http://prem.crawlproxies.com:3621",
  token: "Basic " + Buffer.from("USERNAME:PASSWORD").toString("base64"),
});

const res = await fetch("https://ipinfo.io/json", { dispatcher: agent });
console.log(res.status, await res.json());

The token becomes the Proxy-Authorization header. HTTPS sites are tunnelled through the proxy, so the connection to the website stays encrypted.

Tip: import fetch from undici rather than using the global one. The global fetch uses the copy of undici bundled with your Node version, and mixing it with a different installed version can fail in confusing ways.

To send every undici request through the proxy without passing dispatcher each time:

js
import { fetch, ProxyAgent, setGlobalDispatcher } from "undici";

setGlobalDispatcher(new ProxyAgent({
  uri: "http://prem.crawlproxies.com:3621",
  token: "Basic " + Buffer.from("USERNAME:PASSWORD").toString("base64"),
}));

const res = await fetch("https://ipinfo.io/ip");

Option 2: axios with a proxy agent

axios has a built-in proxy option, but for HTTPS sites an agent is more reliable. Install https-proxy-agent:

bash
npm i axios https-proxy-agent
js
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";

const agent = new HttpsProxyAgent("http://USERNAME:PASSWORD@prem.crawlproxies.com:3621");

const { data } = await axios.get("https://ipinfo.io/json", {
  httpsAgent: agent,
  proxy: false,          // turn off axios's own proxy handling so the agent is used
  timeout: 30_000,
});
console.log(data.ip, data.country);

For plain http:// sites, add http-proxy-agent and pass its HttpProxyAgent as httpAgent the same way.

Option 3: axios with a SOCKS5 proxy

axios can't speak SOCKS on its own: its proxy option is HTTP only. Give it a SOCKS agent from socks-proxy-agent instead. Residential Premium is HTTP only, so SOCKS5 needs a plan that has it, such as Residential Private (priv.crawlproxies.com, SOCKS5 port 11000), with that plan's own username and password.

bash
npm i axios socks-proxy-agent
js
import axios from "axios";
import { SocksProxyAgent } from "socks-proxy-agent";

const agent = new SocksProxyAgent("socks5h://USERNAME:PASSWORD@priv.crawlproxies.com:11000");

const client = axios.create({
  httpAgent: agent,      // http:// URLs
  httpsAgent: agent,     // https:// URLs
  proxy: false,          // switch off axios's HTTP proxy handling, including HTTP_PROXY env vars
  timeout: 30_000,
});

const { data } = await client.get("https://ipinfo.io/json");
console.log(data.ip, data.country);

Three details that trip people up:

  • Use socks5h, not socks5. With socks5h:// the proxy resolves hostnames, so DNS lookups don't come from your machine. With socks5:// your machine looks the name up first and only the IP goes through the proxy.
  • Set both agents. With only httpsAgent, http:// URLs bypass the proxy and go out from your own IP.
  • Encode special characters. A password with @, : or / breaks the URL. Build it with encodeURIComponent:
js
const user = "USERNAME";
const pass = "PASSWORD";
const agent = new SocksProxyAgent(`socks5h://${user}:${encodeURIComponent(pass)}@priv.crawlproxies.com:11000`);

Targeting and sticky sessions go in the username, but Residential Private writes them differently from the Premium examples further down. It takes a country and no city (USERNAME-country-us), and a sticky IP is -session-<id>-time-<seconds>, up to 2 hours: USERNAME-country-us-session-cart42-time-1800.

Option 4: fetch with a SOCKS5 proxy

fetch-socks turns a SOCKS5 proxy into an undici dispatcher, with the username and password as separate fields, so any password works as it is:

bash
npm i undici fetch-socks
js
import { fetch } from "undici";
import { socksDispatcher } from "fetch-socks";

const dispatcher = socksDispatcher({
  type: 5,
  host: "priv.crawlproxies.com",
  port: 11000,
  userId: "USERNAME",
  password: "PASSWORD",
});

const res = await fetch("https://ipinfo.io/json", {
  dispatcher,
  signal: AbortSignal.timeout(30_000),
});
console.log(await res.json());

As in Option 1, import fetch from undici: the global fetch can reject this dispatcher.

Recent undici (7.23 and later) can also take a SOCKS5 proxy directly, as new ProxyAgent("socks5://USERNAME:PASSWORD@priv.crawlproxies.com:11000"). That support is still marked experimental (Node prints a warning), and it sends the password exactly as written in the URI, so a password that needs percent-encoding fails to authenticate. fetch-socks avoids both.

Geo-targeting

Location is part of the username, so it's a string change:

js
const username = (opts = {}) => {
  let u = "USERNAME";
  if (opts.country) u += `-country-${opts.country}`;
  if (opts.country && opts.city) u += `-city-${opts.city}`;
  return u;
};

const agent = new ProxyAgent({
  uri: "http://prem.crawlproxies.com:3621",
  token: "Basic " + Buffer.from(`${username({ country: "gb", city: "london" })}:PASSWORD`).toString("base64"),
});

Country codes are two letters; cities are lowercase, with underscores for spaces (new_york). The geo-targeting guide lists the options.

Rotation and sticky sessions

Without a session in the username, every new connection gets a new IP. Agents and dispatchers keep connections open and reuse them, so a burst of requests through one agent can share an exit IP. When you need a guaranteed fresh IP, use a fresh agent and close it when you're done:

js
const TOKEN = "Basic " + Buffer.from("USERNAME:PASSWORD").toString("base64");

async function freshIp() {
  const agent = new ProxyAgent({ uri: "http://prem.crawlproxies.com:3621", token: TOKEN });
  try {
    const res = await fetch("https://ipinfo.io/ip", { dispatcher: agent });
    return (await res.text()).trim();
  } finally {
    await agent.close();
  }
}

For the opposite (the same IP across a login or checkout), add a session id and a duration in seconds to the username:

js
import { randomUUID } from "node:crypto";

const sessionUser = `USERNAME-country-us-ssid-${randomUUID().slice(0, 8)}-sst-1800`;

That is Residential Premium's form (up to 3600 seconds). On Residential Private the same thing is -session-<id>-time-1800.

Keep one session id per account or task. Sticky vs rotating sessions explains the trade-offs.

Many requests at once

Promise.all on hundreds of URLs opens hundreds of connections at once. A small pool keeps it under control:

js
async function mapLimit(items, limit, fn) {
  const results = new Array(items.length);
  let next = 0;
  const worker = async () => {
    while (next < items.length) {
      const i = next++;
      try { results[i] = await fn(items[i]); }
      catch (err) { results[i] = { error: err.message }; }
    }
  };
  await Promise.all(Array.from({ length: limit }, worker));
  return results;
}

const urls = Array.from({ length: 50 }, (_, i) => `https://example.com/page/${i + 1}`);
const results = await mapLimit(urls, 10, async (url) => {
  const res = await fetch(url, { dispatcher: agent, signal: AbortSignal.timeout(30_000) });
  return { url, status: res.status };
});

Start around 10 concurrent requests and raise it slowly while watching your error rate.

Errors you might see

ErrorUsually means
Proxy response (407) !== 200 when HTTP Tunneling (undici)Wrong username or password
Request failed with status code 407 (axios)The same: check the credentials and targeting syntax
Socks5 Authentication failed (socks-proxy-agent)Wrong username or password on a SOCKS5 port
ECONNREFUSEDWrong host or port
Proxy connection timed out (socks-proxy-agent)Often a SOCKS5 agent pointed at an HTTP-only port
Proxy connection ended before receiving CONNECT response (https-proxy-agent)An HTTP proxy agent pointed at a SOCKS5 port
Response does not match the HTTP/1.1 protocol (undici)An http:// proxy uri pointed at a SOCKS5 port
invalid onRequestStart methodA fetch-socks dispatcher passed to the global fetch: import fetch from undici
TimeoutError / ETIMEDOUTA slow exit IP: retry, and keep timeouts on every request
403 / 429 from the siteBlocking or rate limiting: see the debugging checklist

Driving a real browser instead? See Playwright and Puppeteer with authenticated proxies.

Written by
crawlproxies
Create account