Skip to content
Node.js fetch & axios

How to use proxies in Node.js with fetch and axios

In Node.js, import fetch and ProxyAgent from undici and pass dispatcher: new ProxyAgent("http://USERNAME:PASSWORD@HOST:PORT") to fetch. With axios 1.16.1 or later, set the proxy option with host, port and auth; for SOCKS5, use socks-proxy-agent.

What you need

Node's global fetch has no proxy option, and axios only tunnels HTTPS through a proxy correctly in recent versions. This guide shows the setups that work today, with HTTP(S) and SOCKS5.
  • An active CleanProxies plan: static ISP proxies for fixed US IPs, or rotating residential / mobile proxies for pools. The trial works too.
  • Your credential line from the dashboard's proxy manager: host (or IP), port, username and password.
  • Node.js 20 or later (22.21+ or 24+ for the built-in environment-variable proxy support).
  • npm install undici for fetch, or npm install axios (add socks-proxy-agent for SOCKS5).

New to proxies? For the overview, read Using proxies with cURL, Python and Node.js and SOCKS5 vs HTTP proxies. This guide goes deeper on Node.js only.

Your CleanProxies credentials

Every CleanProxies product works over HTTP(S) and SOCKS5. This is what the dashboard gives you.
ProductLine in your dashboardNotes
Static ISP proxiesIP:PORT:USER:PASSOne line per dedicated IP. With IP whitelisting turned on, the line is just IP:PORT and no password is sent.
Rotating residential and mobileHOST:PORT:USER:PASSGenerated in the proxy manager. The username carries the location and session you pick, so generate a new list to change them.

Most tools want the same details as a URL: http://USERNAME:PASSWORD@HOST:PORT for HTTP(S) or socks5://USERNAME:PASSWORD@HOST:PORT for SOCKS5. The examples on this page use those placeholders; copy the host, port, username and password from your CleanProxies dashboard. To convert a whole list at once, paste it into the proxy list formatter.

How to set up CleanProxies in Node.js

  1. 1

    Install the client

    Run npm install undici (and/or axios). Import fetch from undici too, so the agent and fetch come from the same version.
  2. 2

    Create the proxy agent

    Pass http://USERNAME:PASSWORD@HOST:PORT to new ProxyAgent(...). Credentials in the URL are sent as Proxy-Authorization.
  3. 3

    Send the request

    Call fetch(url, { dispatcher }), or set it globally with setGlobalDispatcher(dispatcher).
  4. 4

    Check the exit IP

    Fetch https://api.ipify.org and log the text.

fetch with undici's ProxyAgent

proxy-fetch.mjsJavaScript
import { fetch, ProxyAgent } from "undici";

const dispatcher = new ProxyAgent("http://USERNAME:PASSWORD@HOST:PORT");

const res = await fetch("https://api.ipify.org", { dispatcher });
console.log("Exit IP:", await res.text());
Built-in proxy from environment variables (Node 24+ or 22.21+)shell
export HTTPS_PROXY="http://USERNAME:PASSWORD@HOST:PORT"
export NO_PROXY="localhost,127.0.0.1"
NODE_USE_ENV_PROXY=1 node app.mjs

With NODE_USE_ENV_PROXY=1 (or the --use-env-proxy flag on newer releases), Node's own http, https and global fetch read HTTP_PROXY, HTTPS_PROXY and NO_PROXY. Node marks the feature as under active development, so the explicit agent is the safer choice for production.

axios proxy configuration

axios 1.16.1 or laterJavaScript
import axios from "axios";

const { data } = await axios.get("https://api.ipify.org", {
  proxy: {
    protocol: "http",
    host: "HOST",
    port: PORT,
    auth: { username: "USERNAME", password: "PASSWORD" },
  },
  timeout: 30_000,
});
console.log(data);
Older axios: https-proxy-agentJavaScript
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";

const agent = new HttpsProxyAgent("http://USERNAME:PASSWORD@HOST:PORT");
const { data } = await axios.get("https://api.ipify.org", { httpsAgent: agent, proxy: false });

Before 1.16.1, axios could send https:// requests to an HTTP proxy without a proper CONNECT tunnel. Upgrade, or use an agent with proxy: false as shown.

SOCKS5 with socks-proxy-agent

axios has no SOCKS support of its own; hand it an agent. Use socks5h:// so the proxy resolves hostnames.

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

const agent = new SocksProxyAgent("socks5h://USERNAME:PASSWORD@HOST:PORT");
const { data } = await axios.get("https://api.ipify.org", {
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false,
});
console.log(data);

Recent undici releases also accept socks5:// URLs in ProxyAgent through an experimental SOCKS5 agent; it prints an ExperimentalWarning, so prefer HTTP(S) with fetch in production.

Rotate across ISP proxies

One agent per ISP lineJavaScript
import { readFileSync } from "node:fs";
import { fetch, ProxyAgent } from "undici";

const agents = readFileSync("proxies.txt", "utf8")
  .split("\n")
  .filter(Boolean)
  .map((line) => {
    const [host, port, user, ...rest] = line.trim().split(":");
    const pass = rest.join(":");
    return new ProxyAgent(`http://${encodeURIComponent(user)}:${encodeURIComponent(pass)}@${host}:${port}`);
  });

for (let i = 0; i < 10; i++) {
  const res = await fetch("https://api.ipify.org", { dispatcher: agents[i % agents.length] });
  console.log(await res.text());
}

Static ISP vs rotating residential in Node.js

Both products use the same setup. What changes is how long an IP stays with you.

Static ISP proxies

Each agent pools keep-alive connections to one dedicated US ISP IP. Keep one agent per line for the life of your process, with unlimited bandwidth and threads behind it.

Static ISP ProxiesAs low as $0.75/IP

Rotating residential proxies

A ProxyAgent reuses connections, so consecutive requests can share an exit IP even on a rotating line. Create a fresh agent (or close it) when you need a new IP; use a sticky line and one agent for multi-step flows.

Rotating Residential ProxiesAs low as $0.63/GB

Verify your exit IP

Run node proxy-fetch.mjs. It prints the exit IP, which should match your dashboard line (ISP) or a residential IP in the location you generated.

Open What is my IP? through the proxy to see the address and headers a website receives, or paste up to ten lines into the free proxy checker to test connectivity, latency and exit IP before you start.

Troubleshooting Node.js proxy errors

ProblemLikely causeFix
407 Proxy Authentication RequiredCredentials missing from the ProxyAgent URL, or special characters not encoded.Wrap username and password in encodeURIComponent when building the URL.
Proxy ignored by global fetchAn agent from npm undici passed to Node's built-in fetch (different versions).Import fetch from undici along with ProxyAgent.
ECONNRESET or EPROTO on HTTPS with axiosOld axios without a CONNECT tunnel, or an https proxy protocol.Upgrade axios, or use https-proxy-agent with proxy: false; keep the proxy protocol http.
UND_ERR_CONNECT_TIMEOUTWrong host or port, or a blocked outbound port.Test the line with curl -x, then check firewall rules.
DNS leaks over SOCKS5socks5:// resolves locally in socks-proxy-agent.Use socks5h://.

Still stuck? Our 24/7 support team answers fast: contact us with the error and the line you used (leave the password out).

Node.js proxy FAQs

Not through a fetch option. Use undici's ProxyAgent as the dispatcher, or, on Node 24+ and 22.21+, enable the built-in support with NODE_USE_ENV_PROXY=1 and the HTTP_PROXY/HTTPS_PROXY variables.

Get CleanProxies for Node.js

Super clean US ISP IPs from $0.75/IP with unlimited bandwidth, rotating residential from $0.63/GB and mobile from $0.79/GB. Instant delivery, 99.9% uptime and 24/7 support.