Proxy in Node.js: fetch and axios

Node.js's built-in fetch doesn't pick up a proxy by itself; you need an agent. Here is undici ProxyAgent for HTTP and agents for axios, including SOCKS5.

HTTPHTTPSSOCKS5Login and passwordRotation and sticky
Node.js · proxy
http://LOGIN:PASSWORD@HOST:PORT
new ProxyAgent(...)
socks5h://LOGIN:PASSWORD@HOST:PORT
✓ IP: Germany, residentialRun

Why use a proxy in Node.js

Node.js is a server-side JavaScript runtime. The built-in fetch runs on the undici library, and a proxy is attached through ProxyAgent in the dispatcher option. For axios and the http/https modules, use the https-proxy-agent and socks-proxy-agent agents.

What to pick

For fetch take HTTP: undici ProxyAgent works with HTTP proxies and takes the login from the URL. If you need SOCKS5, use socks-proxy-agent with the socks5h scheme. Rotation for scraping; a sticky session when one user's requests must come from one IP.

Residential

from $0.6 / GB

Home ISP addresses. The main choice for accounts and protected sites.

VIP

$2 / GB

Hand-picked clean IPs, faster and more stable than regular residential.

Mobile

$4 / GB

Carrier IPs, for platforms that check addresses strictly.

Step-by-step setup

Button names are given exactly as they appear in Node.js.

  1. Install the package

    For fetch: npm i undici. For axios: npm i axios https-proxy-agent, or npm i axios socks-proxy-agent for SOCKS5.

  2. Build the string

    Export login:pass@ip:port in @SousProxyBot and add the http:// or socks5h:// scheme. Encode special characters in the password with encodeURIComponent.

  3. Attach the agent

    For fetch pass { dispatcher: new ProxyAgent(url) }. For axios pass the agent in httpAgent and httpsAgent and turn off the built-in proxy: proxy: false.

  4. Run it

    Save the example as ip.mjs and run node ip.mjs.

  5. Check the IP

    The script prints JSON with the proxy address. If it's your address, the agent didn't reach the request.

Code example

Put in your own HOST, PORT, LOGIN and PASSWORD from the bot or the dashboard.

js
import { fetch, ProxyAgent } from 'undici';

const dispatcher = new ProxyAgent('http://LOGIN:PASSWORD@HOST:PORT');

const res = await fetch('https://api.ipify.org?format=json', { dispatcher });
console.log(await res.json());

Tested by running it

We ran this example ourselves: Node.js 22.23.5 · undici 8.13.1. Real output below, IP hidden.

Terminal · ip.mjs
$ node ip.mjs
{ ip: 'proxy IP' }

And this is the response when the password is wrong:

$ node ip.mjs
…
  }
}

Node.js v22.23.5

Common errors

The issues people most often bring to support.

!

Request goes out on your IP though ProxyAgent was createdThe agent isn't passed to the request. Check the dispatcher option in fetch or httpAgent and httpsAgent in axios.

!

407 responseThe login or password is wrong or not encoded. Run them through encodeURIComponent before putting them in the URL.

!

axios ignores the agentaxios reads HTTP_PROXY by itself. Set proxy: false so it uses only your agent.

!

ProxyAgent doesn't work with socks5://undici ProxyAgent is for HTTP proxies. For SOCKS5 use socks-proxy-agent.

Need a proxy for Node.js?

Residential, VIP and mobile IPs. HTTP and SOCKS5, pay per GB, 1 GB free to try.

FAQ

How do I use SOCKS5 in axios?

import { SocksProxyAgent } from 'socks-proxy-agent'; const agent = new SocksProxyAgent('socks5h://LOGIN:PASSWORD@HOST:PORT'); then axios.get(url, { httpAgent: agent, httpsAgent: agent, proxy: false }).

And an HTTP proxy in axios?

The same, but with new HttpsProxyAgent('http://LOGIN:PASSWORD@HOST:PORT') from the https-proxy-agent package.

Can I give the built-in fetch an undici agent?

Yes, global fetch accepts the dispatcher option. It's more reliable to import fetch from the same undici package so versions match.