Public key vs secret key: which can go in your app?

Published 2026-11-16 by Nordik. Written with AI from the sources listed at the end.

The short version

The risk
A secret key in the part of your app that every visitor downloads lets anyone act as you on that service. A public key there is normal.
Does it affect you?
Yes, if your app uses Supabase, Stripe, an AI provider or an email service, which covers almost every app built with an AI builder.
What to do
Sort each key into public or secret using the service's own documentation, keep every secret key on the server, and replace any secret key that reached the browser.

Rather not read it all? Copy this for your AI tool. Paste it into the tool you build with. It asks the tool to check your app for the problems in this guide and explain what it finds in plain words. Nothing is changed.

See the prompt
Check where my app's keys live and do not change anything yet. List every API key and environment variable the app uses. For each one, tell me the service, whether that service's documentation calls it a public key (safe in a browser) or a secret key, and whether it ends up in code that every visitor downloads, for example through a VITE_ or NEXT_PUBLIC_ prefix. Flag every secret key that reaches the browser, with the file name, and explain each answer in plain words.
Jump to a part (10)
  1. The rule
  2. Supabase
  3. Stripe
  4. AI providers and email services
  5. How the prefix decides
  6. A quick test for any key
  7. What a public key still needs
  8. Three short examples
  9. Why the difference exists
  10. What to do about a key that is in the wrong place

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 .env files are not in your repository. A .env file 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.

Sources

Each source below was accessed on 2026-11-16. Documentation changes over time, so check the source for the current wording.

  • Supabase documentation, API keys, accessed 2026-11-16
  • Stripe documentation, API keys, accessed 2026-11-16
  • Vite documentation, Env variables and modes, accessed 2026-11-16
  • Next.js documentation, Environment variables, accessed 2026-11-16
  • GitHub documentation, About secret scanning, accessed 2026-11-16

See what your live app shows strangers

Paste the link to your app: in under a minute the free scan shows what your live app hands to any visitor, and where to look first. No sign-up, and it never signs in or changes anything.

Run a free scan

Want the code itself read? Order the $49 audit or see a sample report. Your card is held and charged only when your report is delivered. It is not a pentest: the audit is done by AI, and a second AI checks each serious finding.