proxymix
Code examples

Proxy rotation in Node.js: fetch, undici and ProxyAgent

The built-in fetch in Node has no proxy option, so the example uses undici with a ProxyAgent. Each request goes through a random line from your list.

What you need

A current Node.js LTS release and the undici package: npm i undici. The built-in fetch in Node has no proxy option, so the example uses undici with a ProxyAgent. Save the file as rotate.mjs, because it uses ES modules and top-level await.

For a package, put your server address on the allowed list in the ProxyMix panel, and the code needs no logins. Residential lines carry a login, shown below.

The script

The script loads the list from your link, drops empty lines and sends three requests, each through a random line.

undici with an HTTP proxy
import { fetch, ProxyAgent } from 'undici';

const LIST_URL = 'YOUR_LIST_LINK';

const list = (await (await fetch(LIST_URL)).text()).split(/\s+/).filter(Boolean);
for (let i = 0; i < 3; i++) {
  const proxy = list[Math.floor(Math.random() * list.length)];
  const res = await fetch('https://ifconfig.me/ip', { dispatcher: new ProxyAgent(`http://${proxy}`) });
  console.log(proxy, '->', await res.text());
}

How it works

  • For HTTPS targets ProxyAgent sends CONNECT to the proxy, and traffic stays encrypted between your code and the site.
  • A new dispatcher in each fetch call switches the proxy per request.
  • The reply from ifconfig.me shows the address that sites see.

Residential lines with a login

A residential line needs its login on every request. The example builds a Proxy-Authorization token from the line and gives ProxyAgent the host and the port. The password may contain : and @, because the token is built from the raw values.

undici with a residential line
import { fetch, ProxyAgent } from 'undici';

const LINE = 'HOST:PORT:LOGIN:PASSWORD';

const [host, port, login, ...rest] = LINE.split(':');
const token = `Basic ${Buffer.from(`${login}:${rest.join(':')}`).toString('base64')}`;
for (let i = 0; i < 3; i++) {
  const dispatcher = new ProxyAgent({ uri: `http://${host}:${port}`, token });
  const res = await fetch('https://ifconfig.me/ip', { dispatcher });
  console.log(await res.text());
  await dispatcher.close();
}

The script closes the agent after each request, so a rotating line answers through a new address every time. One agent reused for many requests keeps its connection, and they share an IP. A sticky line keeps the address in both cases.

Concurrency and errors

Real scrapers send many requests at once. Keep the requests in flight below the package threads, for example with a small queue such as p-limit. Wrap each request in try and catch, and retry through another line after a timeout or a connection error.

Reload the list once per 8 to 10 minutes with setInterval and replace the array in memory. Requests already in flight keep their proxy, and new ones pick from the fresh list.

SOCKS5 in Node

undici speaks HTTP proxies. For SOCKS5 use a SOCKS agent package with the http and https modules or with axios. Every package line answers both protocols, so the list stays the same.

Errors you may see

  • ECONNREFUSED or a refused CONNECT on every line: your server address is not on the allowed list yet.
  • "Proxy response (407) !== 200 when HTTP Tunneling" in the error cause: the login or the password of a residential line is wrong.
  • UND_ERR_CONNECT_TIMEOUT: the line is slow or has left the pool, retry through another one.
  • A 403 or a captcha in the body: the target limits the address, so add lines and lower the pace.

Next steps

The Python guide and the curl guide show the same pattern in other languages, and the prices page lists the thread limits.

Questions

Questions

All questions
Can I use SOCKS5 in Node?

Yes, with a SOCKS agent package. HTTP proxies work with undici out of the box.

How many requests can run in parallel?

Up to the thread limit of your package. Use a queue such as p-limit to keep the count.

How do I use a residential line in Node?

Pass ProxyAgent the host and the port of the line and a token: Basic plus the base64 of login:password.

Ready to try it?

Top up your balance, create a list with the type and country you need, and test the pool on your site. A trial gigabyte costs from $0.27 and traffic lasts three months.

Build an order