Node.js Axios Proxy: How to Use a Proxy with Axiosa

To use a proxy with axios in Node.js, the most reliable method is a proxy agent: create one with the https-proxy-agent package, pass it as httpsAgent, and set proxy: false. Axios does have its own proxy option, but it is a common source of confusing errors with HTTPS websites. This guide shows both, explains the difference, and covers authentication, SOCKS5, environment variables and the errors you are likely to meet.

New to proxies? See what a proxy server is first.

Install the two packages:

npm install axios https-proxy-agent

Then:

import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";

const agent = new HttpsProxyAgent("http://user:pass@proxy-host:port");

const res = await axios.get("https://api.ipify.org", {
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false,      // stop axios applying its own proxy logic on top
  timeout: 30000,
});

console.log(res.data);   // should print the proxy's address

Replace the address with the details from your provider. If your provider uses an IP allow-list, leave out user:pass@.

Why proxy: false? Without it, axios may also read proxy environment variables and apply its own proxy handling alongside your agent, which leads to double-proxying or confusing failures. Setting it to false leaves the agent fully in charge.

Axios’s built-in proxy option

Axios accepts a proxy object directly:

const res = await axios.get("http://example.com/", {
  proxy: {
    protocol: "http",
    host: "proxy-host",
    port: 8080,
    auth: { username: "user", password: "pass" },
  },
});

This works well for plain http:// websites. For https:// websites, behaviour has varied between axios versions and setups, and it is a frequent cause of errors such as EPROTO, “wrong version number” or certificate mismatches. The agent approach above uses a standard tunnel (the CONNECT method) for HTTPS, which is what proxies expect. Since almost every site is HTTPS today, use the agent.

Setting the proxy once with an instance

If your project makes many requests, create an axios instance so every call uses the proxy:

const client = axios.create({
  httpAgent: agent,
  httpsAgent: agent,
  proxy: false,
  timeout: 30000,
});

const page = await client.get("https://example.com/");

For several static addresses, create one agent per address and pick one per request:

const agents = [
  new HttpsProxyAgent("http://user:pass@static-1:port"),
  new HttpsProxyAgent("http://user:pass@static-2:port"),
];
let i = 0;
const next = () => agents[i++ % agents.length];

const r = await axios.get(url, { httpsAgent: next(), proxy: false, timeout: 30000 });

With a rotating gateway you need only one agent — the provider switches addresses. See rotating vs static proxy.

Username, password and special characters

If your password contains @, :, # or %, encode it before building the address:

const user = encodeURIComponent(process.env.PROXY_USER);
const pass = encodeURIComponent(process.env.PROXY_PASS);
const agent = new HttpsProxyAgent(`http://${user}:${pass}@proxy-host:port`);

Reading credentials from environment variables, as here, keeps them out of your code and your Git history. For a comparison of password login and IP allow-lists, see proxy authentication methods.

Environment variables

In Node.js, axios also picks up the standard proxy variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY when you have not set a proxy yourself:

# macOS / Linux
export HTTPS_PROXY="http://user:pass@proxy-host:port"
node app.js

# Windows PowerShell
$env:HTTPS_PROXY = "http://user:pass@proxy-host:port"
node app.js

Convenient — but it also means a leftover variable can route traffic you did not intend to proxy. If your code sets its own agent, keep proxy: false so the variables cannot interfere. Node.js does not read the Windows or Mac system proxy panel; see Windows proxy settings for why.

SOCKS5 proxies

For SOCKS5, swap the agent package:

npm install socks-proxy-agent
import { SocksProxyAgent } from "socks-proxy-agent";

const agent = new SocksProxyAgent("socks5h://user:pass@proxy-host:port");
const res = await axios.get("https://api.ipify.org", {
  httpAgent: agent, httpsAgent: agent, proxy: false, timeout: 30000,
});

Use socks5h so the proxy, not your machine, looks up website addresses — important when the location matters. See HTTP vs SOCKS5 proxy.

If you use fetch instead of axios

Node’s built-in fetch does not use agents. It is built on a library called undici, which has its own proxy class:

npm install undici
import { ProxyAgent, fetch } from "undici";

const dispatcher = new ProxyAgent("http://user:pass@proxy-host:port");
const res = await fetch("https://api.ipify.org", { dispatcher });
console.log(await res.text());

Axios in the browser

Everything above applies to Node.js. In a browser, axios cannot choose a proxy — the browser decides, using its own or the system settings, and the proxy option is ignored. To route browser traffic, configure the browser itself; see how to set up a proxy in Chrome.

Timeouts and retries

Always set timeout (in milliseconds). Without it, a request through a slow or dead proxy can wait a very long time. For occasional failures, retry a few times with a growing pause:

async function getWithRetry(url, tries = 3) {
  for (let n = 1; n <= tries; n++) {
    try {
      return await client.get(url);
    } catch (err) {
      if (n === tries) throw err;
      await new Promise(r => setTimeout(r, 2000 * n));  // 2s, 4s, ...
    }
  }
}

Retry network errors, not refusals. If a site answers with “too many requests” or a refusal, slow down and check its stated rules rather than sending more.

Common errors and fixes

Error Usual cause Fix
ECONNREFUSED Wrong host or port, or proxy unreachable Check details; test with curl -x to rule out your code
Status 407 Login refused, unencoded special characters, or IP not allow-listed Re-check credentials; use encodeURIComponent
EPROTO / “wrong version number” Built-in proxy option with an HTTPS site, or proxy written as https:// Use HttpsProxyAgent with proxy: false; use http:// for the proxy
“Hostname/IP does not match certificate’s altnames” Request sent to the proxy as if it were the website Switch to the agent approach
ECONNRESET / “socket hang up” Protocol mismatch (SOCKS details with an HTTP agent), or the connection was dropped Match agent to protocol; retry with backoff
ETIMEDOUT / timeout exceeded Slow proxy or far location Pick a closer location; raise the timeout slightly

Avoid silencing certificate errors with rejectUnauthorized: false — it removes the protection HTTPS gives you. Fix the setup instead.

Using YiluProxy with axios

All five YiluProxy lines provide a host, port and login that drop into the agent examples above, over HTTP or SOCKS5: rotating residential, static residential, rotating datacenter, static datacenter and mobile. Python user? See Python requests proxy.

A proxy changes where requests come from; it does not change any website’s rules. Use it for publicly visible pages and your own assets, at a reasonable pace.

FAQ

Q1. Does axios support proxies natively?

A. Yes, through the proxy option, but for HTTPS websites a proxy agent passed as httpsAgent with proxy: false is more reliable.

Q2. Why set proxy: false when I am using a proxy?

A. It turns off axios’s own proxy handling, including environment variables, so your agent is the only thing deciding how requests are routed.

Q3. How do I use a SOCKS5 proxy with axios?

A. Install socks-proxy-agent, create a SocksProxyAgent with a socks5h:// address, and pass it as httpAgent and httpsAgent.

Q4. Can I set a proxy for axios in the browser?

A. No. Browsers control their own network routing; configure the proxy in the browser or the operating system instead.


Test first, then buy

Drop a trial proxy into the agent example and run it against your own target pages.

Start a free trial

(Examples use current versions of axios, https-proxy-agent, socks-proxy-agent and undici; package APIs can change between major versions. No pool-size, success-rate or performance figures are given, as those vary by provider, target and time. Written November 2026.)

Scroll to Top