What is a proxy server — and why your API key is public without one

The classic story. You built a page that talks to an AI model. You put the key in an environment variable — "it's not in the code, so it's hidden." Then you open your own site, hit F12, the Network tab, find the request — and there's your key, sitting in a header in plain text. Two clicks, no hacking involved.
Anything that reaches the browser is public. Always. And a proxy is exactly the place where a key can actually live.
What a proxy is, in plain words
A proxy server is a middleman that takes a request on someone else's behalf and passes it on under its own name.
Think of a hotel front desk. Guests don't go knocking on staff-room doors: they say what they need at the desk. The clerk decides who to pass it to, adds the internal notes, turns some requests down. From the outside, only the clerk is visible.
For your app that means: the browser doesn't talk to OpenAI directly, it talks to a route of your own like /api/ask. And that route talks to OpenAI.
Forward vs reverse proxy
One word, two nearly opposite things — and the confusion costs people real time.
- A forward proxy sits on the client's side. It's what people mean by "going through a proxy": requests leave under its name, not yours. Corporate filters, office network caches.
- A reverse proxy sits on the server's side, in front of it. The client has no idea what's behind it. That's how nginx, a CDN and load balancers work — and, more relevant to you, your own server route sitting in front of somebody else's API.
In vibe coding it's the second one 99% of the time. "Proxy the key" means "add your own endpoint that calls the API instead of the browser."
Three reasons you personally want one
One — the key. It lives on the server and never reaches the browser. The browser sends "here's the user's question," the server adds the key and goes off to the model. More on the key itself in what is an API key, and on where to keep it in how to store secrets safely.
Two — limits. Without a proxy, anyone who found your key spends your money. With one, you decide: no more than 20 requests an hour per user. That's your own fuse, not somebody else's rate limit.
Three — CORS. Browsers forbid a page from calling other people's domains without permission. Your proxy lives on your own domain, so the request stops being "somebody else's" and the error disappears on its own. This is, incidentally, the honest way to fix CORS rather than papering over it.
What the simplest possible proxy looks like
"Proxy" sounds grand, but the minimal version is one server handler, a dozen lines:
// server: /api/ask
export async function POST(req) {
const { question } = await req.json();
const r = await fetch("https://api.provider.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
// the key comes from the SERVER's environment and never leaves it
"x-api-key": process.env.PROVIDER_KEY,
},
body: JSON.stringify({ model: "…", messages: [{ role: "user", content: question }] }),
});
return Response.json(await r.json());
}
The browser calls /api/ask. The key stays in process.env on the server. That's it.
One trap almost everyone hits: if a variable's name carries a prefix like NEXT_PUBLIC_, VITE_ or PUBLIC_, the bundler deliberately bakes its value into the client bundle. That variable isn't a secret, it's a public setting. A secret must get a name without the prefix.
How to tell whether you need one
A one-question check: does any request leaving the browser carry someone else's key or password? Open F12 → Network, click around your app, look at the headers. If yes — you need a proxy, and it's half an hour of work.
If no, good: you've already split frontend and backend correctly, even if you never called it a proxy.
The one mistake that undoes the whole thing
A proxy doesn't save you by existing. If your /api/ask accepts any text from anyone and forwards it to the model without limits, you haven't hidden the key — you've leased it to the entire internet. The only difference is that the bill now grows through your endpoint instead of through a stolen key.
The minimum that makes a proxy a proxy rather than an open gateway:
- Constrain what can be asked. The model, the token limit and the system part of the prompt are set by the server, not the client. All that comes from the browser is the user's text.
- Put a per-user limit on it. By session, by IP — what matters is that a ceiling exists at all.
- Don't forward blindly. "Take the body from the browser and pass it through as-is" is the same open access with an extra hop.
Three rules, half an hour of work — and the proxy starts protecting your balance, not just your key.
Will a proxy slow my app down?
It adds one network hop — typically tens of milliseconds. Against the seconds a model spends thinking, that's invisible. And streaming passes through your own proxy just fine.
Is a proxy the same as a VPN?
No. A VPN wraps all of a device's traffic in an encrypted tunnel. A proxy is a middleman for specific requests from a specific app. Only the word "middleman" overlaps.
Can I skip the proxy if my site is static?
Then you need some sliver of server code — a serverless function, an edge function, a tiny backend. That sliver becomes your proxy. A static page physically cannot keep a secret.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





