01fetch with undici
npm install undici, then pass a ProxyAgent as the dispatcher. Use the fetch exported by undici so the two always match.
import { ProxyAgent, fetch } from "undici";
const proxy = new ProxyAgent("http://USERNAME:PASSWORD@gw.fullstackproxy.com:47631");
const res = await fetch("https://api.ipify.org?format=json", { dispatcher: proxy });
console.log(await res.json());One ProxyAgent keeps connections open and reuses them. That makes it fast, and it also means requests on a reused connection share an IP. For a new IP on each request, give the username a different session name each time, or create a fresh agent.
02Hold one IP
import { ProxyAgent, fetch } from "undici";
const user = "USERNAME-country-us-session-cart7-lifetime-30";
const proxy = new ProxyAgent("http://" + user + ":PASSWORD@gw.fullstackproxy.com:47631");
const a = await fetch("https://api.ipify.org", { dispatcher: proxy });
const b = await fetch("https://api.ipify.org", { dispatcher: proxy });
console.log(await a.text(), await b.text()); // the same address twice03axios
axios has a proxy option, but it handles https:// sites poorly. Turn it off and give axios an agent instead: npm install axios https-proxy-agent.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USERNAME-country-gb:PASSWORD@gw.fullstackproxy.com:47631");
const { data } = await axios.get("https://api.ipify.org?format=json", {
httpAgent: agent,
httpsAgent: agent,
proxy: false,
timeout: 30000,
});
console.log(data);04SOCKS5
npm install socks-proxy-agent and use socks5h:// so the proxy resolves the hostname:
import { SocksProxyAgent } from "socks-proxy-agent";
import https from "node:https";
const agent = new SocksProxyAgent("socks5h://USERNAME:PASSWORD@gw.fullstackproxy.com:47631");
https.get("https://api.ipify.org", { agent }, (res) => res.pipe(process.stdout));