Node.js integration

Proxies for Node.js

fetch() in Node.js has no proxy option of its own: hand it undici's ProxyAgent, or give axios an HTTPS proxy agent. Below: rotating and sticky residential IPs through gate.proxyhat.com, one IP per worker.

Residential proxies
fetch and axios Code tested on our gateway Traffic never expires

What teams use Node.js with proxies for

Public data collection

Load public pages, such as prices, listings and search results, the way a visitor in that country sees them.

Localization and ad checks

Check that geo-targeted pages, translations and ads render correctly in each market, from a residential or mobile connection there.

APIs and pages without a browser

Fetch pages that render without JavaScript, and public APIs, for a fraction of the traffic a headless browser uses.

Connection details

Hostgate.proxyhat.com
SOCKS5 port1080
HTTP port8080
AuthenticationUsername and password from the Proxies page of your dashboard
Username for one workerUSERNAME-country-US-sid-a3f8c20d1e-ttl-30m

Everything after your username is a flag: -country-US picks the exit country, -type-mobile the mobile carrier pool, -sid-… keeps one IP and -ttl-30m sets for how long (up to 24 hours). Leave out -type-mobile for residential IPs.

Need another proxy format? Proxy format converter →

Node.js proxy code

Replace USERNAME and PASSWORD with the proxy credentials from the Proxies page of your dashboard. npm install undici (undici 8 needs Node.js 22.19 or newer), or npm install axios https-proxy-agent for the axios sample. Save it as an .mjs file: the samples use import and top-level await.

import { fetch, ProxyAgent } from 'undici';

// undici takes the login from the proxy URL and sends it as Proxy-Authorization.
// No -sid-: every new connection can exit from a new IP.
const proxy = new ProxyAgent('http://USERNAME-country-US:PASSWORD@gate.proxyhat.com:8080');

const res = await fetch('https://httpbin.org/ip', { dispatcher: proxy });
console.log(await res.json());
await proxy.close();

ProxyAgent takes the username and password from the proxy URL and sends them to the gateway; for HTTPS pages, on the CONNECT request that opens the tunnel. The samples import fetch from undici too, as undici’s own examples do. Without -sid-, every new connection can exit from a new IP.

Tested on November 29, 2026 against gate.proxyhat.com with undici 8.14.0, axios 1.21.1 and https-proxy-agent 9.1.0 on Node.js 22.23: every sample printed a US IP; in the sticky sample each worker kept its IP, and the three workers got three different IPs.

Checked against: undici: ProxyAgent, https-proxy-agent, axios: request config · checked November 29, 2026

Managing proxies from code? The ProxyHat SDK for Python, Node.js and Go creates sub-users with their own traffic limits, lists locations and reads usage; the proxy connection itself stays as above. SDK docs →

Rotating, sticky or no rotation

The session type is part of the username. Your code stays the same; only the login string changes.

Rotating

No session ID: each new connection can exit from a different IP. Good for many independent page loads.

USERNAME-country-US

Sticky

Add -sid- with 10 random hex characters and a -ttl-: the IP holds for that long (10m, 30m, 1h, up to 24h). Use it for multi-page flows that should stay on one address.

USERNAME-country-US-sid-a3f8c20d1e-ttl-30m

No rotation

Add -sid- with -norotate-true and no -ttl-: the IP doesn’t rotate on a timer and stays until you switch to a new sid.

USERNAME-country-US-sid-a3f8c20d1e-norotate-true

One sid holds one exit IP and handles about 4–5 connections at a time, so give every worker or browser its own sid. Sticky sessions last up to 24 hours. Add -type-mobile for 4G/5G carrier IPs; leave it out for residential.

Proxy errors and what they mean

407 Proxy Authentication RequiredThe gateway didn’t accept the login: the username or password is wrong, or the username format is (a typo in a flag such as -country-US). Copy both from the Proxies page of your dashboard and make sure the auth step runs before the first page load.
402 Payment RequiredThe traffic balance is used up, or the sub-user has reached its traffic limit. Top up or raise the limit in the dashboard; traffic you buy never expires.
ERR_TUNNEL_CONNECTION_FAILEDChrome’s name for a proxy that refused the connection, and it can hide a 402 or another gateway status. Send the same username and password through curl -x http://gate.proxyhat.com:8080 to see the real code.
ERR_PROXY_CONNECTION_FAILEDChrome couldn’t reach the gateway at all. Check the host and port (gate.proxyhat.com, 8080 for HTTP) and that your network allows outgoing connections to it.

All gateway errors and username flags are in the documentation: ProxyHat docs →

Price

Mobile and residential traffic share one price and one balance: from $4.00/GB for a 1 GB pack down to $1.80/GB for 1,000 GB. Traffic never expires, and all your workers draw from the same balance. See pricing →

Node.js proxy setup: FAQ

Why doesn’t fetch take a proxy option?

fetch() itself has no proxy setting. undici, the HTTP client behind Node’s fetch, provides ProxyAgent: pass it as the dispatcher option, per request or per worker, as in the samples.

Can I use SOCKS5?

Yes, with an agent that speaks it: socks-proxy-agent for axios and other http-based clients, with a socks5h:// URL on port 1080 so the proxy resolves hostnames. undici also has a Socks5ProxyAgent, still marked experimental. HTTP on port 8080 needs nothing extra and carries HTTPS sites too.

How do I give each worker its own IP?

Give each worker its own agent with its own sid in the username, as in the sticky sample. One sid handles about 4–5 parallel connections, so don’t share one agent between many workers.

How long does a sticky IP last?

As long as the -ttl- flag says, up to 24 hours: -ttl-10m, -ttl-30m, -ttl-1h, -ttl-24h. For an IP that doesn't rotate on a timer, use -sid- with -norotate-true and no -ttl-.

How do I pick a country, state or city?

Add -country- with a two-letter code (residential IPs in 195+ countries). For a state or city, add -region- and then -city- with their location codes, for example -country-US-region-new_york-city-new_york_city: the region must come before the city. -type-mobile switches to 4G/5G carrier IPs (150+ countries) at the same price.

What about got-scraping or Crawlee?

got-scraping takes the same proxy URL in its proxyUrl option. Our got-scraping guide, linked below, covers rotation and retries there.

What do 407 and 402 mean?

407: the gateway didn’t accept the login, so check the username, its flags and the password. 402: the traffic balance or the sub-user’s limit is used up. Depending on the client they arrive as an HTTP status or inside a tunnel error; the errors table above has the details.

Run Node.js through residential IPs

Create an account, buy a small pack and run the samples above before you scale out.

Pay as you go · Traffic never expires · Crypto checkout