PacketStream integration

Using PacketStream residential proxies with Node.js

Copy a Node.js Undici ProxyAgent setup for PacketStream, verify the residential exit, and add country or sticky-session routing.

Add PacketStream to an existing Node.js fetch script with an Undici proxy dispatcher. Each request opts into proxy routing without changing the rest of your Fetch API code.

Configure

Install Undici and set your credentials:

npm install undici
export PACKETSTREAM_USER='your_username'
export PACKETSTREAM_AUTH_KEY='your_auth_key'

Create packetstream_exit.mjs:

import { ProxyAgent, fetch } from "undici";

const username = process.env.PACKETSTREAM_USER;
const authKey = process.env.PACKETSTREAM_AUTH_KEY;

if (!username || !authKey) {
  throw new Error("Set PACKETSTREAM_USER and PACKETSTREAM_AUTH_KEY");
}

const token = Buffer.from(`${username}:${authKey}`).toString("base64");
const dispatcher = new ProxyAgent({
  uri: "https://proxy.packetstream.io:31111",
  token: `Basic ${token}`,
});

try {
  const response = await fetch("https://ipinfo.io", {
    dispatcher,
    signal: AbortSignal.timeout(30_000),
  });
  if (!response.ok) {
    await response.body?.cancel();
    throw new Error(`HTTP ${response.status}`);
  }
  console.log(await response.json());
} finally {
  await dispatcher.close();
}

Undici’s ProxyAgent sample uses PacketStream’s HTTPS endpoint. It does not provide the authenticated SOCKS5 setup used by this guide.

Verify your exit

Run node packetstream_exit.mjs and inspect the ipinfo.io JSON for the observed ip and country. The explicit status check prevents an error document from being treated as exit data.

Country targeting and sticky sessions

Build the authorization token with the modified auth key:

const routedAuthKey = `${authKey}_country-US_session-api42`;
const token = Buffer.from(`${username}:${routedAuthKey}`).toString("base64");

Pass that token to the same ProxyAgent configuration. Do not put sensitive data in the session label.

Troubleshooting

  • Pass the dispatcher on each intended request. Node’s global fetch does not use this ProxyAgent unless you supply it.
  • Close the dispatcher during shutdown so pooled connections do not keep the process open.
  • Keep the Basic token and any authenticated proxy details out of logs and error reporting.

See proxy troubleshooting for authentication, proxy-scheme, TLS, and timeout checks.

Ready to run Node.js through PacketStream? Create an account to get the proxy username and auth key used in the example above.
Create account