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
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 undicifor fetch, ornpm install axios(addsocks-proxy-agentfor 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
| Product | Line in your dashboard | Notes |
|---|---|---|
| Static ISP proxies | IP:PORT:USER:PASS | One line per dedicated IP. With IP whitelisting turned on, the line is just IP:PORT and no password is sent. |
| Rotating residential and mobile | HOST:PORT:USER:PASS | Generated 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
Install the client
Runnpm install undici(and/oraxios). Importfetchfromundicitoo, so the agent and fetch come from the same version. - 2
Create the proxy agent
Passhttp://USERNAME:PASSWORD@HOST:PORTtonew ProxyAgent(...). Credentials in the URL are sent asProxy-Authorization. - 3
Send the request
Callfetch(url, { dispatcher }), or set it globally withsetGlobalDispatcher(dispatcher). - 4
Check the exit IP
Fetchhttps://api.ipify.organd log the text.
fetch with undici's ProxyAgent
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());export HTTPS_PROXY="http://USERNAME:PASSWORD@HOST:PORT"
export NO_PROXY="localhost,127.0.0.1"
NODE_USE_ENV_PROXY=1 node app.mjsWith 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
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);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.
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
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
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.
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.
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
| Problem | Likely cause | Fix |
|---|---|---|
| 407 Proxy Authentication Required | Credentials missing from the ProxyAgent URL, or special characters not encoded. | Wrap username and password in encodeURIComponent when building the URL. |
| Proxy ignored by global fetch | An 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 axios | Old 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_TIMEOUT | Wrong host or port, or a blocked outbound port. | Test the line with curl -x, then check firewall rules. |
| DNS leaks over SOCKS5 | socks5:// 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.