Quick answer: It is safe to expose an API key in the frontend only if its provider made it for that: a Stripe publishable key, a Supabase publishable key with row level security on, or a restricted Firebase or Google Maps key. Every other key, including any OpenAI or Anthropic key, must stay on a server. If one has leaked, replace it first.
Whether it's safe to expose an API key in the frontend depends on which key it is. A few are designed to sit in the browser. Most act as you, on your bill, and anyone who opens your site can copy them.
AI app builders can blur that line. A tool asked to "make it work" may put a secret key where the page can reach it, because that makes the error go away. (Some guard against it: Lovable says it detects a key pasted into its chat and offers to store it properly.) This guide gives you the answer provider by provider, shows how keys end up in the browser, walks through a five-minute check that needs no code reading, and sets out what to do, in order, if a key has leaked. It's check 2 of our eight checks before you launch a vibe-coded app, and part of the wider picture in vibe coding security.
Is it safe to expose an API key in the frontend?
Only when the provider says that key is meant to be public. Mozilla's web documentation states the default plainly: "Unless explicitly permitted by the API vendor's documentation, never, ever, embed API keys in your frontend code. Otherwise, any visitor to your website can extract the API key and abuse it".
The reason is simple. Frontend code is delivered, in full, to every visitor's browser. It can be squashed and renamed, but it can't be hidden. Lovable's own security guide says it in one line: "Secrets stored in frontend code are visible to users and should be considered compromised."
So the question to ask of any key is not "is it hidden?" It's "what can someone do with it?" A public key can only do harmless or tightly limited things. A secret key can spend your money or read your data.
Which API keys are public, and which are secret?
Here is the answer for the services AI-built apps use most, each from the provider's own documentation.
| Provider | Safe in the browser | Never in the browser |
|---|---|---|
| Stripe | Publishable key (pk_…) | Secret key (sk_…) and restricted key (rk_…) |
| Supabase | Publishable key (older name: anon), with row level security on | Secret key (older name: service_role) |
| Firebase | The web API key, if restricted to Firebase services | Any key that can call the Gemini API |
| Google Maps | A browser key restricted to your website and to the Maps APIs it uses | Unrestricted keys; server keys |
| Clerk | Publishable key (pk_test_…, pk_live_…) | Secret key (sk_test_…, sk_live_…) |
| OpenAI | None | Every key |
| Anthropic | None | Every key |
| Resend | None | Every key |
A few of those rows deserve their source.
Stripe publishes a table with a "Safe to expose" column. The publishable key gets a yes: "You can include it in front-end code or applications you distribute." Everything else gets a no: "Only publishable keys are safe to expose outside your application's back end."
Supabase's public key is safe on one condition, which is the subject of its own guide: row level security. Without it, the same key reads every row.
Firebase is the one that surprises people. Its web key is "public by design": it identifies your project, and your security rules do the protecting. But there is a newer exception. Firebase now warns: "never include the Gemini Developer API in the allowlist for a publicly accessible API key". Google's keys all look alike, and a key that was harmless for maps can become a key to a paid AI service if that service is switched on for the same project. In February 2026, Truffle Security reported finding 2,863 live Google API keys on public web pages that also worked against Gemini. By Truffle's account, Google began restricting exposed keys from reaching Gemini that December.
OpenAI, Anthropic and Resend have no public key at all. OpenAI: "Don't share it with others or expose it in any client-side code such as browsers or apps." Anthropic compares its key to a card number: "if someone obtains and uses your API key, they incur charges on your behalf".
If a provider isn't in the table, look in its documentation for the words "publishable" or "public". If you can't find them, the key is a secret.
How do secret keys end up in the browser?
Often through a naming rule the builder never heard of. Web frameworks have a way to mark a setting as "send this to the browser", and Vercel's documentation sums it up: "Frameworks typically use a prefix to expose environment variables to the browser."
| Framework | What sends a setting to the browser |
|---|---|
| Next.js | A name starting with NEXT_PUBLIC_ |
| Vite (used by Lovable projects) | A name starting with VITE_ |
| Create React App | A name starting with REACT_APP_ |
| Expo | A name starting with EXPO_PUBLIC_ |
| Astro, and SvelteKit before version 3 | A name starting with PUBLIC_ |
| Nuxt | Anything under runtimeConfig.public |
The frameworks are honest about what the prefix does. Next.js says such a value "will be inlined into any JavaScript sent to the browser". Vite says prefixed variables "should not contain sensitive information such as API keys". Create React App puts it in capitals: "WARNING: Do not store any secrets (such as private API keys) in your React app!"
Now picture the moment it goes wrong. Your app calls an AI service from a page. It fails, because the key isn't available in the browser, which is the framework protecting you. You paste the error into your AI tool. The quickest way to make that error disappear is to rename the setting with the public prefix. The call works. The key is now in every visitor's browser.
There are three other common routes:
- The key is typed straight into a file. No setting at all, just the key in the code.
- The key is saved into the project's history. More on that below.
- The key is pasted somewhere it gets copied. Resend's guide lists "a screenshot, a chat message, an issue tracker, or a third-party tool" alongside code, and GitGuardian's 2026 report says about 28% of incidents originate in collaboration and productivity tools.
The first of those is the shortest path there is, from a prompt to a stranger's hands:
One nuance for Lovable users, because it runs against the usual advice. Lovable asks you not to hide the .env file from your repository: "Lovable needs .env committed to the repository so build-time VITE_* values are available". That's fine, because in a Lovable project that file should only ever hold VITE_ values, which are public anyway. Real secrets go in Lovable's Secrets manager, where they "never reach the browser".
How do you check your app for exposed keys?
You don't need to read code. You need to search it, in three places.
In your live site (five minutes). Open your app in a browser and open its developer tools (right-click the page and choose Inspect in most browsers). Use the tools' search across all loaded files (ask your AI tool where it is in your browser), and look for each of these:
| Search for | What it would be |
|---|---|
sk_live_ or rk_live_ | A live Stripe secret or restricted key (Clerk secret keys also start sk_live_) |
sk-proj- or sk-ant- | Patterns secret scanners use for OpenAI and Anthropic keys |
sb_secret_ | A Supabase secret key. An older one starts eyJ, the same as the older public key, so ask your AI tool which of the two it is |
AKIA | An Amazon Web Services access key |
ghp_ or github_pat_ | A GitHub access token |
xoxb- | A Slack bot token |
BEGIN PRIVATE KEY | A private key file |
AIza | A Google key. Often fine (Maps, Firebase), so check what it's allowed to call |
Finding pk_live_ or sb_publishable_ is expected, and a Google key may be fine. Finding any of the others in your live site means a stranger can find it too.
In your project (five minutes). Ask your AI tool to list every setting your app reads and say whether each one reaches the browser. Then read the names yourself. Any name with a public prefix from the table above should be a value you'd be happy to print on a poster.
In your repository. If your code is on GitHub in a public repository, GitHub already scans it: "Secret scanning runs automatically for free" on public repositories, across "your entire Git history on all branches". Check the repository's security alerts, and don't read an empty list as a clean result: some findings are reported straight to the provider and "aren't displayed in your repository alerts". GitHub also blocks many secrets before they land, a feature that has been on by default for pushes to public repositories since February 2024. A private repository on a free personal account doesn't get that protection.
What should you do if an API key has leaked?
Work in this order. The order matters more than the speed of any one step.
- Replace the key at the provider. Create a new one, switch your app to it, and delete or revoke the old one. This is the fix. Stripe's advice: "rotate it immediately even if you aren't sure anyone saw it. Treat any exposure as a potential compromise."
- Close the hole it leaked through. Resend puts this well: "Replacing a key without closing the leak means the new key is exposed the same way." Move the call that uses the key behind a server route, so the browser talks to your server and your server talks to the provider.
- Check what the key was used for. Look at the provider's usage or activity page for requests you don't recognise, and keep a note of what you find. OpenAI's steps say the same: delete the key, "review your API usage for unexpected activity and keep the details", then contact support.
- Set a spending limit. OpenAI and Anthropic both let you cap spend. Do it now, while you're in the dashboard.
- Only then, tidy the history. And know that it's often unnecessary.
That last point runs against instinct, so here is GitHub's own guidance: "as a first step you need to revoke and/or rotate that secret. Once the secret is revoked or rotated, it can no longer be used for access, and that may be sufficient to solve your problem." A dead key in your history is a string of characters. A live key you've deleted from one file is still a live key.
Why isn't deleting the file enough?
Because version history keeps everything, and copies of it spread. GitHub lists where a removed secret can still be found after you rewrite history: "in any clones or forks of your repository", "directly via their SHA-1 hashes in cached views on GitHub" and "through any pull requests that reference them".
Truffle Security tested how far that goes, and it cemented their view that "the only way to securely remediate a leaked key on a public GitHub repository is through key rotation".
And old keys tend to stay alive. GitGuardian tracked secrets it had confirmed as working in 2022, and four years later 64% were still active in public repositories. They had never been replaced.
Will the provider catch a leaked key for you?
Sometimes, and only in some places. It's a safety net with large holes, not a plan.
| Provider | What it says it does |
|---|---|
| OpenAI | "When OpenAI detects an API key on the public internet, or leaked inside an app in an app store, the API key is disabled immediately." |
| Anthropic | When GitHub finds a key in a public repository, "Anthropic automatically deactivates the exposed API key" and emails you |
| Stripe | Notifies you and asks you to rotate; "in some cases, Stripe deactivates the key proactively" |
| Amazon Web Services | Attaches a quarantine policy that blocks a list of costly actions. The key itself keeps working for everything else |
Three limits to keep in mind. Anthropic's and Amazon's steps are described for keys found in public GitHub repositories, where GitHub's scanning notifies the provider "so they can take action, such as revoking the credential"; neither says what happens to a key sitting in a website's code. Stripe says outright that it "doesn't guarantee detection of all exposed or compromised keys". And many services don't say they do anything.
How quickly are leaked keys found?
Fast, where it has been measured most often: cloud keys pushed to public GitHub repositories. Security teams have run the experiment several times by publishing a decoy key and timing what happens.
| Who ran it | When | What happened to the decoy key |
|---|---|---|
| Comparitech | 2020 | "It took just one minute for attackers to find and start abusing the exposed AWS secret key." |
| Orca Security | 2023 | "We saw key usage on GitHub within 2 minutes" |
| Palo Alto Networks Unit 42 | 2023 | The attacker used the keys "within five minutes of their initial exposure on GitHub" |
Be careful what you take from those. All three used Amazon Web Services keys on public GitHub, and in two of them Amazon's own quarantine landed within minutes as well. We couldn't find the same test for a key sitting in a small website's code, or for an AI provider's key, so we can't tell you a number of minutes for your case.
What we can say is that keys in websites can be found at scale. A 2026 study by researchers at Stanford and other universities analysed 10 million websites and found 1,748 credentials for 14 providers in their pages. Of those found in JavaScript, 62% appeared only in the built files a browser receives, the same place the five-minute check above looks, and the authors found that these credentials "often persist for months to years". (It's a preprint; we've read its summary, not the full paper.)
The practical reading: assume a leaked key has been copied, whether or not you can see misuse yet.
How do you use a secret key safely?
Keep it on a server, and have the browser ask the server. That's the whole pattern, and Mozilla describes it in two sentences: "your server communicates with the third-party API using the API key, while your user communicates with your own server. The API key only lives on your server."
The builders we checked each have a place for this.
- Lovable: the Secrets manager. Values are "injected into your Edge Functions automatically, and never reach the browser".
- Bolt: Secrets, used by server functions, which "run behind the scenes on Bolt's servers rather than in the user's web browser".
- Replit: the Secrets tool, which "stores and encrypts secrets" and hands them to your app as settings.
- Vercel and Netlify: environment variables, with the option to mark one as a secret so nobody can read it back from the dashboard. On Vercel that hides the value from your teammates; whether it reaches the browser is still decided by its name. Netlify goes a step further and fails the build if a value you marked secret turns up in the built files.
Here is a prompt to hand your AI builder:
List every setting and API key my app uses. For each one, tell me whether
the provider documents it as public (publishable) or secret, and whether my
code sends it to the browser.
For every secret that reaches the browser: move the call behind a server
route or edge function, store the key in this platform's secrets manager,
and remove any public prefix from its name. Never fix an error by adding a
public prefix to a secret.
Give me a list of every key that was exposed, so I can replace each one at
its provider. Don't tell me it's fixed; tell me what to search for in my
live site to confirm it.
Three habits that prevent the next leak:
- One key per job. OpenAI suggests "separate API keys for each feature, team, product, or project". When one leaks, you replace one.
- The smallest key that works. Stripe's restricted keys and Resend's "Sending access" keys can only do part of what a full key can. Resend's reasoning: such a key "can't read your contacts even if it leaks".
- A cap on every paid service. A limit turns a disaster into an annoyance.
What can't a search or a scanner tell you?
- Whether a public key is restricted properly. A Google key in your page may be fine or may open a paid service. You have to look at what it's allowed to call.
- Whether a key leaked somewhere other than code. A chat, a screenshot, a contractor's laptop.
- Whether a leaked key was used. Only the provider's activity log shows that.
- That nothing is exposed. A search finds the patterns it's given. A key in an unusual format won't match.
A SafeLaunch scan automates the searching part: from outside your live site, it looks for keys in the JavaScript your site sends to every visitor and in any public source maps, and for secret files like .env or .git/config that can be downloaded. It doesn't read your repository, so a key committed there is yours to find with the steps above. It shows where each one is, with the key itself hidden, and gives you the prompt to fix it. Replacing the key is still yours to do, because only you can sign in to the provider. How we verify an app lists what the scan covers, and the exposed API keys issue page shows examples.
If a leaked key gave access to customer payment details or personal data, get professional advice on what you're required to tell people. That's a legal question, and it depends on where your customers are.
Frequently asked questions
Is it safe to put an API key in frontend code?
Only if the provider documents that key as public, such as a Stripe publishable key or a Supabase publishable key with row level security on. Any key the provider calls secret must stay on a server, because everything in frontend code is delivered to every visitor's browser and can be read there.
What should I do first if my API key is leaked?
Replace it at the provider: create a new key, switch your app to it and revoke the old one. Then close the hole it leaked through, check the provider's activity log for requests you don't recognise, and set a spending limit. Cleaning the key out of your code history comes last and is often unnecessary.
Is the Supabase anon key safe to expose?
Yes, as long as row level security is enabled with correct policies on every table. Supabase designed the publishable key (formerly the anon key) to be public. The secret key (formerly service_role) is never safe to expose, because it bypasses those rules.
Is the Firebase API key a secret?
Not usually. Firebase says its web API keys are public by design and that security rules protect your data. The exception is any key that can call the Gemini API: Firebase says that key should never be in your code. Check which services your key is allowed to call.
Does deleting a key from GitHub remove it?
No. The key stays in the repository's history, and in any copies or forks made before you removed it. GitHub's own guidance is to revoke or rotate the secret first, and says rewriting history may then not be needed at all.
Will OpenAI or Stripe disable a leaked key automatically?
Sometimes. OpenAI says it disables a key immediately when it detects one on the public internet. Stripe says it notifies you and in some cases deactivates the key. Neither promises to catch every leak, so replace the key yourself as soon as you know.
What is the difference between a publishable key and a secret key?
A publishable key identifies your account and can do only limited, safe things, so it can sit in a web page. A secret key can act as you: spend money, read data, change settings. Stripe's table of key types marks the publishable key "safe to expose" and the others not.
How do I hide an API key in a React or Next.js app?
You don't hide it in the browser. You move it. Store the key as a setting without a public prefix (no NEXT_PUBLIC_, VITE_ or REACT_APP_), call the provider from a server route or edge function, and have your page call that route.
Sources
- Third-party APIs, MDN Web Docs
- API keys and Best practices for managing secret API keys, Stripe documentation
- Learn about using and managing API keys for Firebase, Firebase documentation, and Google Maps Platform security guidance, Google
- API keys, Supabase documentation, and Clerk environment variables, Clerk documentation
- Keeping your OpenAI account secure and API reference: authentication, OpenAI
- API key best practices, Anthropic
- How to handle a leaked API key, Resend documentation
- Environment variables, Next.js; Env variables and modes, Vite; Adding custom environment variables, Create React App; Environment variables, Expo; Using environment variables, Astro
- Secret scanning, Push protection and Removing sensitive data from a repository, GitHub documentation
- Keeping secrets out of public repositories, GitHub, February 2024
- AWSCompromisedKeyQuarantineV3, AWS Managed Policy Reference
- Secrets and Security best practices, Lovable documentation; Secrets, Bolt; Secrets, Replit
- Keys on Doormats: Exposed API Credentials on the Web, Demir, Vekaria, Smaragdakis and Durumeric, 2026
- The State of Secrets Sprawl 2026, GitGuardian
- Anyone can access deleted and private repository data on GitHub and Google API keys weren't secrets. But then Gemini changed the rules, Truffle Security
- CloudKeys in the Air, Palo Alto Networks Unit 42; 2023 Honeypotting in the Cloud Report, Orca Security; GitHub honeypot, Comparitech