# Node.js and TypeScript

Source: https://prontaro.com/docs/languages/nodejs/

Route Node.js and TypeScript requests through your dedicated static IPv4 with undici, axios, native fetch, or SOCKS5 with connection pooling.

The right client depends on the proxy protocol and your Node.js runtime — pick the pattern that matches your stack:

| Client | Proxy schemes | Best for |
| --- | --- | --- |
| `undici` (`ProxyAgent`) | `https://`, `http://` | Default choice for Node 20+ servers and serverless functions using `fetch` |
| `axios` + `https-proxy-agent` | `https://`, `http://` | Existing `axios` codebases |
| Native `fetch` + `NODE_USE_ENV_PROXY=1` | `https://`, `http://` | Zero dependencies on Node 22.21+ and Node 24+ |
| `socks-proxy-agent` | `socks5h://` | `node:https` or `axios` clients that need SOCKS5 |

## Step 1. Get your proxy URL

1. Open **IP addresses** in your Prontaro dashboard ([my.prontaro.com/ips](https://my.prontaro.com/ips)) and select your dedicated IPv4.
2. Copy the `HTTPS` proxy URL (`:443`) from **Proxy credentials**.

IP addresses Proxy credentials [Open in dashboard](https://my.prontaro.com/ips)

Export the proxy string to `PRONTARO_PROXY_URL`:

```bash
export PRONTARO_PROXY_URL="https://user:pass@<entry>.prontaro.io:443"
```

## `undici` (`ProxyAgent`) — recommended

Install `undici` and create one module-level `ProxyAgent` so requests share a single TCP/TLS keep-alive pool:

```bash
npm install undici
```

```ts
// egress-client.ts
import { fetch, ProxyAgent } from 'undici';

const proxyUrl = process.env.PRONTARO_PROXY_URL;
if (!proxyUrl) {
  throw new Error('PRONTARO_PROXY_URL is not set');
}

// One shared agent per process — do not close it after every request
export const proxyAgent = new ProxyAgent({
  uri: proxyUrl,
  connections: 10,
});

export async function checkOutboundIp(): Promise<string> {
  const res = await fetch('https://api.ipify.org', {
    dispatcher: proxyAgent,
    signal: AbortSignal.timeout(15_000),
  });

  if (!res.ok) {
    throw new Error(`HTTP ${res.status}: ${await res.text()}`);
  }
  return (await res.text()).trim();
}

// Close only during graceful process shutdown
process.once('SIGTERM', () => {
  void proxyAgent.close();
});

console.log(await checkOutboundIp());
```

Verify your outbound IP:

```
 
203.0.113.10
```

> **Do not call proxyAgent.close() inside a request handler**
>
> Calling `proxyAgent.close()` in a `finally` block tears down the keep-alive pool and fails concurrent requests with `ClientDestroyedError`. Close the agent only on `SIGTERM`/`SIGINT`, or let short-lived scripts exit naturally.

After confirming `api.ipify.org` returns your dedicated IPv4, you can lock down destinations in the dashboard (**IP addresses → Proxy credentials → Access**).

## `axios` + `https-proxy-agent`

Axios’s built-in `proxy` option struggles with `https://` proxy hosts and can conflict with environment variables. Pass `HttpsProxyAgent` as `httpsAgent` and explicitly set `proxy: false`:

```bash
npm install axios https-proxy-agent
```

```ts
// axios-client.ts
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';

const proxyUrl = process.env.PRONTARO_PROXY_URL;
if (!proxyUrl) {
  throw new Error('PRONTARO_PROXY_URL is not set');
}

const httpsAgent = new HttpsProxyAgent(proxyUrl);

export const partnerApi = axios.create({
  httpsAgent,
  proxy: false, // disable Axios's built-in HTTP_PROXY handling
  timeout: 15_000,
});

const { data } = await partnerApi.get<string>('https://api.ipify.org');
console.log(data.trim());
```

## Native `fetch` with `NODE_USE_ENV_PROXY=1` (Node 22 / 24)

In Node.js 24+ and Node.js 22.21+, built-in `fetch()` honors `HTTPS_PROXY`, `HTTP_PROXY`, and `NO_PROXY` with zero dependencies when started with `NODE_USE_ENV_PROXY=1` (or `--use-env-proxy`):

```js
// check-env.mjs
const res = await fetch('https://api.ipify.org', {
  signal: AbortSignal.timeout(15_000),
});
if (!res.ok) {
  throw new Error(`HTTP ${res.status}`);
}
console.log((await res.text()).trim());
```

```bash
HTTPS_PROXY="$PRONTARO_PROXY_URL" \
NO_PROXY="localhost,127.0.0.1,::1,169.254.169.254,.internal" \
NODE_USE_ENV_PROXY=1 \
node check-env.mjs
```

Because this applies to every `fetch()` call in the process, always configure `NO_PROXY` so internal services and telemetry bypass Prontaro.

## SOCKS5 (`socks5h://`) with `socks-proxy-agent`

`undici.ProxyAgent` supports `http://` and `https://` only. For `socks5h://` (`:1080`), pair `SocksProxyAgent` with `axios` or `node:https`:

```bash
npm install axios socks-proxy-agent
```

```ts
// socks-client.ts
import axios from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';

// Use socks5h:// so DNS resolves on the Prontaro node
const socksUrl = process.env.PRONTARO_SOCKS_URL; // socks5h://user:pass@<entry>.prontaro.io:1080
if (!socksUrl) {
  throw new Error('PRONTARO_SOCKS_URL is not set');
}

const socksAgent = new SocksProxyAgent(socksUrl);

const { data } = await axios.get<string>('https://api.ipify.org', {
  httpAgent: socksAgent,
  httpsAgent: socksAgent,
  proxy: false,
  timeout: 15_000,
});
console.log(data.trim());
```

## Common Node.js issues

- **Mixing global `fetch` with npm `undici`’s `ProxyAgent`.** Version mismatches between Node’s bundled undici and the installed package can break `dispatcher`. Import both `fetch` and `ProxyAgent` from `'undici'`.
- **Omitting `proxy: false` in `axios`.** When `HTTPS_PROXY` is set alongside `httpsAgent`, Axios tries to proxy the request twice.
- **Passing `SocksProxyAgent` to `fetch({ dispatcher })`.** `SocksProxyAgent` implements Node’s `http.Agent`, not `undici.Dispatcher`.
- **Allocating a new agent per request.** Calling `new ProxyAgent(...)` in a route handler forces a new proxy TLS handshake every time and can hit [per-IP concurrent connection limits](https://prontaro.com/docs/billing/limits.md).

## Calling an API through a Relay URL

In edge runtimes or codebases where you cannot configure a proxy agent, create a Relay in the dashboard ([IP addresses](https://my.prontaro.com/ips)) and call it directly with `fetch`:

```ts
const res = await fetch(`${process.env.PRONTARO_RELAY_URL}/v1/status`, {
  headers: {
    Authorization: `Bearer ${process.env.PARTNER_API_TOKEN}`,
    // Include only if you set a password (in the dashboard under IP addresses → Relay address → Access):
    'Prontaro-Relay-Auth': `${process.env.RELAY_LOGIN}:${process.env.RELAY_PASSWORD}`,
  },
  signal: AbortSignal.timeout(15_000),
});
if (!res.ok) {
  throw new Error(`HTTP ${res.status}: ${await res.text()}`);
}
console.log(await res.json());
```

See [Relay URLs](https://prontaro.com/docs/outbound/relay.md) for TLS termination details and limits.

## Next steps

- [curl and environment variables](https://prontaro.com/docs/languages/curl.md) — diagnose `407` and `403` errors with `curl -v`.
- [HTTP & SOCKS5 proxy](https://prontaro.com/docs/outbound/proxy.md) — configure `access` and `targets` allowlists and credential rotation.
- [Static IP for Vercel](https://prontaro.com/docs/integrations/vercel.md) — configure proxy and Relay URLs in Vercel Functions.
- [Static IP for AWS Lambda](https://prontaro.com/docs/integrations/aws-lambda.md) — use a static IP without VPC plumbing or a NAT Gateway.
