How to hide API keys in Lovable, Bolt and Cursor apps
AI app builders make it easy to call OpenAI, Stripe or other APIs straight from the page. The catch: anything that runs in the browser can be read by anyone who visits your site, including your API keys.
The one rule
Secret keys must only be used on a server. That can be a Supabase Edge Function, a Next.js API route or server action, a serverless function on Vercel/Netlify, or any backend. The browser calls your server function, and only the server holds the key.
Some keys are designed to be public: a Supabase anon/publishable key, a Stripe publishable key (pk_), or a Firebase web config. Those are fine in the browser as long as your database rules are correct. Keys like sk_live_, sk-proj-, sk-ant-, AWS secret keys and Supabase service_role are not.
Step 1: find exposed keys
- Open your live site, press F12 to open DevTools, go to Sources (or Debugger) and search all files (Ctrl+Shift+F / Cmd+Opt+F) for
sk_,sk-,keyandsecret. - In your code, search for keys typed directly into files and for env variables with a public prefix (
NEXT_PUBLIC_,VITE_,REACT_APP_,EXPO_PUBLIC_) that hold secrets. - Check that
.envfiles are in.gitignoreand were never committed to GitHub.
Step 2: move the key to a server function
- Lovable: projects connected to Supabase can use Supabase Edge Functions. Ask Lovable to create an edge function for the API call and store the key as a Supabase secret, not in the code.
- Bolt: ask Bolt to move the call into a backend route or a Supabase/Netlify function and read the key from an environment variable set in your hosting dashboard.
- Cursor (Next.js): put the call in a Route Handler (
app/api/.../route.ts) or server action, and readprocess.env.OPENAI_API_KEYwith noNEXT_PUBLIC_prefix.
// app/api/chat/route.ts (runs on the server)
export async function POST(req: Request) {
const { message } = await req.json();
const r = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({ model: "gpt-4o-mini", messages: [{ role: "user", content: message }] }),
});
return Response.json(await r.json());
}
Also add login checks and rate limits to that function, or strangers can still run up your bill through it.
Step 3: rotate the leaked key
Moving the key isn't enough if it was ever public. Old versions of your site and your Git history may still contain it. Create a new key in the provider's dashboard, delete the old one, and put only the new key in your server environment.
Copy-paste prompt for your AI coding tool
Paste this into Cursor, Claude, Lovable, Bolt or whichever AI built your app:
Find every API key or secret used in this project. For each one, tell me whether it is safe to be public (e.g. Supabase anon key, Stripe pk_) or must be secret. Move every secret key out of browser code into a server-side function (API route, server action or edge function) that reads it from an environment variable with no public prefix. Add authentication and basic rate limiting to that function, make sure .env is in .gitignore, and give me a list of keys I need to rotate.
Check your repo in about a minute
Paste a public GitHub repo into KeySnare for a free grade and your top 3 issues, each with a copy-paste AI fix prompt.
Run a free scan →FAQ
Can people really see API keys in my frontend?
Yes. Everything sent to the browser, including JavaScript bundles, can be read with the browser's developer tools.
Is it safe to put my key in an environment variable?
Only if the variable is read on the server. Variables with public prefixes like NEXT_PUBLIC_ or VITE_ are copied into the browser bundle.
Which keys are OK to be public?
Keys designed for browsers, such as the Supabase anon or publishable key, Stripe publishable keys (pk_) and Firebase web config. Their safety depends on your database rules and server checks.
I already leaked a key. What now?
Create a new key, delete the old one in the provider's dashboard, move the new key to server-only code, and check your billing for unexpected usage.