Where to put your OpenAI API key in a Lovable app (so nobody can copy it)
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. Bo
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 big 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. Same for NEXT_PUBLIC_ in Next.js. Those prefixes mean "safe to show the world". Supabase's publishable key belongs there. An OpenAI, Anthropic, Stripe secret or Resend key does not.
Check your live site in one minute
- Open your live site, press F12, go to Sources.
- Press Ctrl+Shift+F (Cmd+Option+F on a Mac) and search all files for
sk-, thenOPENAI. - In 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
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 } }).
Two extras: 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 stops one account from draining your credit.
If the key already leaked
- Revoke it now in the OpenAI dashboard and create a new one. Deleting it from your code isn't enough: copies keep working until it's revoked.
- Check Usage for spend you don't recognise.
- Put the new key only in the Edge Function secret, redeploy, and search your live JavaScript again.
A prompt for 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.
The full guide, with FAQs, is on LeakPatrol. If you want an outside check of your live app (leaked keys, public files, tables that answer without a login), there's a free read-only scan there too.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.