Authenticated proxies in Node.js: Axios, native fetch and Got
Updated:
The reliable way to use a Node.js proxy with Axios is an HttpsProxyAgent passed as httpsAgent, together with proxy: false. Axios's own proxy: { host, port } setting does not open an HTTP CONNECT tunnel, so it breaks on https:// URLs; native fetch() needs an undici ProxyAgent instead. This page shows each client with a ProxyV4 http://user:pass@ip:port URL and how to confirm the exit IP.
new HttpsProxyAgent('http://user:pass@ip:port')Pick the snippet for your HTTP client
All three clients take the same proxy URL. Build it from the ip:port:user:pass line in your order; the scheme stays http:// because the client speaks HTTP to the proxy and tunnels TLS through it.
| Client | Install | Proxy wiring |
|---|---|---|
| Axios | npm i https-proxy-agent | axios.get(url, { httpsAgent: new HttpsProxyAgent('http://user:pass@ip:port'), proxy: false }) |
Native fetch (Node 18+) | npm i undici | fetch(url, { dispatcher: new ProxyAgent('http://user:pass@ip:port') }) |
| Got | npm i hpagent | got(url, { agent: { https: new HttpsProxyAgent({ proxy: 'http://user:pass@ip:port' }) } }) |
Why `proxy: false` matters in Axios
Axios checks the HTTP_PROXY and HTTPS_PROXY environment variables on its own. If one of them is set on the machine (common in CI runners and containers) and you also pass an agent, the two configurations collide. proxy: false tells Axios to ignore the environment and its built-in proxy logic, so the agent is the only thing deciding where the request goes.
Reusing agents across many requests
- Create one agent per proxy IP outside your request loop; the agent holds keep-alive sockets, and a new agent per call throws them away
- With several proxies, keep a
Mapfrom IP to agent and pick from it, rather than building agents on the fly - Give every call a
timeout(for exampletimeout: 10000in Axios) so one stalled target does not hold a worker forever - Need the proxy list as data? The proxy format converter turns
ip:port:user:passlines into a JSON array you canrequireorimport
What a stable IP gives a Node.js job
A ProxyV4 private IPv4 belongs to one customer for the whole rental and keeps the same address when you renew, so an allow-listed API, a logged-in session or a rate-limit budget tied to that IP stays valid between deploys. Bandwidth is not metered, which keeps long-running workers predictable. For crawling patterns and pacing, see the web scraping use case.
Step by step
- 1
Install the client and the agent
Run
npm install axios https-proxy-agentin the project (ornpm install undiciif you use nativefetch). - 2
Build the agent once
Create
const agent = new HttpsProxyAgent('http://user:pass@ip:port');with the values from your ProxyV4 order. - 3
Send a request through it
Call
const res = await axios.get('https://proxyv4.com/api/my-ip', { httpsAgent: agent, proxy: false, timeout: 10000 });. - 4
Compare the IP
console.log(res.data)should print the proxy IP. If it prints your server's own address, the agent was not applied.
Frequently asked questions
Why does Axios `proxy: { host, port, auth }` fail on HTTPS sites?
That option sends an ordinary HTTP request to the proxy instead of first asking for an HTTP CONNECT tunnel, so TLS to the target never gets set up properly. Use httpsAgent: new HttpsProxyAgent('http://user:pass@ip:port') and set proxy: false.
How do I add a proxy to native `fetch()` in Node 18, 20 or 22?
Node's fetch runs on undici, so import ProxyAgent from undici and pass { dispatcher: new ProxyAgent('http://user:pass@ip:port') } as part of the fetch() options.
My password contains `@` or `:`. Will the URL break?
It can, because those characters have meaning inside a URL. Percent-encode the password with encodeURIComponent(password) before you build http://user:pass@ip:port.
The request returns 407. What is wrong?
The proxy answered but rejected the login. Recheck the username and password from your order; the 407 troubleshooting page lists the common causes.
Static IPv4 for your Node.js workers
Private IPv4 in 7 countries plus static ISP in the US and Vietnam, unmetered, one customer per IP. Buy one or many.
Check live US prices