How to rotate a leaked Stripe key

If your Stripe secret key ended up in your code, your repo or your browser code, replace it now. Here is how, step by step, without breaking your checkout for long.

VibeWarden · Updated · 3 min read

The short answer

In the Stripe Dashboard, open the API keys page, find the leaked key and choose Rotate key. Set the old key to expire now, put the new key in your server's secrets and redeploy. Then check your Stripe logs for activity you did not cause.

Which Stripe key leaked?

Stripe gives you several keys. Look at how the leaked one starts:

  • sk_live_ is your live secret key. It can charge cards, issue refunds and read customer data. Rotate it now.
  • rk_live_ is a restricted key. It can do only what you allowed when you made it. Rotate it now.
  • whsec_ is a webhook signing secret. It lets your app trust events from Stripe. Roll it in Workbench: open Webhooks, choose the endpoint, open the menu (the three dots) and choose Roll secret.
  • pk_live_ is a publishable key. It is meant to be public, so it needs no action.

How do I rotate the key?

You need to be an Administrator, Developer or Super Administrator on the Stripe account. Have your hosting provider's secrets screen open in another tab first.

  1. Sign in to the Stripe Dashboard, make sure you are in live mode, and open the API keys page.
  2. Find the leaked key. Open the menu (the three dots) at the end of its row and choose Rotate key.
  3. Choose when the old key expires. If the key was public, choose now.
  4. Copy the new key straight away. Stripe shows a live secret key only once.
  5. Paste it into your host's secrets, replacing the old value, and redeploy your app.
  6. Make a small test purchase, or watch the next real one, to check that payments work.

Where do I put the new key?

Put it only in your server's secrets. Never put it in your code, and never in a variable the browser can read, such as one that starts with VITE_ or NEXT_PUBLIC_.

Then ask your AI builder to check that the code reads the key the right way:

Prompt for your AI builder
I rotated my Stripe secret key. Check that the code reads it only from the STRIPE_SECRET_KEY environment variable, only in server code, and never in code sent to the browser. Check that no Stripe secret key (starting sk_live_, sk_test_ or rk_live_) is written anywhere in the project. Show me what you would change before you change it.

How do I check whether someone used the old key?

Stripe keeps a log of the API requests made to your account. Open the request logs in Stripe's Workbench and look for requests you did not make, from times or places you do not expect.

Then check the parts of Stripe a thief would touch:

  • refunds and payouts you did not start;
  • a bank account or payout schedule that changed;
  • new webhook endpoints, team members or API keys you did not add;
  • customers or charges you do not recognise.

If you find any, contact Stripe support and say your key leaked.

How do I stop it leaking again?

Use a restricted key with only the permissions your app needs, so a leak can do less harm. Keep every secret in your host's secrets, and scan your repo for keys before each launch. Our guide on finding leaked keys shows how.

Common questions

Will rotating the key break my live site?
If the old key expires now, payments fail until your app uses the new key. Have your host's secrets screen open before you rotate, and the gap is a few minutes.
Do I need to rotate my publishable key?
No. Publishable keys start with pk_ and are made to be public. They cannot charge cards or read your data on their own.
The leaked key was a test key. Do I still need to rotate it?
Yes, but it is less urgent. A test key cannot move real money, but it can read and change your test data.