Skip to content
NASHSOFTWARE SERVICES
Writing

Published For buyers

Are Your API Keys Visible in the Browser? How to Check

Some keys are meant to be in the browser and some must never get there. How to tell which is which, and how to check what your app sends to visitors.

Mike Nash · Fractional CTO and independent code reviewer

Some of them probably are, and that's fine. A few keys are designed to sit in the browser: the Supabase publishable (or anon) key and the Stripe publishable key, starting pk_, are the usual ones. The problem is a secret key reaching the browser, such as a Supabase secret or service_role key or a Stripe key starting sk_ or rk_, because whoever finds one can act as your server.

Here's how to check, without reading much code. Only do this on an app you own or are authorised to test.

Which keys are which

Most apps built with AI tools talk to two or three outside services, and each one hands out a public key and a secret one.

For Supabase, the publishable key (sb_publishable_..., or the older anon key) is safe in the browser as long as row level security is doing its job. The secret key (sb_secret_..., or the older service_role key) skips row level security entirely. Supabase's docs say never to put it in a browser, even when you're testing on your own machine.

For Stripe, the publishable key (pk_live_ or pk_test_) is meant for the front end. Secret keys (sk_live_, sk_test_) and restricted keys (rk_live_, rk_test_) are for your server only. A live secret key can create charges and refunds and read your account's data.

For anything else, such as an email service or an AI model provider, assume the key is secret unless that provider's own docs say it's safe to publish. If an AI key leaks, someone else runs up your bill.

How keys end up in the browser

Two frameworks cover most of these apps, and both decide what goes to the browser by the name of the setting.

In Next.js, anything named NEXT_PUBLIC_... is copied into the JavaScript sent to visitors when the app is built. Settings without that prefix stay on the server.

In Vite, which sits underneath many React apps, anything named VITE_... ends up in the code visitors download. Vite's docs say plainly that these shouldn't hold API keys.

So a setting called NEXT_PUBLIC_STRIPE_SECRET_KEY or VITE_SUPABASE_SERVICE_ROLE_KEY is a leak however it's used. The name decides it.

The other route is a key pasted straight into the code, which AI tools sometimes do when you hand them a key in a prompt to get something working.

Check 1: your settings

Open wherever your environment variables live: a .env file in the project, and the settings page on your hosting. List every one that starts NEXT_PUBLIC_ or VITE_. For each, ask what it is. A project URL, a publishable key or an analytics ID is expected. Anything with "secret", "service", "private" or "sk_" in its name or value isn't.

Check 2: search the code

Search the whole project for these strings:

  • sk_live and sk_test
  • rk_live and rk_test
  • sb_secret
  • service_role
  • whsec_ (a Stripe webhook signing secret)

A match in a server-only file that reads the value from an environment variable is normal. A match with the actual key written out, or a match in any file that runs in the browser, isn't. If you're not sure which files run in the browser, ask your developer or your AI tool to show you, and ask it to explain its answer.

Also check the .env file isn't committed to your repository. Projects started from the standard Next.js template ignore .env files from the outset, but that only helps if nobody has changed it since.

Check 3: look at what the browser actually receives

This is the check that settles it, because it looks at the live site rather than the code.

  1. Open your live site in Chrome, Edge or Firefox.
  2. Press F12 to open the developer tools.
  3. Press Ctrl+Shift+F (Cmd+Option+F on a Mac) to search across every file the page loaded.
  4. Search for sk_live, sk_test, rk_live, sb_secret and service_role in turn.
  5. Repeat on the pages behind sign-in, and on any admin pages.

Then open the Network tab, reload, and click a few requests to your database or API. Look at the request headers. A Supabase request carrying the publishable key is expected. One carrying a secret key means the browser holds it.

Older Supabase keys are long strings starting eyJ, so you can't tell anon from service_role by looking. Compare what you find with the two keys shown in your Supabase dashboard under the project's API key settings.

If you find a secret key

Moving it isn't enough. Once a key has been in the browser or in a repository, assume someone has a copy.

  1. Create a replacement key with the provider. Stripe lets you rotate a key from the API keys page, and the old one can keep working for up to 7 days while you switch over. Supabase asks you to rotate a leaked secret key once you've fixed the cause.
  2. Put the new key in a server-only setting, without a public prefix.
  3. Move whatever code used it onto the server.
  4. Turn the old key off.
  5. Check the provider's logs for requests you don't recognise.

For Stripe, a restricted key with only the permissions your app needs limits the damage if it ever leaks again.

What this doesn't tell you

A clean result means no secret key is sitting in the browser. It doesn't mean the server code using those keys is safe: a server route that runs with the Supabase secret key and trusts whatever the browser sends can still hand out other people's data. That side of it is in checking row level security in Supabase, and if you take payments, in what to check before you take payments.

Keys and secrets exposed to the browser is one of the checks in the Launch Check on the code audit for AI-built software page, alongside who can read which records and how payments are confirmed.

Official documentation

Share