Your AI API key is in your frontend. Anyone can spend your money.
Updated 2026-10-13 · Plain English, written for founders who built with Lovable, Bolt, Cursor or similar.
If your app calls OpenAI, Anthropic or another AI API straight from the browser, your API key is in the JavaScript every visitor downloads. Anyone can copy it and send their own requests on your account, to the most expensive model, as fast as they like. Leaked AI keys are found by bots and abused quickly, and the bill is yours.
The same risk exists even when the key is safely on a server, if your server route answers anyone who calls it. An open proxy to OpenAI costs you exactly what a leaked key does.
Check if you're affected
Open your live app and DevTools:
- Network tab: use the AI feature. If you see requests going directly to
api.openai.com,api.anthropic.comorgenerativelanguage.googleapis.com, the key is in the browser. - Search the loaded JavaScript for
sk-proj-,sk-(OpenAI),sk-ant-(Anthropic) andAIza(Google). - Search your code for
VITE_OPENAI,NEXT_PUBLIC_OPENAI,dangerouslyAllowBrowser: trueor similar. Those prefixes and flags exist to put things in the browser.
Then try your own AI endpoint while logged out. If it answers, strangers can use it too.
Stop the damage now
- Revoke the key in the provider's dashboard (OpenAI: API keys; Anthropic: Console, API keys) and create a new one. Do not put the new one anywhere near the frontend.
- Set a spend limit. Both OpenAI and Anthropic let you cap monthly spend for a project or workspace. Set it to a number you could pay without pain.
- Look at the usage page for models you don't use, or traffic at hours your users are asleep.
Fix the code
Move the call to your server. The server checks who is calling, limits what they can ask for, and only then talks to the AI provider. In Next.js:
// app/api/chat/route.ts
import OpenAI from "openai";
import { createClient } from "@/lib/supabase/server";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); // no NEXT_PUBLIC_ prefix
export async function POST(req: Request) {
const supabase = await createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return new Response("Sign in first", { status: 401 });
const { message } = await req.json();
if (typeof message !== "string" || message.length > 4000) {
return new Response("Bad request", { status: 400 });
}
const res = await openai.chat.completions.create({
model: "YOUR-MODEL", // chosen here, never taken from the request
max_tokens: 500, // a cap you chose, never taken from the request
messages: [{ role: "user", content: message }],
});
return Response.json({ reply: res.choices[0].message.content });
}
On Lovable or Bolt with Supabase, do the same in an Edge Function. Store the key with supabase secrets set OPENAI_API_KEY=... and read it with Deno.env.get("OPENAI_API_KEY").
- Never let the browser choose the model or
max_tokens. Otherwise a caller can switch your cheap model for the most expensive one. - Rate-limit per user, for example a few dozen requests an hour on a free plan. Count requests in a table or use your host's rate limiting.
- Free tier without login? Then rate-limit per IP address and keep the spend cap low. Anonymous AI is the most abused feature on the internet.
After the fix
- Reload your app and confirm no request in the Network tab goes to the AI provider directly.
- Call your AI route logged out. It should return 401.
- The old key is still in your git history. That is fine once it has been revoked, and only then.
Check your app
SecureIndie looks for this mistake. The free quick check covers the basics from outside (exposed files, keys in your site's code, headers) in about a minute. Database rules, payment webhooks and AI endpoints need the full audit, which also reads your code and proves what a visitor can actually reach.
Questions
Is VITE_OPENAI_API_KEY or NEXT_PUBLIC_OPENAI_API_KEY safe?
No. The VITE_ and NEXT_PUBLIC_ prefixes exist to copy a variable into the browser bundle. Anything with that prefix is public.
How fast do leaked keys get used?
Automated scanners look for key patterns in public code and bundles all the time. Treat a leaked key as already used, not as something that might be found later.
I moved the call to a server route. Am I done?
Only if that route checks who is calling it and limits how much they can use. An open proxy to OpenAI costs you the same as a leaked key.
Related guides
Checks covered: SEC-008-openai, SEC-009-anthropic, COST-001 · If you think data already leaked