← Docs / Proxies
Datacenter proxies with Node.js
Node's built-in fetch does not read proxy settings, so you hand it a proxy agent. This page uses undici's ProxyAgent and the datacenter tier: a US datacenter IP that changes for every new connection.
What you need
- Your proxy API key, from the Proxies page in the portal.
- Node 18 or later, and
npm install undici.
export PB_PROXY_KEY="YOUR_KEY"
Send a request through the proxy
import { ProxyAgent, fetch } from "undici";
const token =
"Basic " + Buffer.from(`${process.env.PB_PROXY_KEY}:datacenter`).toString("base64");
const agent = new ProxyAgent({ uri: "http://proxy.privatebyte.com:3128", token });
const res = await fetch("https://api.ipify.org", { dispatcher: agent });
console.log(await res.text()); // a US datacenter address
await agent.close();
The token is the proxy login: your API key and the tier, base64 encoded.
One IP, or a new one each time
An agent keeps its connections open. Every request through the same agent leaves from the same address. Create a new agent to get a new one:
for (let i = 0; i < 3; i++) {
const agent = new ProxyAgent({ uri: "http://proxy.privatebyte.com:3128", token });
console.log(await (await fetch("https://api.ipify.org", { dispatcher: agent })).text());
await agent.close();
}
Reuse one agent when a site expects a steady address, or when you want a cheaper bill: a new HTTPS connection costs about 4 KB of handshake. See how usage is counted.
Errors
| What you see | What it means |
|---|---|
407 Proxy Authentication Required |
The API key is wrong or missing. |
400 with Unknown tier "datacentre" |
The tier word is a close misspelling of a real tier. Fix the spelling. A password that is not close to any tier, such as x, uses your account's default tier. |
400 Invalid proxy password settings |
The password asked for something this tier does not do: a country other than US, a session or a TTL. |
A site returns 403 |
The site is refusing datacenter addresses. Try a residential tier such as standard. |
Related
Related
Still stuck? Open a ticket — tell us what was missing and the article gets fixed.