# Node.js Proxy Setup: HTTP and SOCKS5 | Core Proxies

Source: https://coreproxies.com/integrations/nodejs/

Node.js · Integration guide

## Node.js proxy setup

Node.js has no single proxy setting: each HTTP client takes the proxy in its own way. This guide covers the three you are most likely to use, `fetch` through undici, axios, and the built-in `https` module, with the same Core Proxies username in each.

Last updated: November 27, 2026

[Get started](https://coreproxies.com/register/) · [See pricing](https://coreproxies.com/pricing/)

### Connection fields

The four dashboard values end up in a proxy URL or in an agent object, depending on the client.

The dashboard shows `HOST:PORT:USERNAME:PASSWORD`. In Node.js these become one of two shapes:

- **undici ProxyAgent**: `uri` holds the scheme, host and port; `token` holds the login as a Basic authorization value, which is the Base64 form of `USERNAME:PASSWORD`.
- **Agent packages** (`https-proxy-agent`, `socks-proxy-agent`): a single URL, `http://USERNAME:PASSWORD@HOST:PORT`. Encode the username and password with `encodeURIComponent()` if they contain characters such as `@` or `:`.

The `fetch` built into Node.js is powered by undici, but `ProxyAgent` is exported only by the npm package. Install it with `npm install undici` and import `fetch` from the same package, so the client and the agent come from one version. The examples use ES modules and top-level `await`; save them as `.mjs`.

| Product | Host and port | Protocols | Authentication |
| --- | --- | --- | --- |
| Residential and Unlimited Residential | shown in your dashboard | HTTP, HTTPS and SOCKS5 on the same port | username:password |
| Rotating Mobile | shown in your dashboard | HTTP(S) and SOCKS5 on the same port | username:password |
| Mobile Proxy (Reset Link) | shown in your dashboard | HTTP(S) and SOCKS5 on the same port | username:password |
| Datacenter, ISP and IPv6 | shown with your order in the dashboard | HTTP(S) and SOCKS5 on separate ports | username:password |

HOST, PORT, USERNAME and PASSWORD in the examples are placeholders. Your dashboard shows the host, port, username and password for each plan: copy them from the Proxy generator.

### HTTP(S) or SOCKS5

The protocol decides which package you need.

| Protocol | Residential and mobile plans | Datacenter, ISP and IPv6 | In Node.js |
| --- | --- | --- | --- |
| HTTP(S) | host and port from your dashboard | HTTP(S) port shown with your order | undici ProxyAgent, or https-proxy-agent for axios and the https module |
| SOCKS5 | same host and port | SOCKS5 port shown with your order | socks-proxy-agent with a socks5h:// URL |

Use HTTP(S) unless you have a reason not to; it works with `fetch` and needs one dependency. For SOCKS5, `socks-proxy-agent` produces a standard agent for the `http` and `https` modules and for libraries built on them. The `socks5h://` scheme makes the proxy resolve the target hostname. On Residential and mobile plans SOCKS5 uses the same host and port as HTTP(S); Datacenter, ISP and IPv6 orders show a separate SOCKS5 port.

```javascript
// npm install socks-proxy-agent
import https from "node:https";
import { SocksProxyAgent } from "socks-proxy-agent";

// HOST, PORT, USERNAME and PASSWORD: copy them from your dashboard (Proxy generator).
// socks5h: the proxy resolves the target hostname
const agent = new SocksProxyAgent("socks5h://USERNAME:PASSWORD@HOST:PORT");

https
  .get("https://api.ipify.org?format=json", { agent, timeout: 30000 }, (res) => {
    let body = "";
    res.on("data", (chunk) => (body += chunk));
    res.on("end", () => console.log(body));
  })
  .on("error", (error) => console.error("request failed:", error.message));
```

### Authentication example

fetch through the proxy with a username and password.

**fetch (undici)**

```javascript
import { fetch, ProxyAgent } from "undici";

// Copy these four values from your dashboard (Proxy generator).
const HOST = "HOST";
const PORT = "PORT";
const USERNAME = "USERNAME";
const PASSWORD = "PASSWORD";

const dispatcher = new ProxyAgent({
  uri: `http://${HOST}:${PORT}`,
  token: "Basic " + Buffer.from(`${USERNAME}:${PASSWORD}`).toString("base64"),
});

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

**Global dispatcher**

```javascript
import { fetch, ProxyAgent, setGlobalDispatcher } from "undici";

// HOST, PORT, USERNAME and PASSWORD: copy them from your dashboard (Proxy generator).
// Every undici request in this process now goes through the proxy.
setGlobalDispatcher(
  new ProxyAgent({
    uri: "http://HOST:PORT",
    token: "Basic " + Buffer.from("USERNAME:PASSWORD").toString("base64"),
  }),
);

const res = await fetch("https://api.ipify.org?format=json");
console.log(await res.json());
```

**axios**

```javascript
// npm install axios https-proxy-agent
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";

// HOST, PORT, USERNAME and PASSWORD: copy them from your dashboard (Proxy generator).
const agent = new HttpsProxyAgent("http://USERNAME:PASSWORD@HOST:PORT");

// proxy: false turns off axios' own proxy handling so the agent is used.
const res = await axios.get("https://api.ipify.org?format=json", {
  httpsAgent: agent,
  proxy: false,
  timeout: 30000,
});
console.log(res.data);
```

Replace `HOST`, `PORT`, `USERNAME` and `PASSWORD` with the values from your dashboard; copy them from the Proxy generator. The first tab passes the agent to a single request. The second installs it for the whole process with `setGlobalDispatcher()`, which is convenient when a library calls undici for you.

The axios tab uses `https-proxy-agent` together with `proxy: false`. That routes HTTPS targets through a CONNECT tunnel, which is what the proxy expects. All three work the same way on every plan; only the four values change.

### Rotation and sticky sessions

Create the agent with the username that matches the behaviour you want.

The script builds a small helper that returns an agent for any username. On a Residential plan, the first loop uses the plain username with a fresh agent each time, so every request opens a new connection and should normally print a different IP (an address can repeat now and then). The second loop creates one agent with a random six-digit session number and reuses it, so all three requests share an IP.

```javascript
import { fetch, ProxyAgent } from "undici";

// Residential plan: copy these four values from your dashboard (Proxy generator).
const HOST = "HOST";
const PORT = "PORT";
const USERNAME = "USERNAME";
const PASSWORD = "PASSWORD";
const URL_TO_FETCH = "https://api.ipify.org?format=json";

const agentFor = (username) =>
  new ProxyAgent({
    uri: `http://${HOST}:${PORT}`,
    token: "Basic " + Buffer.from(`${username}:${PASSWORD}`).toString("base64"),
  });

// Rotating: no session in the username. A fresh agent means a new connection,
// so each pass normally gets a new IP (an address can repeat now and then).
for (let i = 0; i < 3; i++) {
  const dispatcher = agentFor(USERNAME);
  const res = await fetch(URL_TO_FETCH, { dispatcher });
  console.log("rotating:", await res.json());
  await dispatcher.close();
}

// Sticky: a session number (digits only) keeps the same IP for the ttl (600 seconds).
const sessionId = Math.floor(100000 + Math.random() * 900000);
const sticky = agentFor(`${USERNAME}-session-${sessionId}-ttl-600`);
for (let i = 0; i < 3; i++) {
  const res = await fetch(URL_TO_FETCH, { dispatcher: sticky });
  console.log("sticky:", await res.json());
}
await sticky.close();
```

An agent keeps its connection to the proxy open between requests. That is efficient for sticky work. For rotating work, closing the agent after each request, as the first loop does, makes sure every request starts on a new connection. In a concurrent crawler, keep one agent per sticky session and pass it explicitly to each request.

| Product | New IP | Same IP | Location |
| --- | --- | --- | --- |
| Residential and Unlimited Residential | `USERNAME` (every new connection) | `USERNAME-session-482913-ttl-600` | `USERNAME-country-de` |
| Rotating Mobile | `USERNAME` (every new connection) | `USERNAME-session-482913-ttl-600` | chosen in your dashboard |
| Mobile Proxy (Reset Link) | Change IP button or reset link | `USERNAME` keeps one IP for up to 24 hours | chosen in your dashboard |
| Datacenter, ISP and IPv6 | set in the Proxy generator | set in the Proxy generator | set in the Proxy generator |

On Residential and Unlimited Residential the username on its own rotates, so every new connection gets a new IP. For a sticky session add `-session-` with a number and `-ttl-` with the time in seconds. Sticky sessions last up to 24 hours; the dashboard offers 1 minute, 10 minutes, 30 minutes, 1 hour, 6 hours and 24 hours, or a custom time. Use a different session number for each account or task; a new number gives you a new IP. On Datacenter, ISP and IPv6, the Proxy generator of your order builds the username for you: copy the generated lines from there. Details are in the [rotating proxies guide](https://coreproxies.com/rotating-proxies/).

For IPs from one country on Residential or Unlimited Residential, add `-country-` and a two-letter country code to the username, for example `USERNAME-country-de`. State and city are chosen in the Proxy generator, which adds them to the username for you. Location and session combine as `USERNAME-country-de-session-482913-ttl-600`:

```javascript
import { fetch, ProxyAgent } from "undici";

// Residential and Unlimited Residential: copy these four values from your dashboard (Proxy generator).
const HOST = "HOST";
const PORT = "PORT";
const USERNAME = "USERNAME";
const PASSWORD = "PASSWORD";

// IPs from Germany, one IP held for 600 seconds.
// Use `${USERNAME}-country-de` on its own to rotate inside the country.
const username = `${USERNAME}-country-de-session-482913-ttl-600`;

const dispatcher = new ProxyAgent({
  uri: `http://${HOST}:${PORT}`,
  token: "Basic " + Buffer.from(`${username}:${PASSWORD}`).toString("base64"),
});

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

On Rotating Mobile you choose All countries or a single country in your dashboard, and the carrier inside that country is picked automatically. The username carries only the session: without one, every new connection gets a new carrier IP.

On Rotating Mobile the session ID must be numbers only (digits 0–9); an ID with letters is ignored and the IP keeps rotating. The number is only an example: pick your own, such as a random 6-digit number, and use a different one for each account or task. The ttl value is how long you want to keep the IP, in seconds. Mobile networks can still change an IP on their own, so plan for an occasional change.

Mobile Proxy (Reset Link) keeps one carrier IP for up to 24 hours. Change it with the Change IP button or your reset link, once every 30 seconds; changing the location in your dashboard also gives you a new IP.

### Test the connection

Print the exit IP and the failure reason in one script.

`https://api.ipify.org?format=json` returns the address a target would see. Compare it with your own address, which the [My IP tool](https://coreproxies.com/tools/my-ip/) shows in a browser. The script below adds a 30-second limit and prints the underlying cause when the request fails:

```javascript
import { fetch, ProxyAgent } from "undici";

// HOST, PORT, USERNAME and PASSWORD: copy them from your dashboard (Proxy generator).
const dispatcher = new ProxyAgent({
  uri: "http://HOST:PORT",
  token: "Basic " + Buffer.from("USERNAME:PASSWORD").toString("base64"),
});

try {
  const res = await fetch("https://api.ipify.org?format=json", {
    dispatcher,
    signal: AbortSignal.timeout(30000),
  });
  console.log("status:", res.status);
  console.log("exit IP:", (await res.json()).ip);
} catch (error) {
  // The underlying reason (refused tunnel, timeout, DNS) is in error.cause.
  console.error("request failed:", error.cause?.message ?? error.message);
} finally {
  await dispatcher.close();
}
```

### Troubleshooting

In Node.js the useful detail is usually one level down, in error.cause.

#### 407 Proxy Authentication Required

The proxy rejected the login while opening the tunnel, so `fetch` fails with a generic error and the proxy's status is in `error.cause`. Check that the token is built from `USERNAME:PASSWORD` with any suffix on the username, that it starts with `Basic` including the space, and that the credentials belong to the plan whose host is in `uri`.

#### Timeouts

A connect timeout or `ECONNREFUSED` means the proxy was not reached: check the host, the port and outbound firewall rules for the proxy port. `ENOTFOUND` is a typo in the hostname. If the IP check is fast but a target is slow, raise the value in `AbortSignal.timeout()`. If requests ignore the proxy altogether, you are probably calling a `fetch` that has no dispatcher: import `fetch` from undici and pass the agent, or call `setGlobalDispatcher()` first, as the examples do.

#### Target ports by plan

On Residential and Unlimited Residential plans, residential IPs connect to websites on ports 80 and 443 only: a URL that names another port, such as `:8443`, fails even when the proxy login is correct.

All target ports are open on Rotating Mobile and Mobile Proxy (Reset Link) plans.

### Node.js proxy setup: frequently asked questions

#### Does the built-in fetch in Node.js support proxies?

Not on its own. Install the undici package and import both fetch and ProxyAgent from it, as the examples do, so the client and the agent come from one version.

#### How do I use a proxy with axios in Node.js?

Create an HttpsProxyAgent from the https-proxy-agent package with the proxy URL, pass it as httpsAgent and set proxy: false so axios uses the agent.

#### How do I use a SOCKS5 proxy in Node.js?

Use the socks-proxy-agent package with a socks5h:// URL and the host and port from your dashboard, and pass the agent to the https module or to a library that accepts an agent. On Residential and mobile plans SOCKS5 uses the same port as HTTP(S).

#### How do I rotate IPs in Node.js?

On Residential, build the agent with the plain username and open a new connection for each request. For a sticky session, use USERNAME-session-482913-ttl-600 with your own session number and reuse that agent. On Datacenter, ISP and IPv6, copy the lines from the Proxy generator.

#### Why do my requests not go through the proxy?

A request made without a dispatcher or agent goes direct. Pass the ProxyAgent to each fetch call or install it once with setGlobalDispatcher().

[All frequently asked questions](https://coreproxies.com/faq/)

Get started

### Send Node.js traffic through Core Proxies

Create an account, choose your proxy type and plan, and copy the host, port, username and password from your dashboard. Activation is automatic after payment.

- Rotating and sticky sessions
- HTTP(S) and SOCKS5
- 24/7 support by email

[Get started](https://coreproxies.com/register/) · [Talk to sales](mailto:info@coreproxies.com)

**Plans to start with** · Datacenter and ISP

**Datacenter** · from €15 / month · 250 GB · 100 IPs · [See pricing](https://coreproxies.com/datacenter/)

**ISP** · from €18.23 / 250 GB + €0.23 / IP · [See pricing](https://coreproxies.com/isp/)

Prices exclude VAT

### Related pages
