How to protect API keys: keep them out of the browser.

Anything your frontend ships, anyone can read. Here is the short version of how to protect your keys when building with Lovable, Bolt, Cursor or v0.

1. Know which keys are allowed to be public

Some keys are designed to be public: the Supabase anon / sb_publishable_ key, Stripe pk_live_ keys, and referrer-restricted Google Maps keys. Others must never reach the browser: OpenAI sk-, Anthropic sk-ant-, Stripe sk_live_, Supabase service_role / sb_secret_, AWS access keys.

2. Put secret calls behind a server

Call paid APIs from a server route, Supabase Edge Function or serverless function. The browser calls your endpoint; your endpoint holds the key in an environment variable. In Next.js, never prefix a secret with NEXT_PUBLIC_; in Vite, never with VITE_ — those prefixes are bundled into public JS.

3. Turn on Row Level Security

A public anon key is only safe if every Supabase table has RLS enabled with sensible policies. Without RLS, the anon key can read and write your tables.

4. Never commit .env

Add .env* to .gitignore, and make sure your host doesn't serve /.env or /.git/. Removing a key in a later commit doesn't remove it from git history.

5. Limit the blast radius

Set spend limits and usage alerts on AI providers, use restricted/scoped keys, and restrict Google keys by HTTP referrer and API.

6. If a key leaked: rotate first, investigate second

Revoke and replace the key in the provider dashboard immediately, deploy the new key server-side only, then check usage logs and billing for abuse. Deleting the file is not enough — assume it was already copied. Real examples: a stolen Gemini key, a spend limit that capped the damage, and all leaked-key stories.

7. Check what your app actually ships

Open your live site, view the page source and the JavaScript files in your browser's developer tools, and search for key prefixes such as sk-, sk-ant-, sk_live_ and service_role. Do the same in your repository and its git history. You can also ask your AI tool to list every environment variable the client bundle reads. The scan of AI-built repos and the research on our trends page show how often this goes wrong.

8. Mobile apps are not a hiding place

Keys bundled into an iOS or Android app can be extracted from the app file. Treat a mobile app exactly like a website: secret keys live on your server, and the app calls your server.

9. Keys stored in AI tools and agents

Coding agents and AI platforms often store provider keys for you. Give them separate keys with low spend limits, remove keys you no longer use at the provider (deleting them inside the tool does not revoke them), and keep production keys away from agents. See trusting agents.

Frequently asked questions

Is the Supabase anon key safe to put in my frontend?

Yes, it is designed to be public, but only if every table has row-level security turned on with policies that limit what each user can read and write. Without RLS the anon key can read and change your data.

Can I hide a secret key by putting it in an environment variable?

Only if that variable is read on the server. Variables prefixed with NEXT_PUBLIC_ or VITE_ are bundled into the JavaScript sent to every visitor, so anything in them is public.

I deleted the leaked key from my code. Am I safe?

No. Assume the key was already copied. Revoke or rotate it in the provider dashboard, deploy a new key on the server only, then check usage and billing for abuse. Git history keeps old versions of files.

How do I limit the damage if a key is stolen?

Set spend limits and usage alerts with each AI and cloud provider, use scoped or restricted keys where possible, and restrict Google keys to specific APIs and referrers.

Next: common security mistakes in AI-built apps.