Back to blog
Automation6 min read

Proxy Setup in Node.js and fetch

Use https-proxy-agent and socks-proxy-agent with Node fetch — authentication, rotation, and production checklist.

Node.js 18+ includes native fetch, but proxy support requires explicit agents. Use https-proxy-agent for HTTP proxies and socks-proxy-agent for SOCKS5 — the same agents proxies.st uses internally for health checks.

fetch with HTTP proxy agents

Node fetch + HttpsProxyAgent
import fetch from "node-fetch"; // or global fetch in Node 18+
import { HttpsProxyAgent } from "https-proxy-agent";

const agent = new HttpsProxyAgent("http://203.0.113.10:8080");

const res = await fetch("https://api.example.com/v1/items", { agent });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();

For authenticated proxies, embed credentials in the URL: http://user:pass@host:port.

SOCKS5 in Node.js

SOCKS5 agent
import { SocksProxyAgent } from "socks-proxy-agent";

const agent = new SocksProxyAgent("socks5://203.0.113.20:1080");
const res = await fetch("https://example.com", { agent });

SOCKS5 handles arbitrary TCP — useful when one agent config must cover mixed protocols. Compare with HTTP vs SOCKS5.

Environment variables

Some CLI tools respect HTTP_PROXY and HTTPS_PROXY. Node fetch does not use them automatically — set agents explicitly in application code.

Pool rotation

Simple rotator
const pool = ["http://203.0.113.10:8080", "http://203.0.113.11:8080"];
let i = 0;

function nextAgent() {
  const url = pool[i++ % pool.length];
  return new HttpsProxyAgent(url);
}

Ingest pools via bulk list parsing or scheduled fetches from API feeds.

Production checklist

  • Set timeouts on every fetch — dead proxies hang by default.
  • Reuse agents per endpoint where possible to reduce connection overhead.
  • Monitor success rates and prune bad IPs — see health checks.
  • Apply rate limits and backoff at the application layer.

Need proxies at scale?

proxies.st offers health-checked HTTP and SOCKS pools with dashboard access, API keys, and plain-text bulk feeds for pipelines.

Related guides