OpenAI API Key Exposed in the Browser: Fix and Prevent
Updated 2026-10-17
AI features are the fastest way to leak a key: the first working prototype often calls OpenAI straight from React. Leaked LLM keys are attractive because they convert directly into someone else's compute bill.
Proxy with auth and limits
Call OpenAI from a server route that reads the key from a server-only env var, checks the user's session, caps input size, and rate-limits per user. Return only what the UI needs.
Contain spend
Revoke the exposed key, use project-scoped keys, and configure usage limits and alerts in the provider dashboard so a future leak has a ceiling.
Checklist
- Key revoked and replaced
- Calls proxied through an authenticated route
- Per-user rate limit
- Input length caps
- Provider usage limits and alerts
Copy-paste fix prompt
Move all OpenAI calls from client code into a server route that requires a logged-in user, limits each user to N requests per minute, truncates prompts to a safe length, and reads OPENAI_API_KEY from a server-only env var. Update the client to call the route.FAQ
Can I obfuscate the key instead?
No. Anything the browser can use, an attacker can extract. Obfuscation only delays discovery.
Is a key in a mobile app or desktop app safer?
Not meaningfully. Binaries can be inspected; use a server proxy there too.
Check your deployed app in ~25 seconds
Free passive scan: leaked keys, Supabase exposure, public config files and headers.
Run a free scan