How to Use Proxies in Node.js (fetch and axios)
Node.js 18 and newer ship fetch through undici. A ProxyAgent sends every request through the gateway with your username and password.
Steps
- Install undici with npm install undici
- Create a ProxyAgent with the gateway URL and your login
- Pass it as dispatcher to fetch
- Add a country code or a session id to the username when needed
Example code
import { ProxyAgent, fetch } from "undici";
const agent = new ProxyAgent({
uri: "http://gw.gbproxies.cc:10000",
token: "Basic " + Buffer.from("USERNAME-cc-de:PASSWORD").toString("base64"),
});
const r = await fetch("https://api.ipify.org?format=json", { dispatcher: agent });
console.log(await r.json());Replace USERNAME and PASSWORD with the proxy login from the Proxies page of the console. Port 10000 is residential HTTP; see the ports of every product below.
Tips
- With axios, set proxy: { protocol: "http", host, port, auth: { username, password } }
- Create one agent per session id when you need several sticky IPs at once
Common questions
Does it work with HTTPS sites?
Yes. The agent opens a CONNECT tunnel to the target, so HTTPS stays end to end encrypted.