Stripe secret key in your frontend code? How to check and fix it

A Stripe sk_live_ key in client-side code gives anyone full access to your Stripe account. How to find it, rotate it, and move payments to the server.

· 7 min read

You added payments to your app, the checkout button works, and then you spot it: a string starting with sk_live_ in a React component, or a VITE_STRIPE_SECRET_KEY in your .env. Maybe you found it yourself, maybe Stripe emailed you about an exposed key.

Either way, treat the key as public. Every visitor downloaded it with your JavaScript, and a Stripe secret key is not a narrow credential like a map API key. This guide covers how to confirm the leak on the live site, what to do in the Stripe Dashboard first, and how to rebuild the payment flow so no secret ever reaches the browser.

Why a Stripe secret key ends up in the browser

Stripe has three kinds of API keys, and the names are easy to mix up when you are moving fast. According to Stripe's API keys documentation:

  • Publishable keys (pk_live_…, pk_test_…) are meant for client-side code. They can't create charges or read your account data.
  • Secret keys (sk_live_…, sk_test_…) have unrestricted access to your account. They are not safe to expose.
  • Restricted keys (rk_live_…, rk_test_…) only have the permissions you give them, but they are still server-side credentials and not safe to expose either.

AI builders tend to leak the secret key in one of three ways:

  1. A public-prefix variable. Vite inlines every VITE_ variable into the bundle, Next.js does the same with NEXT_PUBLIC_. Naming the key VITE_STRIPE_SECRET_KEY makes the code work and publishes the key in one step. The mechanics are covered in VITE_ env variables are visible in your build.
  2. Creating the Checkout Session in the browser. A Checkout Session has to be created with the secret key. When there is no backend yet, an assistant may call the Stripe API straight from a component to get the button working.
  3. Copying the wrong key. The Dashboard shows the publishable and secret keys next to each other. Pasting sk_ where pk_ belongs is an easy mistake, and the integration may still appear to work.

Stripe is direct about this in its key best practices: secret keys must stay in your server environment, and client tools, SDKs and mobile apps should embed publishable keys instead.

Check it yourself

Check the deployed site, not your repository. The live bundle is what strangers get.

1. Search the files the browser loaded

Open your live site in Chrome or Firefox and open DevTools. On the Network tab, tick "Disable cache" and reload. Go through the pricing page and the checkout flow so lazily loaded scripts arrive too. Then search across all loaded files with Cmd+Option+F (Mac) or Ctrl+Shift+F (Windows) for:

sk_live_
rk_live_
sk_test_
api.stripe.com

What a hit means:

  • sk_live_ is a live secret key. This is the urgent case.
  • rk_live_ is a live restricted key. Less power than a secret key, but whatever permissions you gave it are now public.
  • sk_test_ is a test-mode secret key. No real money is at stake, but it shows the code pattern is wrong and the live key will follow it the day you switch modes.
  • api.stripe.com with no key nearby can still mean the browser calls Stripe directly. Use the checkout once and look at the request in the Network tab.

pk_live_ and pk_test_ are publishable keys. Finding them is expected and fine.

2. Watch the checkout request

With the Network tab open, click your buy button. Look at the first request it sends:

  • If it goes to api.stripe.com/v1/checkout/sessions from the browser, the page is creating sessions with a secret or restricted key.
  • If the request headers contain Authorization: Bearer sk_… or Bearer rk_…, that is the key, readable by anyone.

A correct setup sends the request to your own server or function first, and only then redirects to checkout.stripe.com.

3. Find the source in your code

grep -rnE 'sk_(live|test)_|rk_(live|test)_|STRIPE_SECRET|api\.stripe\.com' \
  --include='*.ts' --include='*.tsx' --include='*.js' --include='*.jsx' \
  --include='.env*' . | grep -v node_modules

Any match in code that runs in the browser, and any VITE_, NEXT_PUBLIC_ or REACT_APP_ variable holding a secret, is part of the leak. For a wider sweep across other providers, see how to find API keys in your JavaScript bundle.

Stop the damage first

Do this before you touch the code. Deleting the line from your component does nothing about copies that were already downloaded.

  1. Rotate the key now. In the Dashboard, open the API keys page, click the menu next to the exposed key and choose to rotate it. Stripe lets you pick when the old key expires. For a leaked key, choose to expire it immediately. A grace period keeps the leaked key working for the attacker too. Stripe's advice is to rotate right away, even if you aren't sure anyone saw it.
  2. Expect a short outage. Anything still using the old key stops working. That's fine: the next section replaces it.
  3. Review recent activity. In the Dashboard, look at payments, refunds, payouts, customers and API request logs for the period the key was live. Watch for refunds you didn't issue, changed payout settings, new webhook endpoints or connected apps you don't recognise, and bulk reads of customer data.
  4. If anything looks wrong, contact Stripe support with the dates and the key's last four characters. Stripe's own page says it notifies you when it detects an exposed key and sometimes deactivates it, but it also says it doesn't catch every case. Don't wait for an email.
  5. If the key was also in a Git repository, a new commit doesn't remove it from history. Rotation is what makes it harmless.

Fix it: payments go through your server

The pattern Stripe documents in its Checkout quickstart is simple:

  1. The browser asks your server (an API route, a serverless function, or a Supabase Edge Function) to start a checkout.
  2. The server creates a Checkout Session with the secret key, read from a server-only variable such as STRIPE_SECRET_KEY.
  3. The server returns or redirects to the session's URL, and Stripe's hosted page takes the payment.

Two details matter as much as moving the key:

  • Decide the price on the server. The browser should send a product or plan identifier, not an amount. If the client can send amount: 1, someone will. Stripe's quickstart recommends using Price IDs set on the server for exactly this reason.
  • Confirm payment with a webhook, not the success page. Anyone can open your /success URL. Grant access or ship orders only when your webhook receives the payment event, and verify its signature with the endpoint's whsec_ secret, as described in Stripe's webhook docs. That secret is server-only too.

Also consider a restricted key for the server, with only the permissions the integration needs. Stripe recommends restricted keys over unrestricted secret keys, so a future leak would expose less.

A prompt for Lovable, Cursor or Claude Code:

My Stripe secret key was exposed in client-side code. I have already
rotated it in the Stripe Dashboard. Fix the payment flow properly:

1. Remove every Stripe secret or restricted key (sk_ / rk_), every
   VITE_ / NEXT_PUBLIC_ / REACT_APP_ variable holding one, and every
   direct call to api.stripe.com from client-side code. The browser
   may only use the publishable key (pk_).
2. Create a server-side endpoint (a Supabase Edge Function if this
   project uses Supabase, otherwise an API route) that creates the
   Stripe Checkout Session using a server-only env variable
   STRIPE_SECRET_KEY.
3. The endpoint accepts only a plan or product identifier from the
   client and maps it to a Stripe Price ID on the server. It never
   accepts an amount or currency from the client.
4. Add a webhook endpoint that verifies the Stripe signature with a
   server-only STRIPE_WEBHOOK_SECRET and grants access or fulfils
   the order only on a successful payment event. The success page
   must not grant anything by itself.
5. List every file you changed and every env variable I need to set,
   and say which ones are server-only.

When it's done, set the new key only in your server or function secrets, deploy, and repeat the three checks above. The search for sk_live_ should find nothing, and the buy button should call your own domain or Supabase project before going to checkout.stripe.com.

Keep it from coming back

This leak tends to return. A later prompt like "the checkout button is broken, fix it" can lead an assistant straight back to calling Stripe from the browser, and the site looks exactly the same either way.

A few habits help:

  • Never give a secret a public prefix. If a variable starts with VITE_ or NEXT_PUBLIC_, assume it's on every visitor's screen.
  • Keep test and live keys in separate server-side secrets, so switching modes doesn't mean pasting keys around.
  • Repeat the DevTools search after any change to payments, not just after the first launch.

ScanYourSite runs that search for you. The free check downloads your page and the scripts it loads and looks for known secret-key formats, including Stripe live secret keys (sk_live_) and live restricted keys (rk_live_), plus OpenAI, Anthropic, AWS, Supabase and others. Keys are shown redacted, as a prefix and the last few characters, with the file they were found in. The free plan checks one site weekly; on Pro, the site is checked after every deploy, so a key that slips back in is flagged the day it ships rather than on your next Stripe statement.

Check your own site in a minute

Free, no sign-up. You get a prioritised list of what’s wrong and a prompt you can paste back into Lovable, Bolt, Cursor or Claude Code.

Free, no sign-up. The basic check makes only the requests an ordinary browser would.

Leaked API keys