Launch sale: 20% off Fix Pack, Deep Audit and Monitor through Nov 30, 2026. The scan stays free. See sale prices →
Home / Guides / Exposed OpenAI API key: what to do

Exposed OpenAI API key? What to do in the next 10 minutes

OpenAI's own guidance is blunt: never deploy your key in client-side environments like browsers or mobile apps, because anyone can take it and make requests on your behalf, which can mean unexpected charges or access to account data. If yours is exposed, work through these steps in order.

Scan your live app free  Passive, no signup, never touches your database.

1. Rotate the key now

In the OpenAI platform, open the API keys page, create a replacement key, then revoke the exposed one. OpenAI's help center says to rotate immediately if you believe a key has leaked. Revoking breaks anything still using the old key, so update your server's environment variable right after.

2. Check usage for anything you didn't do

Open the Usage page and look for traffic that doesn't match your app. If you see misuse, OpenAI suggests contacting them through help.openai.com.

3. Set spend limits

OpenAI recommends monthly spend limits for your organization or projects, with hard enforcement turned on. Its docs note that alerts alone don't stop API traffic, and that spend may slightly exceed the limit because enforcement isn't instantaneous.

4. Move OpenAI calls to your server

Call the API from a server route, edge function or serverless function that reads OPENAI_API_KEY from an environment variable and checks the caller (signed-in user, rate limit). The browser calls your endpoint, never OpenAI directly.

// Next.js route handler: app/api/chat/route.js (server only)
export async function POST(req) {
  const user = await getUser(req);            // your auth check
  if (!user) return new Response("Unauthorized", { status: 401 });
  const { message } = await req.json();
  const r = await fetch("https://api.openai.com/v1/responses", {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, "Content-Type": "application/json" },
    body: JSON.stringify({ model: "YOUR_MODEL", input: String(message).slice(0, 4000) }),
  });
  return new Response(r.body, { status: r.status });
}

5. Don't use a public env prefix

NEXT_PUBLIC_OPENAI_API_KEY or VITE_OPENAI_API_KEY ships the key to every visitor. Use plain OPENAI_API_KEY and read it only on the server.

6. Reduce the blast radius next time

OpenAI recommends a separate key per team member, project keys with an expiration date plus a rotation routine, permissions on individual keys, and IP allowlisting where your backend has fixed addresses.

7. Check the leak is really gone

Rebuild and redeploy, then run Unspill on your live URL. It looks for OpenAI (sk-proj-), Anthropic and other AI keys in the JavaScript your site serves. If the key was committed to git, it's still in history, which is why rotating comes first.

Fix guides for these issues

FAQ

Is it enough to delete the key from my code?

No. Anyone who already copied it can keep using it, and it may remain in git history or cached bundles. Revoke it in the OpenAI dashboard first.

Can I hide the key with obfuscation or base64?

No. Anything the browser can use, a visitor can extract. The key has to live on a server.

Does the same apply to Anthropic, Groq or other AI keys?

Yes. Treat every AI provider key as a server-side secret, and rotate and move it the same way.

Sources

Last checked against these sources on 2026-10-19. Platforms change; when in doubt, the official docs win.

Want help fixing this?

We're opening a done-for-you fix service, a deep audit (database rules, auth, user-to-user data access) and continuous monitoring. Join the waitlist to get first access. No spam, and we never share your email.