# How to rotate a leaked Stripe secret key without downtime

Canonical: https://caelo.sh/guides/rotate-stripe-secret-key
Published: 2026-11-27 · Updated: 2026-11-27 · Arthi Arumugam, Caelo by Irth Labs

**Short answer:** In the Stripe Dashboard, open the API keys page, choose Rotate key on the secret key and pick an expiry. Stripe says both keys work for up to 7 days, and choosing Now deletes the old key at once. For a leak, choose Now or the shortest window you can deploy in, update your server, then review recent activity.

## Where does a Stripe secret key usually leak?

- Pasted into frontend code or behind a public prefix such as `NEXT_PUBLIC_STRIPE_SECRET_KEY` or `VITE_STRIPE_SECRET_KEY`.
- In a committed `.env` file or a config file. Stripe says: "Don't put keys in source code or configuration files checked into version control."
- In a screenshot, a support thread or a chat with an AI assistant.

## The vulnerable pattern and the fix

Vulnerable: the browser calls Stripe with the secret key

```ts
// src/checkout.ts, bundled for the browser
await fetch("https://api.stripe.com/v1/checkout/sessions", {
  method: "POST",
  headers: { Authorization: "Bearer " + import.meta.env.VITE_STRIPE_SECRET_KEY },
  body: new URLSearchParams({ mode: "payment", "line_items[0][price]": priceId }),
});
```

Fixed: the server creates the session, the browser only gets a URL

```ts
// app/api/checkout/route.ts, runs on the server
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST() {
  const session = await stripe.checkout.sessions.create({
    mode: "payment",
    line_items: [{ price: process.env.STRIPE_PRICE_ID!, quantity: 1 }], // price set on the server
    success_url: "https://example.com/thanks",
  });
  return Response.json({ url: session.url });
}
```

The browser only ever needs the publishable key (`pk_live_...`), and with Checkout it does not even need that. Setting the price on the server also stops a user from editing it in the request; our payments guide covers that and webhook checks.

## Rotate it, step by step

1. In the Stripe Dashboard, open Developers, then API keys, and choose Rotate key on the secret key.
2. Pick an expiry from the Expiry dropdown. For a leak, choose Now if you can deploy right away, or the shortest window you need. Stripe says both keys work for up to 7 days.
3. Put the new key in your host's environment (for example as a Vercel Secret variable) and redeploy. Never paste it into code.
4. If you need more than 7 days, Stripe says to create a new key manually, migrate to it, then expire the old one.
5. Consider replacing the secret key with a restricted key (`rk_live_...`). Stripe describes restricted keys as API keys with permissions you control, to "limit the damage" if one is stolen, and now recommends them over secret keys for new use cases.
6. If your webhook signing secret (`whsec_...`) leaked too, roll it on the webhook endpoint. Stripe lets you expire it immediately or after up to 7 days, with both secrets active in between.
7. Review recent payments, refunds, payouts and API request logs in the Dashboard for anything you did not do.

## Will Stripe notice the leak?

Sometimes. Stripe says that if it detects an exposed secret or restricted key, it notifies you and asks you to rotate it, and "in some cases" deactivates it proactively. Stripe also says it does not guarantee detection of all exposed keys, so rotate on your own the moment you know.

## Check it automatically

Caelo flags Stripe live secret keys (`sk_live_`) and live restricted keys (`rk_live_`) written in any file of your project, prints only a redacted form, and skips documentation placeholders such as `sk_live_xxxx` (rules `secrets.stripe-secret` and `secrets.stripe-restricted`). It does not flag test keys, and it does not read git history, where an old key can still sit.

Part of the real report from the demo app in the Caelo README

```text
- Stripe live secret key in source
    .env:2  (secrets.stripe-secret)
    value: sk_l…cdEF (43 chars)
    why: Matches the Stripe live secret key format.
```

- [Install Caelo: npx caelo . or the MCP server](/)
- [How Caelo compares with gitleaks, TruffleHog, Semgrep and others](/compare)

## Check your own app

1. Run the free local check: `npx caelo .` Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and `.env` files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Follow the fix in the report and rerun Caelo. For an untracked, unignored file containing secrets, preview the US$19 quick fix. https://caelo.sh/fix
3. For an engineer's assessment, book the US$490 App Health Check. Irth Labs delivers a report and fixed repair quote, with credit toward the repair. https://www.irthlabs.com/start?service=app-health-check

## Questions

### Is the Stripe publishable key a leak?

No. Stripe says to embed publishable keys in client-side code. It can only do what a browser checkout needs.

### Does rotating the API key break my webhooks?

No. The webhook signing secret is separate from your API keys. Roll it only if it leaked as well.

### Do leaked test keys matter?

Less, because test mode moves no real money, but they still give access to your test data and settings. Rotate them too. Caelo only flags live keys.

## Sources

- [Stripe docs: API keys (rotating keys and restricted keys).](https://docs.stripe.com/keys)
- [Stripe docs: API keys best practices.](https://docs.stripe.com/keys-best-practices)
- [Stripe docs: Webhooks (rolling the signing secret).](https://docs.stripe.com/webhooks)
- [Stripe support: Protecting against compromised API keys.](https://support.stripe.com/questions/protecting-against-compromised-api-keys)
- [Caelo source: every rule in lib/rules.js.](https://github.com/arthi-arumugam-git/caelo/blob/master/lib/rules.js)

## Related guides

- [Can users get my AI-built app's paid features for free? Checking Stripe payments](https://caelo.sh/guides/stripe-webhook-security-ai-built-app)
- [I leaked an API key or the Supabase service_role key. What do I do now?](https://caelo.sh/guides/leaked-api-key-what-to-do)
- [Security checklist for apps built with Lovable, Bolt, Cursor, Replit or v0](https://caelo.sh/guides/vibe-coded-app-security-checklist)
- [Are NEXT_PUBLIC_ environment variables secret? What leaks, and how to fix it](https://caelo.sh/guides/next-public-env-variables-secrets)
- [Committed your .env file to GitHub? What to do, in order](https://caelo.sh/guides/env-file-committed-to-github)
