How to use a proxy with axios in Node.js
axios has a proxy option, but it doesn't tunnel HTTPS properly. The reliable way to use a proxy with axios in Node.js is an agent from https-proxy-agent (or socks-proxy-agent) with proxy: false. This guide covers that setup, rotating and sticky IPs, country targeting, SOCKS5 and retries.
Updated 9 November 2026
Prerequisites
- Node.js 20 or newer.
npm i axios https-proxy-agent, andsocks-proxy-agentfor SOCKS5. Current major versions of the agent packages (https-proxy-agent 9, socks-proxy-agent 10) are ESM-only; in CommonJS, userequire()on Node 22.12+ (which can load ES modules) or a dynamicimport().- A proxy login from the dashboard (Logins), shown as
USERandPASS.
Typical uses: price and availability monitoring, checking localized API responses, and collecting public data.
Basic HTTP proxy
Create one HttpsProxyAgent with the proxy URL and give it to axios as httpsAgent. Set proxy: false. Otherwise axios applies its own proxy logic, including HTTP_PROXY and HTTPS_PROXY environment variables, on top of the agent.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USER-cc-us:PASS@gw.hyperproxy.io:8888");
const client = axios.create({
httpsAgent: agent,
httpAgent: agent, // also tunnels plain http:// targets with CONNECT
proxy: false, // turn off axios's own proxy handling and HTTP_PROXY env vars
timeout: 30_000,
});
const { data } = await client.get("https://api.ipify.org");
console.log(data); // the exit IPWhy not axios's proxy option?
For https:// URLs, axios's built-in proxy sends the request to the proxy as a plain absolute URL instead of opening a CONNECT tunnel. The gateway rejects that with 400, as described in Errors. The built-in option is fine for plain http:// targets only:
// axios's built-in option: fine for http:// targets only
await axios.get("http://example.com", {
proxy: {
protocol: "http",
host: "gw.hyperproxy.io",
port: 8888,
auth: { username: "USER-cc-us", password: "PASS" },
},
});Authentication
Put the credentials in the agent's proxy URL. The agent sends them as Proxy-Authorization on the CONNECT request, so they go to the proxy and never to the target. If the password contains @, : or /, wrap it in encodeURIComponent().
Rotating vs sticky IPs
The gateway gives every new connection a new exit. HttpsProxyAgent doesn't keep connections alive by default, so each axios request opens a new tunnel and gets a new IP. If you pass keepAlive: true to the agent, requests to the same host can share an exit.
For multi-step flows, add a session id to the username. Every request with that id uses the same exit for 5 minutes by default, up to 120 with ttl. A small factory function makes one axios instance per session.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
// keepAlive is off by default, so every request opens a new tunnel: new exit each time
const agent = new HttpsProxyAgent("http://USER-cc-us:PASS@gw.hyperproxy.io:8888");
for (let i = 0; i < 3; i++) {
const { data } = await axios.get("https://api.ipify.org", { httpsAgent: agent, proxy: false });
console.log(data);
}Country and city targeting
Options go in the username. All of them are listed in Targeting.
| Username | Exit |
|---|---|
USER-cc-us | United States |
USER-cc-us-city-new_york | New York |
USER-cc-us-region-california | California |
USER-cc-de-asn-3320 | One German network |
USER-cont-eu | Anywhere in Europe |
USER-cc-us-city-houston-strict-false | Houston, falling back to the US |
SOCKS5
The same port accepts SOCKS5. Use socks-proxy-agent with the socks5h:// scheme so hostnames are resolved by the proxy rather than locally, and set it as both httpAgent and httpsAgent.
// npm i socks-proxy-agent
import axios from "axios";
import { SocksProxyAgent } from "socks-proxy-agent";
const agent = new SocksProxyAgent("socks5h://USER-cc-de:PASS@gw.hyperproxy.io:8888");
const { data } = await axios.get("https://api.ipify.org", {
httpAgent: agent,
httpsAgent: agent,
proxy: false,
});
console.log(data);Concurrency
Run a fixed number of requests in flight rather than firing thousands of promises at once. A small worker pool is enough; libraries such as p-limit do the same. Stay within the target site's rate limits.
const urls = Array.from({ length: 50 }, (_, i) => `https://example.com/page/${i}`);
const client = axios.create({ httpsAgent: agent, proxy: false, timeout: 30_000 });
async function worker() {
for (let url = urls.shift(); url; url = urls.shift()) {
const res = await getWithRetry(client, url);
console.log(url, res.status);
}
}
await Promise.all(Array.from({ length: 8 }, worker)); // 8 requests in flightError handling and retries
When the gateway refuses a CONNECT, https-proxy-agent passes the proxy's response on to Node's HTTP parser, so axios rejects with err.response.status set to the gateway status. Map it with Errors:
- 407: wrong login, an invalid or repeated option, or no balance. Don't retry.
- 404: no exit matches the targeting; widen it or add
strict-false. - 403: the target is not allowed. 400: usually axios's built-in proxy used with an HTTPS URL.
- 429: back off. 502 / 504: retry; without a session each retry gets a new exit.
const RETRYABLE = new Set([429, 502, 504]);
async function getWithRetry(client, url, attempts = 4) {
for (let i = 0; ; i++) {
try {
return await client.get(url);
} catch (err) {
const status = err.response?.status; // includes the gateway's CONNECT status
if (status === 407) throw new Error("Proxy auth failed: check login, options and balance");
if (status === 404 || status === 403 || status === 400) throw err; // not retryable
const network = !err.response; // timeouts, resets
if (i + 1 >= attempts || !(network || RETRYABLE.has(status))) throw err;
await new Promise((r) => setTimeout(r, 500 * 2 ** i)); // new exit unless sticky
}
}
}A target site can return the same status codes itself. If you need to know whether a 403 came from the gateway or the site, listen for the agent's proxyConnect event, which reports the CONNECT status.
Verify the exit IP
Request https://api.ipify.org (or ?format=json) through the client. The rotating example prints three different addresses and the sticky example prints the same one three times.
Troubleshooting
- 400 Bad Request on HTTPS URLs: you used axios's
proxyoption. Switch to the agent andproxy: false. - Requests skip the proxy: you set
httpsAgentbut called anhttp://URL; sethttpAgenttoo. - ERR_REQUIRE_ESM: the agent packages are ES modules; use
importor a newer Node.js. - Works in Node, not in the browser: agents only exist in Node.js. Browsers use the system proxy.
FAQ
Does https-proxy-agent work for http:// URLs?
Yes. It tunnels them with CONNECT as well. You can also use http-proxy-agent for plain HTTP if you prefer forwarding.
Can I use fetch instead of axios?
Yes. See the Node.js fetch and undici guide.
How do I change country per request?
Create one agent per username (country, city or session) and pass it on the request config. Agents are cheap; reuse them per target instead of creating one per request.
Where are other languages?
See Examples, or the Python requests and curl guides.
Try it on your own workload
Sign up with GitHub or Google, create a proxy login and send your first request in a minute.
More proxy integration guides
- Python requests — Rotating and sticky residential proxies with Python requests, including SOCKS5 and retries.
- Python httpx — Sync and async httpx through a residential proxy with the proxy= argument and SOCKS5.
- Python aiohttp — High-concurrency aiohttp requests through rotating or sticky residential IPs.
- Scrapy — Route Scrapy spiders through residential proxies with request.meta and a middleware.
- Playwright — Run Playwright through residential proxies, per browser or per context, with sticky sessions.
- Puppeteer — Launch Puppeteer behind an authenticated residential proxy with page.authenticate.
- Crawlee — Map Crawlee sessions to sticky residential IPs and target countries per request.
- Node.js fetch & undici — Use Node's built-in fetch with undici ProxyAgent or NODE_USE_ENV_PROXY.
- Go net/http — Configure Go's http.Transport for HTTP and SOCKS5 residential proxies.
- curl & SOCKS5 — Use curl with HTTP and SOCKS5 proxies, and test targeting from the command line.