Fix guides

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:

Then try your own AI endpoint while logged out. If it answers, strangers can use it too.

Stop the damage now

  1. 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.
  2. 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.
  3. 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").

After the fix

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