Quick answer
How do I use a rotating proxy in Node.js?
CrawlRelay works in Node.js 22+ through an undici ProxyAgent passed to fetch as dispatcher, pointing at http://gw.crawlrelay.com:7777. For axios or got, use a proxy agent and set proxy: false in axios. The country and an optional session of up to 30 minutes go in the username; traffic costs $0.50/GB from 50 GB.
In brief
- fetch: global fetch ignores HTTP_PROXY by default; pass an undici ProxyAgent as dispatcher.
- axios: proxy: false plus an HttpsProxyAgent, so HTTPS goes through a real CONNECT tunnel.
- got: one hpagent agent per protocol; keep-alive off for a new exit per request.
- SOCKS5: socks-proxy-agent with socks5h:// on port 7778.
- Cost: $250 for 500 GB, the same $0.50/GB rate as 50 GB.
What does the Node.js setup look like at a glance?
| fetch / undici | new ProxyAgent({ uri, token }), then fetch(url, { dispatcher }) |
|---|---|
| axios | httpsAgent: new HttpsProxyAgent(url) and proxy: false |
| got | agent: { https: new HttpsProxyAgent({ proxy }) } from hpagent |
| SOCKS5 | socks-proxy-agent with a socks5h:// URL on port 7778 |
| New IP per request | A new agent or a new session ID per request; pooled connections keep their exit |
| Module format | Examples are ES modules (.mjs or "type": "module") with top-level await |
How do I use a proxy with Node's fetch?
Install undici and pass a ProxyAgent as the dispatcher of each fetch call. Node's global fetch is built on undici but does not read HTTP_PROXY or HTTPS_PROXY by default. The token field sends the credentials as a Proxy-Authorization header, so no password ever appears in a URL that might be logged.
import { ProxyAgent, fetch } from "undici";
const credentials = Buffer.from("USERNAME-country-us:PASSWORD").toString("base64");
const dispatcher = new ProxyAgent({
uri: "http://gw.crawlrelay.com:7777",
token: `Basic ${credentials}`,
});
const res = await fetch("https://api.ipify.org?format=json", {
dispatcher,
signal: AbortSignal.timeout(30000),
});
console.log(res.status, await res.json());
await dispatcher.close();How do I use a proxy with axios?
Set proxy: false and give axios an HttpsProxyAgent, because its own proxy option does not open a CONNECT tunnel for HTTPS sites. Without a tunnel, many proxies reject the request. The agent opens a proper tunnel, and TLS then stays between your code and the website.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USERNAME-country-de:[email protected]:7777");
const client = axios.create({ httpsAgent: agent, httpAgent: agent, proxy: false, timeout: 30000 });
const { data } = await client.get("https://api.ipify.org?format=json");
console.log(data.ip);How do I use a proxy with got?
Give got one hpagent agent per protocol, with keep-alive off when each request should leave through a new exit. hpagent follows Node's http.Agent: keep-alive is off unless you pass keepAlive: true. Setting keepAlive: false explicitly, as below, documents the intent.
import got from "got";
import { HttpsProxyAgent } from "hpagent";
const proxy = "http://USERNAME-country-fr:[email protected]:7777";
const body = await got("https://api.ipify.org", {
agent: { https: new HttpsProxyAgent({ proxy, keepAlive: false }) },
timeout: { request: 30000 },
retry: { limit: 2, statusCodes: [429, 502, 503, 504] },
}).text();
console.log(body);How do I use SOCKS5 in Node.js?
Use socks-proxy-agent with a socks5h:// URL on port 7778, so hostnames resolve at the exit. It works with Node's http and https modules and with any library that accepts an agent.
import https from "node:https";
import { SocksProxyAgent } from "socks-proxy-agent";
const agent = new SocksProxyAgent("socks5h://USERNAME-country-gb:[email protected]:7778");
const req = https.get("https://api.ipify.org", { agent, timeout: 30000 }, (res) => {
let body = "";
res.on("data", (chunk) => (body += chunk));
res.on("end", () => console.log(res.statusCode, body));
});
req.on("timeout", () => req.destroy(new Error("timeout after 30 s")));
req.on("error", (err) => console.error(err.code, err.message));How do I limit concurrency per host without a library?
Wrap each request in a per-hostname slot counter, so no site gets more than a fixed number of requests at once. Promise.all over 10,000 URLs opens 10,000 requests at once. The limiter below, with no dependency, keeps each site at 4 requests in flight while the crawl as a whole runs in parallel, and works with any client above. The concurrency guide explains how to size that number.
import { ProxyAgent, fetch } from "undici";
const PER_HOST = 4;
const active = new Map();
const waiting = new Map();
const dispatcher = new ProxyAgent("http://USERNAME-country-us:[email protected]:7777");
async function withHostSlot(url, task) {
const host = new URL(url).hostname;
while ((active.get(host) ?? 0) >= PER_HOST) {
await new Promise((resolve) => {
if (!waiting.has(host)) waiting.set(host, []);
waiting.get(host).push(resolve);
});
}
active.set(host, (active.get(host) ?? 0) + 1);
try {
return await task();
} finally {
active.set(host, active.get(host) - 1);
waiting.get(host)?.shift()?.();
}
}
const urls = ["https://example.com/", "https://example.org/", "https://example.net/"];
const results = await Promise.all(
urls.map((url) =>
withHostSlot(url, async () => {
const res = await fetch(url, { dispatcher, signal: AbortSignal.timeout(30000) });
return [url, res.status, (await res.arrayBuffer()).byteLength];
}).catch((err) => [url, err.name, 0])
)
);
console.table(results);
await dispatcher.close();Why do I get the same IP twice, or ECONNRESET?
Pooled connections keep the exit they opened with, and a reused socket that the other side closed throws ECONNRESET. For a new IP on each request, create a new agent per request and close it afterwards, turn keep-alive off in the agent, or put a unique -session-<id> in each request's username.
ECONNRESET and socket hang up usually come from reusing a pooled socket that the other side has already closed, or from an exit that dropped. Retry GET requests once on a new connection. The [connection reset page](/docs/errors/connection-reset/ "How do I fix Connection reset by peer (ECONNRESET)?") covers the details.
How do I watch traffic and cost from a Node.js crawler?
Sum the byte length of every response body and divide by the page count: that is the figure to multiply by the price per GB. The limiter example already returns the size of each body. CrawlRelay counts upload and download, headers included, with 1 GB = 1,000,000,000 bytes, so the billed total runs slightly above the sum of bodies. The bandwidth guide shows typical page weights.
According to CrawlRelay's survey of 13 October 2026, the median residential price was $1.50/GB at 500 GB across 11 offers. CrawlRelay sells 500 GB for $250. The Statistics tab of the dashboard shows the traffic used per day and per sub-user, with a CSV export for a team's own reporting.
Node.js and proxies: questions
Does Node.js fetch read HTTP_PROXY for CrawlRelay?
Not by default: the global fetch ignores proxy environment variables unless it gets a dispatcher. Use an undici ProxyAgent pointing at CrawlRelay, or setGlobalDispatcher() if every request in the process should use it.
Why does axios fail with HTTPS sites behind CrawlRelay?
The built-in proxy option of axios sends HTTPS requests without a CONNECT tunnel. Set proxy: false and pass an HttpsProxyAgent for CrawlRelay as httpsAgent; TLS then runs end to end.
How do I send several countries from one Node.js process with CrawlRelay?
Create one agent per country, each with its own CrawlRelay username such as USERNAME-country-de, and pick the agent per request. Separate agents keep separate connection pools, and so separate exits.
Which timeout should a Node.js crawler use with CrawlRelay?
Thirty seconds suits HTML pages through CrawlRelay residential exits; give slow targets and mobile exits up to 60 seconds. Retry once on a new connection rather than waiting longer.
Verdict
- Use undici ProxyAgent with fetch for new Node.js code: no extra HTTP library, credentials kept out of URLs.
- Keep axios or got in existing code, with a proxy agent; never rely on the axios proxy option for HTTPS sites.
Sources (2)
- undici ProxyAgent documentation — Node.js undici (read 2026-11-13)
- axios request config: proxy and httpsAgent — axios (read 2026-11-13)
Connect your Node.js crawler
$5.94 for 10 GB to build, $250 for 500 GB at scale.