Keep Your LLM API Key Out of the Browser: A Backend Route That Decides, Not Forwards
Here's a common first version of an AI feature. A support dashboard gets a "Summarize ticket" button. To ship the prototype quickly, the frontend calls the model provider directly, and the API key goes into .env as VITE_
Here's a common first version of an AI feature. A support dashboard gets a "Summarize ticket" button. To ship the prototype quickly, the frontend calls the model provider directly, and the API key goes into .env as VITE_LLM_API_KEY so the React code can read it.
It works in the demo. It also puts the key in every user's browser. Vite's env variable docs say variables with the VITE_ prefix are exposed in client-side source code after bundling, and they tell you to avoid the prefix to keep values from leaking. Whatever your bundler calls it, the rule holds: anything the browser can read, a user can read too.
This post covers the small backend route that fixes this, what that route should decide on its own, and the trade-offs in where the key lives once it's on a server.
Why "just hide it better" doesn't work
Minifying, splitting the key into pieces or loading it from a config endpoint doesn't change anything. If the browser sends the key to the provider, anyone can open the network tab and copy it. OpenAI's API key safety guide says not to deploy keys in browsers or mobile apps, because others can make requests on your behalf, and recommends routing requests through your own backend.
The cost isn't only a surprise bill. A copied key can send any prompt to any model that key is allowed to use, and those calls show up as yours.
before: browser --(key + any prompt)--> provider
after: browser --(session, task, ticket id)--> your backend
| checks user
| builds prompt
| adds key
v
provider
Make the backend decide, not forward
The tempting fix is a route that takes the browser's request body and passes it on with the key attached. That hides the key but keeps the problem: anyone signed in can still send any prompt to any model. You've built an open relay with a login page.
A better route accepts an intent ("summarize ticket T-1042") and makes every other decision on the server: who the user is, whether they can read that ticket, which pinned model to use, the prompt template and the token limit.
# summarize.py
import logging
import os
import re
TASKS = {
"summarize_ticket": {
"model": "example-model-2026-01-15", # pinned, chosen server-side
"max_tokens": 400,
"prompt": "Summarize this support ticket in three bullets:\n\n{text}",
},
}
def load_key() -> str:
# Injected at deploy time from your secret store. Read once at startup.
key = os.environ.get("LLM_API_KEY")
if not key:
raise RuntimeError("LLM_API_KEY is not set")
return key
class DropSecrets(logging.Filter):
PATTERN = re.compile(r"(Bearer\s+\S+|sk-[A-Za-z0-9_-]{8,})")
def filter(self, record):
record.msg = self.PATTERN.sub("[REDACTED]", record.getMessage())
record.args = ()
return True
def handle(user, body, can_read, load_ticket, call_model, key, log):
if user is None:
return 401, {"error": "sign in"}
if set(body) != {"task", "ticket_id"} or body["task"] not in TASKS:
return 400, {"error": "unsupported request"}
if not can_read(user, body["ticket_id"]):
return 403, {"error": "forbidden"}
task = TASKS[body["task"]]
request = {
"model": task["model"],
"max_tokens": task["max_tokens"],
"messages": [{"role": "user", "content": task["prompt"].format(
text=load_ticket(body["ticket_id"]))}],
}
reply = call_model(request, headers={"Authorization": f"Bearer {key}"})
log.info("task=%s user=%s ticket=%s model=%s", body["task"], user,
body["ticket_id"], reply.get("model"))
return 200, {"summary": reply["text"]}
The handler takes its dependencies as arguments, so you can test it without a web framework or a real provider. Two tests cover the behavior that matters most:
def test_client_cannot_choose_model_or_prompt():
status, _ = call({"task": "summarize_ticket", "ticket_id": "T-1042",
"model": "most-expensive-model"})
assert status == 400
def test_key_never_reaches_logs():
stream = io.StringIO()
handler = logging.StreamHandler(stream)
handler.addFilter(DropSecrets())
log = logging.getLogger("t")
log.addHandler(handler)
log.setLevel(logging.INFO)
log.info("retrying with %s", {"Authorization": "Bearer sk-test-0000000000"})
assert "sk-test" not in stream.getvalue()
call is a small helper that passes fakes for the permission check, ticket loader and provider. The log filter is a backstop, not the plan. The real rule is simpler: never log headers or client config objects. Keep the filter because someone eventually adds a debug line during an outage. Attach it to every handler, since a filter on one handler doesn't protect the others.
Where the key lives on the server
Sources disagree here, and the disagreement is useful. OpenAI's guide recommends environment variables over keys in code, plus a key management service. The OWASP Secrets Management Cheat Sheet points out that environment variables are usually readable by every process and can end up in logs or system dumps, and prefers other injection methods when they're available.
A practical reading:
- Treat a secret manager as the source of truth, with access scoped to the service that needs it.
- If your platform injects the value as an environment variable at deploy time, that's a reasonable last step. Don't bake it into images,
.envfiles in the repo or CI logs. - Use separate keys per environment, so a leaked staging key can't touch production.
- Plan rotation before you need it. OpenAI's guide describes the order: create the replacement, deploy it, confirm it works, then revoke the old key. Where your provider supports short-lived credentials from workload identity, prefer them to long-lived keys.
Limits
- A backend route protects the key, not your budget. Signed-in users can still call the route in a loop, so add per-user rate limits and a token ceiling.
- Ticket text is untrusted input, and it reaches the model. Keeping the key server-side doesn't address prompt injection.
- The filter only catches patterns you thought of. Key formats vary by provider.
Where this fits
The same rule covers every service key your AI feature uses, including keys for tools that record what the feature did. We work on one of those, Obligra Verify, and our page on API key placement and secret storage applies it to Verify keys: trusted server-side runtimes, a secret manager and separate keys per environment.
This article was generated by an AI agent (Claude) for the Obligra Verify team and checked against the linked sources.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.