Skip to content
ScanAnt

OpenAI API key leaked: how to revoke it and stay safe

Short answer

Delete the key in the OpenAI platform dashboard and create a new one, because anyone who holds it can spend your money. Check the usage page for requests you did not make, set a monthly budget, and move the new key to a server endpoint so the browser never sees it. The same steps apply to Anthropic, Groq and other AI provider keys.

Why you got this

AI keys are among the most abused leaks because stolen access is immediately useful: attackers run their own workloads on your account or resell access, and the bill arrives with you. Bots scan public repositories and front-end bundles for strings that match known key formats.

Builders such as Lovable, Bolt and Cursor often generate code that calls the AI provider straight from the browser, which requires the key to be in public JavaScript. If the key is visible in the page sources, it is already out.

What to do now

  1. Open the OpenAI platform, go to API keys and delete the exposed key. Create a replacement with a clear name, and if your account supports project-scoped keys, use a separate project for this app.

  2. Check the Usage dashboard for spikes or models you never call. Compare it with your normal traffic.

  3. Set a monthly budget and usage limits for the project, so a future leak has a ceiling. Also set an alert so you hear about it early.

  4. Store the new key as a server environment variable, remove it from the repository and the front end, and redeploy.

How to fix it properly

Call the AI provider from your own backend. The browser talks to your endpoint, and your endpoint holds the key. This route also lets you require login and limit requests per user.

ts
// app/api/chat/route.ts
import OpenAI from 'openai';

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

export async function POST(req: Request) {
  // 1. check the user is signed in
  // 2. apply a per-user rate limit
  const { message } = await req.json();
  const r = await client.chat.completions.create({
    model: 'gpt-4o-mini',
    messages: [{ role: 'user', content: String(message).slice(0, 2000) }],
    max_tokens: 500,
  });
  return Response.json({ text: r.choices[0].message.content });
}

Cap the input length and max_tokens as shown, so one request cannot be expensive. Without a login check and a rate limit, a public endpoint becomes a free AI service for strangers, so add both.

Never prefix the variable with NEXT_PUBLIC_ or VITE_, since those are copied into the browser bundle. Enable secret scanning in your repository to catch future mistakes.

How to check it is fixed

Search the built JavaScript and repository for sk- strings. Confirm the old key now returns an authentication error and the new one works only from the server.

The free ScanAnt scan looks through the JavaScript your site ships for OpenAI, Anthropic, Groq and similar keys and reports them masked, never storing them whole. It also spots AI endpoints that anyone can call, though it never sends an AI request itself unless you separately allow that on a verified site. It cannot see keys held only in private code.

Questions

Will OpenAI revoke a leaked key for me?

Some providers detect keys on public GitHub and disable them, but do not count on it. Delete the key yourself.

Can I limit damage if it leaks again?

Yes: project-scoped keys, a monthly budget, alerts and a server-side proxy with login and rate limits.

Is it safe to call OpenAI from the browser with a limited key?

Any key in the browser is public, so a limit only caps the loss. A server endpoint is the proper design.

Do I still pay for requests an attacker made?

Usually the account owner is billed. Contact the provider's support and explain what happened, but act as if you will pay.

Check your site after the fix

The quick scan is free for any public site and needs no signup. Checks that touch your database or files run only after you prove you own the site.

Choose a scan level →