---
title: "How to Use a Proxy with Axios: Authentication and HTTPS"
description: "In Node.js, Axios takes a proxy through the proxy option, or through https-proxy-agent with proxy: false. HTTPS, passwords, SOCKS5 and environment variables."
url: https://proxynet.io/blog/axios-proxy-setup
date: 2026-12-01
author: "Acar Diveroli"
category: "Tutorial, Integration"
lang: en
---

# How to Use a Proxy with Axios: Authentication and HTTPS

In Axios, a proxy is passed in one of two ways: with the `proxy` option in the request config or with a proxy agent. Both work in Node.js, but their behaviour changes with the version and the environment. In axios 1.16.0 the `proxy` option opens no tunnel for an HTTPS request, and in 1.16.1 it does. An `HTTPS_PROXY` in the environment can disable an agent you passed by hand. This post covers both methods, environment variables, special characters in passwords and SOCKS5 setup.

> **Note: Short answer**
>
> In Node.js, use the `proxy: { protocol, host, port, auth }` option and keep axios at 1.16.1 or later. On older versions and for SOCKS5, pass the proxy as an agent in `httpsAgent` and add `proxy: false` next to it.

## The built-in proxy option

The Axios `proxy` option takes four fields: `protocol`, `host`, `port` and `auth`. `protocol` says how to connect to the proxy, not which protocol the target site uses. Even when the target is HTTPS, the value stays `http` for most proxies. The `auth` object is written to the `Proxy-Authorization` header as Basic authentication.

```js
import axios from "axios";

const client = axios.create({
  proxy: {
    protocol: "http",
    host: "pr.proxynet.io",
    port: 8000,
    auth: { username: "user", password: "pass" },
  },
  timeout: 20_000,
});

const res = await client.get("https://example.com/");
console.log(res.status);
```

Axios sends the request in one of two ways, depending on the target's protocol. For an `http://` target, the request line carries the full address and the proxy sees everything. For an `https://` target, axios first opens a `CONNECT` tunnel. The encrypted connection is set up inside that tunnel, directly with the target site. The proxy sees only the domain and the port.

This behaviour is new. We ran the same code with three axios versions and checked our test proxy's log:

| Version | Line the proxy saw | Result |
|---|---|---|
| 1.16.0 | `GET https://example.com/` | Address and headers exposed ✗ |
| 1.16.1 | `CONNECT example.com:443` | Tunnel ✓ |
| 1.21.1 | `CONNECT example.com:443` | Tunnel ✓ |

In 1.16.0 the request succeeded, but the proxy received the address and headers in plain text. That row is the trap. The axios 1.16.1 release notes list this fix under a security heading. Check your version with `npm ls axios`, and if it is below 1.16.1, upgrade or use the agent from the next section.

## Setup with https-proxy-agent

`https-proxy-agent` opens the tunnel itself instead of axios. You pass the agent in the `httpsAgent` field, and axios sends the request through it. Because it works independently of the axios version, it is the safe path for older projects.

```bash
npm install axios https-proxy-agent
```

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

const agent = new HttpsProxyAgent("http://user:pass@pr.proxynet.io:8000");

const client = axios.create({
  httpAgent: agent, // http:// targets
  httpsAgent: agent, // https:// targets
  proxy: false, // keep HTTPS_PROXY in the environment from overriding the agent
  timeout: 20_000,
});

const res = await client.get("https://example.com/");
console.log(res.status);
```

The `proxy: false` line is not decoration. In our test, we pointed the agent at proxy B and the `HTTPS_PROXY` variable at proxy A. Without `proxy: false`, the `CONNECT` request went to A. The agent was never used, and no error came back. With the line added, the request went to B.

The reason is that axios applies the environment variable before the agent. If a proxy is defined in the environment, axios builds its own tunnel and uses it in place of your agent. If you use an agent, always add `proxy: false`.

## Environment variables

When no `proxy` option is given, Axios reads the proxy from environment variables. The target's protocol decides which variable is read. An `https://` target uses `HTTPS_PROXY`, an `http://` target uses `HTTP_PROXY`. Lowercase spellings work too.

```bash
export HTTPS_PROXY="http://user:pass@pr.proxynet.io:8000"
export NO_PROXY="localhost,127.0.0.1"
node app.js
```

We tried five combinations:

| Environment | Target | Request |
|---|---|---|
| `HTTPS_PROXY` | `https://example.com` | Via proxy ✓ |
| `HTTP_PROXY` | Local `http://` server | Via proxy ✓ |
| `HTTP_PROXY` only | `https://example.com` | Direct ✗ |
| `HTTPS_PROXY` + `NO_PROXY=example.com` | `https://example.com` | Direct |
| `HTTPS_PROXY` + `proxy: false` | `https://example.com` | Direct |

The third row is a common mistake. If only `HTTP_PROXY` is set, HTTPS requests leave from your own IP without any error. `NO_PROXY` takes a comma-separated list of addresses. `proxy: false` ignores the environment entirely.

Node.js 24.5 and 22.21 brought built-in proxy support. If the process starts with `NODE_USE_ENV_PROXY=1`, Node reads the environment variables. Axios 1.21.1 then leaves the job to Node. In our test, this path also opened a `CONNECT` tunnel.

## Special characters in the password

Where you write the password decides whether it needs encoding. A password in the `auth` object is written as is, and axios turns it into the Basic header itself. Anywhere the proxy address is taken as a URL, `@`, `:`, `/` and `#` break the address.

We tried five paths with the password `p@ss:w#rd/1`:

| Where the password is | Encoding | Result |
|---|---|---|
| `auth` object | None | Connected ✓ |
| `HttpsProxyAgent` URL | None | `Invalid URL` ✗ |
| `HttpsProxyAgent` URL | `encodeURIComponent` | Connected ✓ |
| `HTTPS_PROXY` | None | `Invalid URL` ✗ |
| `HTTPS_PROXY` | `encodeURIComponent` | 407 ✗ |

The last row is a silent trap. Axios takes the password from the address in the environment together with codes like `%40` and sends it without decoding. Our test proxy received `p%40ss%3Aw%23rd%2F1` and returned 407. We saw this in axios 1.16.0 and 1.21.1. The same variable connected with `NODE_USE_ENV_PROXY=1`, because this time Node decoded the address.

Do not put a password with special characters in an environment variable. Use the `auth` object, or an agent with an encoded URL:

```js
const pass = encodeURIComponent(process.env.PROXY_PASS);
const agent = new HttpsProxyAgent(`http://user:${pass}@pr.proxynet.io:8000`);
```

## socks-proxy-agent for SOCKS5

The Axios `proxy` option does not speak SOCKS. When we wrote `protocol: "socks5"`, axios sent an HTTP `CONNECT` line to the SOCKS server. The server did not understand it, and the request returned neither a response nor an error.

SOCKS5 needs the `socks-proxy-agent` package:

```bash
npm install socks-proxy-agent
```

```js
import axios from "axios";
import { SocksProxyAgent } from "socks-proxy-agent";

// take the SOCKS5 address and port from your account's connection details
const agent = new SocksProxyAgent("socks5h://user:pass@pr.proxynet.io:8000");

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

const res = await client.get("https://example.com/");
console.log(res.status);
```

The `h` in the scheme decides where the domain is resolved. With `socks5h://`, the domain reached our test server and the proxy resolved the name. With `socks5://`, the server received an IP address, because the name had been resolved on your machine. If you want the domain resolved at the proxy's location, choose `socks5h`. [SOCKS5 Proxy](https://proxynet.io/socks5-proxy) also carries UDP, but axios speaks HTTP over TCP only.

## Browser and the fetch adapter

Proxy settings work only in Node.js. The axios documentation marks the `proxy` option as "Node.js only". In the browser, axios uses XHR or fetch. There the proxy is decided by the browser or operating system settings, and you cannot pass a proxy and password from code.

The same limit applies in Node when you choose `adapter: "fetch"`. In our test, the fetch adapter ignored both the `proxy` option and the `HTTPS_PROXY` variable and sent the request directly without any error. If you need a proxy, stay on the default `http` adapter. We covered proxy setup with built-in fetch in [Using a Proxy in Node.js](/blog/nodejs-proxy).

## Debugging

Most proxy errors in Axios produce no error message: the request looks successful but leaves from your own IP. So verify the exit IP first: call the same IP echo address with and without the proxy.

```js
const ipUrl = "https://api.ipify.org?format=json";
const direct = await axios.get(ipUrl, { proxy: false });
const viaProxy = await client.get(ipUrl);
console.log(direct.data.ip, viaProxy.data.ip); // the two values should differ
```

| Symptom | Likely cause |
|---|---|
| `Request failed with status code 407` | Wrong password, or an encoded password in the environment |
| `Invalid URL` | Unencoded special character in the password in the URL |
| Both IPs are the same | `HTTP_PROXY` only, a `NO_PROXY` match or the fetch adapter |
| Request hangs | `socks5` in the `proxy` option |
| `GET https://…` in the proxy log | axios older than 1.16.1 |

We tried a wrong password on three paths: the `proxy` option with an `http://` target, axios's own tunnel with an `https://` target, and `https-proxy-agent`. In all three, `error.response.status` was 407. We collected the other causes of 407 in [HTTP 407 Proxy Authentication Required](/blog/http-407-proxy-authentication-required).

## How we tested

We ran the tests on 1 December 2026 on Windows 11 with Node.js 24.22.2. The packages were installed in a separate folder: axios 1.21.1, axios 1.16.0 and 1.16.1 for comparison, https-proxy-agent 9.1.0 and socks-proxy-agent 10.1.0. The setup was:

- A local Node.js HTTP proxy that requires Basic authentication and logs every request and `CONNECT` line.
- A small SOCKS5 server that requires a username and password and logs whether the incoming address is a domain or an IP.
- `https://example.com` and a local HTTP server as targets.
- Each scenario in a fresh Node.js process, because environment variables are read at process start.

We also ran the code samples in this article on the same setup, with the proxy address swapped for the local proxy.

## The bottom line

Keep axios at 1.16.1 or later, put the password in the `auth` object, and add `proxy: false` whenever you use an agent. Above all, compare the IP with and without the proxy once, because most proxy errors in axios are silent. If you need an exit IP by location, [Residential Proxy](https://proxynet.io/residential-proxy) works with the same setup.

## Sources

- [axios, Request config, proxy section](https://axios.rest/pages/advanced/request-config): axios.rest
- [axios v1.16.1 release notes, Security Fixes](https://github.com/axios/axios/releases/tag/v1.16.1): github.com
- [https-proxy-agent README](https://github.com/TooTallNate/proxy-agents/tree/main/packages/https-proxy-agent): github.com
- [Node.js HTTP documentation, Built-in Proxy Support](https://nodejs.org/api/http.html#built-in-proxy-support): nodejs.org

## Frequently asked questions

### Can Axios use a proxy in the browser?

No. The `proxy` option only works in the `http` adapter in Node.js. In the browser, axios sends the request with XHR or fetch, and the browser or operating system proxy settings manage the traffic. Writing a proxy password into page code exposes it to every visitor. If a browser request has to go through a proxy, send it to your own server and use the proxy there.

### Does the Axios proxy setting work with HTTPS sites?

Yes, in 1.16.1 and later. These versions open a `CONNECT` tunnel for an HTTPS target and set up the encrypted connection directly with the site. In our test, 1.16.0 sent the request to the proxy with the full address, in plain text. Check your version with `npm ls axios`. If you cannot upgrade, use `https-proxy-agent` and add `proxy: false`.

### What does proxy: false do?

It turns off Axios's own proxy logic. The `proxy` option and the `HTTP_PROXY` and `HTTPS_PROXY` variables are ignored. You need this line when using an agent. In our test, with `HTTPS_PROXY` set in the environment, axios put the agent aside and used the proxy from the environment. If you write only `proxy: false` without an agent, the request goes out directly, from your own IP.

### Does Axios read the HTTP_PROXY variable?

Yes. If no `proxy` option is given, axios reads `HTTP_PROXY` for `http://` targets and `HTTPS_PROXY` for `https://` targets. Lowercase spellings are valid too. If only `HTTP_PROXY` is set, HTTPS requests do not go to the proxy. Addresses in the `NO_PROXY` list go out directly. If the password has special characters, use the `auth` object instead of the variable.

### Does Axios support SOCKS5 proxies?

Not built in. The `proxy` option speaks HTTP proxy only. For SOCKS5, install the `socks-proxy-agent` package, pass the agent to both `httpAgent` and `httpsAgent`, and write `proxy: false`. Start the address with `socks5h://` so the proxy resolves the domain. With `socks5://`, the name is resolved on your machine and the proxy receives an IP address.

### Why do I get a 407 error in Axios?

Because the proxy did not accept the credentials. The most common cause is a wrong username or password. The second is an encoded password in an environment variable: axios sends codes like `%40` without decoding them, and the proxy does not recognise the password. Catch the error with `error.response.status === 407`. Writing the password unencoded in the `auth` object removes the second problem.
