LeakPatrol › Guides

Where to put your OpenAI API key in a Lovable app

By LeakPatrol · Updated 23 November 2026

If your app calls OpenAI straight from the browser, your key is public. It doesn't matter that it sits in a .env file or a VITE_OPENAI_API_KEY variable: the build copies it into the JavaScript every visitor downloads. Bots scan sites for sk- keys all day, and a leaked key can run up a large bill in hours.

Why VITE_ variables are not secret

Vite (used by Lovable, Bolt and most Cursor starters) replaces every import.meta.env.VITE_… value with the real text at build time. The same goes for NEXT_PUBLIC_ in Next.js. Those prefixes mean "safe to show the world". Supabase's publishable key is meant to go there. An OpenAI, Anthropic, Stripe secret or Resend key is not.

Check your live site in one minute

  1. Open your live site, press F12 and go to Sources (Chrome) or Debugger (Firefox).
  2. Press Ctrl+Shift+F (Cmd+Option+F on a Mac) to search all files, and search for sk-, then OPENAI.
  3. Go to Network, use the AI feature and look for requests to api.openai.com. If the browser talks to OpenAI directly, the key is in the browser.

The fix: call OpenAI from a Supabase Edge Function

Keep the key on the server and let your app call your own function instead:

supabase secrets set OPENAI_API_KEY=sk-...
// supabase/functions/chat/index.ts
import { createClient } from "npm:@supabase/supabase-js@2";

Deno.serve(async (req) => {
  // Only signed-in users can spend your OpenAI credit
  const supabase = createClient(
    Deno.env.get("SUPABASE_URL")!,
    Deno.env.get("SUPABASE_ANON_KEY")!,
    { global: { headers: { Authorization: req.headers.get("Authorization") ?? "" } } }
  );
  const { data: { user } } = await supabase.auth.getUser();
  if (!user) return new Response("Unauthorized", { status: 401 });

  const { message } = await req.json();
  const r = await fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${Deno.env.get("OPENAI_API_KEY")}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "gpt-4o-mini",
      messages: [{ role: "user", content: String(message).slice(0, 4000) }],
    }),
  });
  return new Response(await r.text(), { headers: { "Content-Type": "application/json" } });
});

Then in the app: supabase.functions.invoke('chat', { body: { message } }). In Lovable you can also add the key under the Supabase integration's secrets and ask Lovable to "call OpenAI from an Edge Function, never from the browser".

Two extras that save you when something goes wrong: set a monthly usage limit in your OpenAI project, and give the app its own project key so you can revoke it without breaking anything else. A per-user rate limit in the function (say 30 requests an hour) stops one account from draining your credit.

If the key already leaked

  1. Revoke it now in the OpenAI dashboard (API keys) and create a new one. Deleting it from your code is not enough: old builds and copies keep working until the key is revoked.
  2. Check Usage for spend you don't recognise.
  3. Put the new key only in the Edge Function secret, rebuild and redeploy, then search your live JavaScript again.

A prompt to paste into Lovable, Bolt or Cursor

Move every OpenAI (and other secret API) call out of the browser:
1. Create a Supabase Edge Function that calls OpenAI with
   Deno.env.get("OPENAI_API_KEY"). Only signed-in users may call it.
2. Change the app to use supabase.functions.invoke instead of
   calling api.openai.com directly.
3. Remove VITE_OPENAI_API_KEY and any hardcoded sk- key from the
   code and .env files.
4. Tell me the exact `supabase secrets set` command to run.

After deploying, LeakPatrol's free read-only scan reads the JavaScript your live site loads and flags OpenAI, Anthropic, Stripe and other secret keys, so you can confirm nothing slipped back in. More on this: are VITE_ variables secret? and the API key checker.

Check your app in a minute

Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.

Get the 10-point checklist

Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.

Questions

Is it safe to put my OpenAI key in a VITE_ environment variable?

No. Vite copies every VITE_ value into the JavaScript at build time, so anyone can read it. Keep the key in a Supabase Edge Function secret or another server.

Where does Lovable store secret API keys?

In Supabase Edge Function secrets, set from the Supabase integration or with supabase secrets set. Your app then calls the Edge Function, which calls OpenAI.

How do I know if my OpenAI key is exposed?

Open your live site, press F12, search all JavaScript files for sk- and watch the Network tab for requests to api.openai.com. A free read-only LeakPatrol scan does the same check automatically.

My OpenAI key leaked. Is removing it from the code enough?

No. Revoke it in the OpenAI dashboard first, create a new key, store it only on the server, then redeploy. A copied key keeps working until it is revoked.

More guides