API Keys and Secrets: Keeping Them Safe
Anything you ship to a browser or phone can be read. Here is how to tell a public key from a secret one, where secrets belong, and the five-step plan for a leaked key.

Key takeaways
- Every file your app sends to a browser or phone is readable by its users, so a secret key in frontend code is a published key.
- Publishable keys are designed to be public; secret keys belong only in server code, loaded from an encrypted secrets store.
- If a secret leaks, rotate it first, then investigate. Deleting the commit does not un-leak it.
An API key is a password for a program. It lets your app use a service (payments, AI, maps, email) and tells that service whose account to bill. Some keys are designed to be public; secret keys must only ever live on a server, in an encrypted secrets store, read by server-side code. The rule that keeps you safe is short: anything shipped to a browser or a phone can be read by the people using it, so a secret must never be in frontend code, even in a "hidden" variable.
Why "hidden" in the frontend is not hidden
A web app is a bundle of files the browser downloads and runs. Anyone can open developer tools, look at those files and search for strings that start with `sk_` or `sk-`. Minifying the code makes it harder for a person to read, not for a search. Mobile apps are the same: the app package can be unpacked and inspected. Automated scrapers scan public websites and code repositories for key patterns continuously, so a leaked key can be found and abused quickly.
The damage depends on the key. A leaked AI key can run up a large bill before you notice. A leaked payment secret key can create refunds or read customer data. A database master key exposes every row of every user.
Public keys vs secret keys
Many services give you a pair, and it pays to recognize them:
- Stripe: `pk_live_...` is publishable and can sit in your frontend. `sk_live_...` is secret and server only. The webhook signing secret (`whsec_...`) is also server only.
- Supabase: the public (anon) key is meant for the frontend and grants only what row level security allows. The service role key bypasses every rule and must never leave the server.
- Maps and similar browser services: often use keys that are public by design but should be restricted to your domain in the provider's dashboard, so nobody can reuse them elsewhere.
- AI, SMS and email providers: almost always secret. There is no safe way to call them straight from a browser with your key.
Where secrets should live
- 01In a secrets store: an encrypted place your hosting platform provides. Server code reads the value as an environment variable at run time.
- 02Not in your code, not in a committed `.env` file, not in a screenshot, not in a chat with a teammate. Secrets in git history stay there even after you delete the line.
- 03Only on the server side of the line. A server function calls the service with the key and returns just the result to the app.
One trap catches many people: frameworks embed environment variables with certain prefixes into the frontend bundle on purpose. In Vite projects, anything starting with `VITE_` is baked into the browser code at build time; in Expo it is `EXPO_PUBLIC_`; in Next.js, `NEXT_PUBLIC_`. Those prefixes mean "public". Putting a secret behind one publishes it.
How the safe version works
Say your journaling app has a "Suggest a title" button that uses an AI service.
- Unsafe: the browser calls the AI provider directly with the key in the request. Anyone can copy the key from the network tab.
- Safe: the browser calls your own server function `suggest-title` with the user's session. The function checks the user is signed in and under their daily limit, reads the key from secrets, calls the provider and returns only the suggested title.
The safe version also lets you add the protections a key alone cannot: per-user rate limits, a spending cap, logging. Read more in edge functions explained.
One key per job, per environment
A single all-powerful key used everywhere is convenient until something goes wrong. A few habits shrink the blast radius:
- Separate test and live keys. Stripe and many others give you both. Previews and experiments use test keys, so a mistake there can never move real money.
- Restricted keys where offered. Stripe, for example, lets you create keys limited to specific actions. A function that only creates checkouts does not need permission to issue refunds.
- One key per app or service. If your shop and your newsletter tool share a key, rotating it after a leak breaks both. Separate keys can be rotated independently, and usage logs show which app made which calls.
- Know who can see them. Limit dashboard access to the people who need it, and rotate the keys a departing collaborator could have copied.
Write down, somewhere private, which keys exist, which service each belongs to and where it is used. When a provider emails you about suspicious activity, that list turns a panic into a five-minute fix.
If a key leaks: the five-step plan
- 01Rotate it now. Create a new key in the provider's dashboard, put it in your secrets store, and revoke the old one. Do this before anything else.
- 02Check usage. Look at the provider's logs and billing for activity you do not recognize, and contact their support if you see abuse.
- 03Find how it leaked. Frontend code, a public repository, a shared screenshot, a log file?
- 04Remove it from the source. Move the call to the server. Note that deleting a commit does not remove a key from copies people already pulled, which is why rotation comes first.
- 05Add guardrails. Spending limits on the provider's account, domain restrictions on public keys, and a scan before each release.
Secrets in a Startbase app
In Startbase Cloud, Backend → Secrets stores API keys for your backend, encrypted at rest. Edge functions read them as environment variables, and they are never exposed to client-side code. Secrets are write-only: the list shows names and when each was last updated, values are masked, and nobody can read a saved value back, including you. Names that start with `VITE_`, `EXPO_PUBLIC_` or `NEXT_PUBLIC_` are rejected there, because those are client-side variables. Secrets starting with `SUPABASE_` are provisioned and rotated by Startbase and marked "Managed by Startbase". See the Secrets docs.
Two more safety nets help. Every publish runs a security check that looks for leaked keys (common AI provider, AWS, Stripe and GitHub key formats, private keys and the Supabase service role key), committed `.env` files and localhost URLs, and shows findings with a Fix button that sends them to the agent (pre-publish scan docs). Downloading your code as a zip also leaves out environment and key files. And for some features you may not need a key at all: the App AI gateway lets your app call AI, send platform email and start Stripe checkouts through its helper library without keys of its own. For the wider picture, see is AI-generated code safe.
Audit my project for secrets. List every API key or token the code uses, say whether each is public or secret, and move any secret out of frontend code into an edge function that reads it from Secrets. Tell me which keys I should rotate because they were ever in frontend code, and add a per-user daily limit to the function that calls the AI service.
For the wider picture of keeping an app safe, work through the app security checklist.
Frequently asked questions
Is it safe to put an API key in my frontend code?
Only if the provider designed that key to be public, such as a Stripe publishable key or a Supabase anon key protected by row level security. Secret keys must stay on the server.
Are environment variables secure?
On a server, yes, when they come from an encrypted secrets store. In frontend frameworks, variables with public prefixes like VITE_ or NEXT_PUBLIC_ are copied into the browser bundle, so they are public.
What should I do if I pushed a key to GitHub?
Rotate the key immediately in the provider's dashboard, then check its usage logs. Removing it from the repository afterward is good hygiene, but the old key must be treated as compromised.
What is key rotation?
Replacing a key with a new one and revoking the old one. Do it whenever a key may have leaked, when someone with access leaves, and on a regular schedule for sensitive keys.

