# Static IP for Cloudflare Workers

Source: https://prontaro.com/docs/integrations/cloudflare-workers/

Call IP-allowlisted APIs directly from Cloudflare Workers with built-in fetch() — no npm dependencies and no proxy client required.

Cloudflare Workers run in V8 isolates across rotating Cloudflare IP ranges and cannot use traditional `ProxyAgent` or `HTTP_PROXY` libraries over direct TCP sockets. Instead, you bind a personal Relay URL (`https://relay-<uuid>.prontaro.run`) to the partner API host, store it with `wrangler secret put`, and let Prontaro forward each request from your dedicated static IPv4.

## Why Relay URL is the right fit

| Approach in Cloudflare Workers | Works with built-in `fetch()` | npm dependencies | Outbound IPv4 |
| --- | --- | --- | --- |
| **Direct `fetch()`** | Yes | None | Rotating Cloudflare pool |
| **HTTP CONNECT proxy** | No (no `ProxyAgent` over direct TCP sockets) | — | — |
| **Prontaro Relay URL** | Yes | None | Your dedicated `/32` |

> **TLS behavior with Relay URLs**
>
> Relay decrypts TLS on the Prontaro server and opens a new HTTPS connection to the target API from your static IPv4.
>
> If a partner requires end-to-end mTLS with a client certificate, run that call from a Node/Go service using [HTTPS proxy](https://prontaro.com/docs/outbound/proxy.md).

## Prerequisites

- A Prontaro account on Dedicated (from $12/mo, 100 GB traffic) or Pro ([plans](https://prontaro.com/docs/billing/plans.md)) with a dedicated IPv4.
- A Cloudflare Workers project with `wrangler` (`npx wrangler`).

## Step 1. Create a Relay URL and enable `Prontaro-Relay-Auth`

Add your dedicated IPv4 (`/32`) to the partner API allowlist, then create a Relay URL bound to the partner host and protect it with a login and password.

The Prontaro server verifies and strips the `Prontaro-Relay-Auth` header before forwarding the request, so your upstream `Authorization: Bearer ...` header reaches the partner API untouched.

1. Open **IP addresses** in [my.prontaro.com](https://my.prontaro.com/ips) and click **Add relay address**.
2. Enter the target host `api.example.com` and copy the generated `https://relay-....prontaro.run` URL.
3. Open the Relay’s **Access** tab, add a password named `cf-worker`, and copy the `Prontaro-Relay-Auth` value (`<login>:<password>`).

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

## Step 2. Save secrets with `wrangler secret put`

Store the Relay URL, `Prontaro-Relay-Auth` credentials, and partner API token as encrypted Worker secrets:

```bash
npx wrangler secret put PRONTARO_RELAY_URL
# Paste: https://relay-3f9c2a7e-5b1d-4c8a-9e60-1d2b3c4d5e6f.prontaro.run

npx wrangler secret put PRONTARO_RELAY_AUTH
# Paste: cf-worker:s3cr3tPass

npx wrangler secret put PARTNER_API_TOKEN
# Paste: tok_partner_example
```

You can also set the same secrets in the Cloudflare dashboard:

**Workers & Pages → Your Worker → Settings → Variables and Secrets**

- `PRONTARO_RELAY_URL`: `https://relay-3f9c2a7e-5b1d-4c8a-9e60-1d2b3c4d5e6f.prontaro.run`
- `PRONTARO_RELAY_AUTH`: `cf-worker:s3cr3tPass`
- `PARTNER_API_TOKEN`: `tok_partner_example`

For local testing with `npx wrangler dev`, add these three variables to `.dev.vars` in `KEY=value` format (and keep `.dev.vars` in `.gitignore`).

## Step 3. Call the API with standard `fetch()`

Zero npm packages required — construct `new URL(path, env.PRONTARO_RELAY_URL)` and send both headers:

```ts
// src/index.ts
export interface Env {
  PRONTARO_RELAY_URL: string;
  PRONTARO_RELAY_AUTH: string;
  PARTNER_API_TOKEN: string;
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    if (!env.PRONTARO_RELAY_URL || !env.PRONTARO_RELAY_AUTH) {
      return Response.json(
        { error: 'PRONTARO_RELAY_URL and PRONTARO_RELAY_AUTH are required' },
        { status: 500 },
      );
    }

    // Path and query string are forwarded to https://api.example.com unchanged
    const upstreamUrl = new URL('/v1/status?env=prod', env.PRONTARO_RELAY_URL);

    const upstream = await fetch(upstreamUrl, {
      method: request.method === 'POST' ? 'POST' : 'GET',
      headers: {
        // Verified and stripped by the Prontaro server before forwarding
        'Prontaro-Relay-Auth': env.PRONTARO_RELAY_AUTH,
        // Reaches api.example.com untouched
        Authorization: `Bearer ${env.PARTNER_API_TOKEN}`,
        'Content-Type': 'application/json',
      },
      body: request.method === 'POST' ? await request.text() : undefined,
      signal: AbortSignal.timeout(15_000),
    });

    return new Response(upstream.body, {
      status: upstream.status,
      headers: {
        'content-type': upstream.headers.get('content-type') ?? 'application/json',
      },
    });
  },
};
```

Deploy the Worker:

```bash
npx wrangler deploy
```

## Step 4. Verify the outbound IP

To verify your Cloudflare Worker exits from your dedicated static IPv4, create a temporary Relay for `api.ipify.org`, call it from your Worker, and remove it right after:

1. Open **IP addresses** in [my.prontaro.com](https://my.prontaro.com/ips) and click **Add relay address**.
2. Enter the target host `api.ipify.org` and copy the generated temporary `https://relay-....prontaro.run` URL.

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

Call `new URL('/?format=json', relayIpifyUrl)` from a temporary check route and invoke your deployed Worker:

```
 
{"ip":"203.0.113.10"}
```

Confirm the address matches your dedicated IPv4 in [my.prontaro.com](https://my.prontaro.com/ips), then delete the temporary Relay:

1. Open **IP addresses**, find the temporary Relay for `api.ipify.org`, and click **Delete**.

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

## Cloudflare Workers notes and limits

- **Enable Smart Placement for lower latency:** add `[placement] mode = "smart"` in `wrangler.toml` so Cloudflare runs your Worker closer to your Prontaro IP location and target API.
- **One Relay per target host (`https://...:443`):** if your Worker talks to two partner domains, create two Relay URLs and store them in separate secrets (`PRONTARO_RELAY_PAYMENTS`, `PRONTARO_RELAY_CRM`).
- **HTTP/1.1 only:** WebSockets and `Upgrade` requests are not supported over Relay URLs.
- **Fast rotation if leaked:** add a new password login and delete the old one in the Relay’s **Access** tab, or delete and recreate the Relay under [IP addresses](https://my.prontaro.com/ips), then update the secret with `wrangler secret put`.

## Common errors

| Symptom | Cause | Fix |
| --- | --- | --- |
| `401` or `407` from Relay | Missing `Prontaro-Relay-Auth` header or trailing whitespace in the secret. | Store `PRONTARO_RELAY_AUTH` as `login:password` using `wrangler secret put`. |
| Partner API returns `401 Unauthorized` | Used HTTP Basic in `Authorization` instead of `Prontaro-Relay-Auth`, overwriting the partner token. | Send Relay credentials in `Prontaro-Relay-Auth` and keep the partner token in `Authorization`. |
| Cross-host redirect disappeared | Relay strips the `Location` header pointing to another domain and adds `Prontaro-Relay-Notice`. | Point your Relay at the final API host, or create a separate Relay for the second domain. |
| Secret is `undefined` in `wrangler dev` | `wrangler secret put` only populates deployed Workers. | Put local development values in `.dev.vars` at the project root. |

## Next steps

- [Relay URLs](https://prontaro.com/docs/outbound/relay.md) — how Relay URLs work, password protection, and limits.
- [Static IP for Vercel](https://prontaro.com/docs/integrations/vercel.md) — static IP setup for Vercel Functions and Edge Runtime.
- [Static IP for Supabase](https://prontaro.com/docs/integrations/supabase.md) — outbound calls from Edge Functions and access to locked-down Postgres.
- [Reliability, zero-downtime updates, and BGP failover](https://prontaro.com/docs/outbound/failover.md) — how your `/32` stays intact during BGP failover.
