Almost every app built with an AI builder uses keys. A key is a long password your app uses to prove who it is to another service. It looks like a long random string, and the builder often asks you to paste it somewhere.
The most useful single rule in app security is knowing which keys may be public and which may not. This post gives you the rule, with examples from the services founders use most.
The rule
- A public key is made to be seen by everyone, and it is limited by other rules.
- A secret key is a master password for your account on that service.
Public keys can sit in code that runs in the browser (the program people use to open websites, such as Chrome or Safari). In other words, they can sit in the part of your app that every visitor downloads, where they can look at it. Secret keys cannot, because anyone using the browser can read everything in it.
Supabase
Supabase is a service that keeps your app's data in a database. Its documentation lists two kinds of key.
The publishable key (the newer form of the anon key) is designed to be exposed in web pages and mobile apps, because Row Level Security limits what it can reach. Row Level Security, or RLS, is the database's own set of rules about who may read or change each record.
The secret key (the newer form of service_role) has elevated privileges and bypasses RLS.
- It belongs only in backend code you control, the part of your app that runs on the service's computers, where visitors cannot look.
- Its variable (the named setting that holds it) must not carry the
NEXT_PUBLIC_prefix, because that prefix copies a setting into the code every visitor downloads. - If it leaks, Supabase says to rotate it once the cause is fixed. To rotate is to make a new key and turn the old one off.
- Supabase suggests a separate secret key for each backend component, so one leak means replacing one key.
Stripe
Stripe takes payments for your app. Its documentation says:
- Publishable keys (
pk_) are safe to include in front-end code, the part visitors download. - Secret keys (
sk_) have unrestricted permissions on all Stripe APIs and must not be exposed. (An API is an address that programs call to get or change data.) - Restricted keys (rk_) are recommended for server-side code, since they let you limit the damage if one leaks. A restricted key can do only the jobs you allow.
Webhook signing secrets are not API keys, but they are secret too. A webhook is a message Stripe sends to your app, and the signing secret proves the message really came from Stripe.
AI providers and email services
Keys for AI models, email senders and SMS services are almost always secret keys. If the key can spend money or send messages as you, it is secret. If the browser calls these services directly with your key, anyone who opens your page can copy it and use it on your bill.
How the prefix decides
- In Next.js, a popular tool for building websites, variables starting with
NEXT_PUBLIC_are built into the browser code. - In Vite, another tool that builds apps (used by Lovable and Bolt), the prefix is
VITE_. The Vite documentation says such variables should not contain sensitive information such as API keys.
A secret key with either prefix has been published.
The reverse is also worth knowing. If a key is public by design, such as a Stripe publishable key, putting it behind a prefix is correct, and hiding it on a server would only break your checkout page. The goal is not to hide everything. It is to put each key where its own documentation says it belongs.
A quick test for any key
- Ask the service's documentation: is this key safe to expose in a browser?
- If the docs do not say it is safe, treat it as secret.
- Search your published page source for the first few characters of the key.
- Confirm
.envfiles are not in your repository. A.envfile is a settings file where an app keeps its keys, and a repository is the folder that holds your app's code and its history.
What a public key still needs
A public key is safe only because something else limits it. For Supabase, that something is your Row Level Security rules. For Stripe, it is the fact that a publishable key cannot create charges or read account data.
If your rules are missing, a public key can reach more than you meant. That is why checking the rules matters as much as checking the keys.
Three short examples
A Stripe secret key in a VITE_ variable
A founder pastes a Stripe key starting sk_live into a Vite variable named VITE_STRIPE_KEY so the checkout button works. The key is now in every visitor's browser. The fix is a server-side function, a small piece of code that runs where visitors cannot look. It creates the checkout session, with the key stored as a server secret.
A Supabase publishable key with RLS on
A founder puts a Supabase publishable key in the front end and turns RLS on for every table. That is the intended design.
A Supabase secret key to make a page load
A founder gives a Supabase secret key to the front end so a stubborn page will load data. It loads, because the key skips every rule, and so it will for everyone. The right fix is a correct policy (one database rule), not a stronger key.
Why the difference exists
Services separate these keys because a browser must be able to talk to them without handing over the keys to the kingdom.
- The public key lets the page say "this is app X". The service then applies the rules you configured, such as your database policies, to decide what that visitor can do.
- The secret key skips the visitor-level rules because a trusted server uses it for admin work.
Keeping the two apart is what makes the whole design hold together. When an AI builder mixes them up, the app usually still works, which is exactly why the mistake survives until someone else finds it.
What to do about a key that is in the wrong place
Create a replacement, move the call that uses it to server-side code, and turn the old key off.
GitHub's documentation says to rotate affected credentials immediately. It also notes that scrubbing Git history (the saved record of every change to your code) is slow and often unnecessary once the credential has been revoked.