DEV Community

Ikkun
Ikkun

Posted on Originally published at agentjournal.dev

Hide API keys behind a Cloudflare Worker

Every client that talks to an API ends up holding a key, and every copy of that key is another place it can leak. The pattern I use to avoid that: hide the key behind a Cloudflare Worker on the free tier. The client calls the Worker. The Worker adds the real key server-side and forwards the request to the upstream API. The client never sees the key, and the Worker URL is the only credential it needs.

The problem: keys multiply until one leaks

It starts with one script and one key. Then the key is in an environment file, then in a config, then in a notebook, then in a chat log because someone needed to debug quickly. Each copy is a separate leak surface, and keys leak exactly once — after that, the damage is done. My credential hygiene improved a lot once I stopped distributing keys at all.

How the proxy works

The mechanics are simple. The client sends its request to the Worker URL. The Worker takes the request, attaches the real API key from its own environment, and forwards it to the upstream API. The response comes back through the Worker to the client.

From the client's point of view nothing changes except the base URL. From the key's point of view, it never leaves the Worker. There is no token in any request the client makes, so there is nothing for a logs dump, a browser extension, or a misconfigured proxy to grab.

Why the free tier is enough

Two requirements drove this choice: zero maintenance cost, and keys never exposed to the client. The Cloudflare Worker free tier satisfies both. There is no server to patch, no process to supervise, and no bill while traffic stays within the free allowance. The Worker is a small piece of code with one job, which is the kind of thing that keeps working for months without attention.

This fits the rest of my setup, which leans on free tiers wherever the workload is light. The pattern is not limited to Cloudflare either: if the upstream API is Google-adjacent, a Google Apps Script web app can do the same thing at zero cost. The shape is what matters — a thin proxy that owns the secret — not the specific host.

Rotation without touching clients

The quiet win of this pattern is key rotation. When a key needs to rotate, I change it in exactly one place: the Worker's environment. Every client keeps using the same Worker URL and keeps working. No redeploy of clients, no hunting through repos for the old key, no guessing which environment still holds the stale value.

Rotation is the moment most key management fails, because it is rare enough that nobody practices it. A pattern where rotation is a one-line change is a pattern that actually gets rotated. That is worth more than any amount of ceremony around the key itself.

Where the pattern stops fitting

The proxy is not a universal answer. If the client itself has to sign requests or the upstream API expects the key in a place the Worker cannot intercept cleanly, the pattern adds indirection without removing exposure. I also do not use it for short-lived experiments where the key never leaves my own machine anyway — the cost of standing up a Worker is not worth it there.

It is also not a substitute for treating keys as replaceable. A leaked key behind a proxy is still a leaked key; the proxy just shrinks the number of places that can happen. I rotate on suspicion, and because rotation is a one-line change here, I actually do it.

This is the same principle I apply to credentials everywhere else: the fewer places a secret lives, the fewer places it can be stolen from. The Worker is my narrowest possible surface.

If you already proxy your agent traffic through a gateway or a custom endpoint, have you checked whether the keys behind it stay server-side, or are they visible to the client?


Originally published at agentjournal.dev.

Top comments (0)