Where to put your OpenAI API key in a Lovable app
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
- Open your live site, press F12 and go to Sources (Chrome) or Debugger (Firefox).
- Press Ctrl+Shift+F (Cmd+Option+F on a Mac) to search all files, and search for
sk-, thenOPENAI. - 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
- 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.
- Check Usage for spend you don't recognise.
- 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.
Plus the odd security tip. Unsubscribe any time.
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
- Supabase RLS policy generator (free)
- Is this API key safe in my frontend? Free key checker
- Security scan MCP server for Claude Code, Cursor and other AI agents
- Supabase October 30 grants change: what it fixes for your app's security, and what it doesn't
- Supabase "42501 permission denied for table": the safe fix (not GRANT ALL)
- Supabase "new row violates row-level security policy": 5 causes and the safe fix
- Supabase service_role key exposed? What to do in the next hour
- Supabase select returns an empty array but the data is there: the RLS fix
- Supabase "Database error saving new user": find the cause and fix it safely
- Supabase Storage upload: "new row violates row-level security policy" fixed
- Supabase "infinite recursion detected in policy for relation" fixed
- Supabase Edge Function "blocked by CORS policy" fixed
- Supabase "Forbidden use of secret API key in browser" (401) fixed
- Supabase "Legacy API keys are disabled" or "Invalid API key": what it means and the fix
- Supabase login or magic link redirects to localhost:3000 (fixed)
- Is your Supabase storage bucket public? What anyone can see, and the fix
- Lovable data exposure study: is your app one of the 24,664?
- Lovable security check: is your Lovable app safe?
- Bolt.new security check: is your Bolt app safe?
- Launch checklist for apps built with Lovable, Bolt and Cursor
- Supabase security check: is your anon key safe? (RLS checker)
- How to check if your API key is exposed on your website
- Are VITE_ env variables secret? How to hide API keys
- Stop users giving themselves your paid plan (Supabase)