Node.js proxy with undici or https-proxy-agent
fetch in Node has no option that takes a proxy URL, so you hand it a dispatcher from undici. Code built on the https module takes an agent from https-proxy-agent instead. Both are below.
Before you start
- Node.js 22 or newer, kept up to date: current undici releases refuse older versions.
- npm i undici for the fetch example, or npm i https-proxy-agent for the https one.
- HOST, PORT, USERNAME and PASSWORD from the service page.
Your connection details
Sign in to the dashboard and open the service you bought. Its page shows the host, port, username and password for that service. There is no single ProxyPanda address to remember, so copy them from there each time. The code in these guides uses the placeholders below; replace each one with your own value.
- HOST
- The proxy address for this service, exactly as the service page shows it.
- PORT
- The port to connect to. Copy it with the host, since it can differ from one service to the next.
- USERNAME
- Your proxy login. It is separate from the email you use for the dashboard.
- PASSWORD
- Copy it in full. On residential, the options you choose in the dashboard are added to the end of the password, so a password typed from memory loses them.
Need help? Letting the proxy recognise your IP address
The examples use HTTP, which reaches https sites through an encrypted tunnel. SOCKS5 works too: residential ports accept both, and ISP and datacenter proxies switch protocol in the dashboard.
Steps
- fetch through undici’s ProxyAgentImport fetch and ProxyAgent from the same package and pass the agent as dispatcher. The login goes inside the URL.
import { fetch, ProxyAgent } from 'undici'; const dispatcher = new ProxyAgent('http://USERNAME:PASSWORD@HOST:PORT'); const response = await fetch('https://api.ipify.org?format=json', { dispatcher }); console.log(await response.json()); - The https module with https-proxy-agentHttpsProxyAgent opens a CONNECT tunnel through the proxy for https requests. Any library that accepts a Node agent takes the same object.
import https from 'node:https'; import { HttpsProxyAgent } from 'https-proxy-agent'; const agent = new HttpsProxyAgent('http://USERNAME:PASSWORD@HOST:PORT'); https.get('https://api.ipify.org?format=json', { agent }, (res) => { let body = ''; res.on('data', (chunk) => (body += chunk)); res.on('end', () => console.log(body)); }); - Run it twice and compareBoth scripts print the address api.ipify.org saw. Each run is a new process with a new connection, so two runs on residential with Randomize IP should print two addresses.
Rotating and sticky IPs
Both agents keep connections open and reuse them. Requests that share one tunnel leave from the same address, so a long-running process on Randomize IP may not change exit on every call. Create a fresh agent when you want a new address for certain.
On Sticky IP, or on an ISP or datacenter IP, reuse is harmless: the address is meant to stay the same.
Common errors and fixes
TypeError: fetch failed, caused by invalid onRequestStart method
Node’s built-in fetch was given a ProxyAgent from the undici package, and the two versions do not match. Import fetch from undici too, as in the example.
RequestAbortedError: Proxy response (407) !== 200 when HTTP Tunneling
The proxy refused the login. Copy the username and password again, and wrap them in encodeURIComponent if they contain @, : or /.
ECONNREFUSED or ETIMEDOUT
HOST or PORT is wrong, or the service has lapsed. Check both on the service page, and check that the service still shows as active.
The script prints your own IP
The agent never reached the request. With https.get it belongs in the options object as agent; with fetch it goes in as dispatcher.
Need help? What a 407 or 403 message means
Which line to pick
For API calls and light scraping from a Node service, datacenter IPs are the fast, cheap default. Choose ISP when your code logs in to one account from one address for weeks, and residential when it needs home-network addresses. If an API starts answering 429, lower the request rate.
Other setup guides
Not sure what a word means? The glossary explains it in plain English.
Stuck on a step?
Paste the command and the error into Discord, with your password taken out. People there have met most of these errors before.