Guides · Free check · $9 fix pack

Guides

Stripe secret key exposed in your frontend or GitHub: what to do now

AI builders sometimes paste the Stripe secret key straight into the code, or into an environment variable the browser can read. Stripe is blunt about what that means: "If an unauthorized party obtains your secret API key, they can make unauthorized charges, access customer data, or disrupt your integration" (Stripe docs: Best practices for managing secret API keys).

Is it actually exposed? A 2-minute check

Rotate first, clean up second

Stripe: "If a restricted or secret API key is exposed or compromised, rotate it immediately even if you aren't sure anyone saw it. Treat any exposure as a potential compromise" (source).

  1. Rotate the key on the Dashboard API keys page: overflow menu (⋯) → Rotate key → choose an expiration (Stripe docs: Rotate an API key). For a key that is already public, choose Now so the old key stops working ("If you choose Now, the old key is deleted", source). The 7-day grace period Stripe offers is for planned rotations without downtime, not for a key strangers may hold. Expect a short outage until the new key is deployed.
  2. Put the new key on the server only: a server-side environment variable or your host's secrets store, with no VITE_ or NEXT_PUBLIC_ prefix, read only from a server route, Edge Function or Worker. Stripe: "Never embed secret API keys in applications" and use publishable keys for client-side code (source).
  3. Prefer a restricted key. "Unlike a secret key that can do anything in your Stripe account, a RAK can do only what you give it permission to do" (Stripe docs: Restricted API keys). A typical Checkout app needs write access to Checkout Sessions (plus whatever your webhook reads), not payouts or customers' payment methods.
  4. Check the request logs for calls you didn't make while the key was exposed (Stripe docs: Request logs), and follow Stripe's support article on compromised keys (Stripe support: Protecting against compromised API keys). Stripe may also notify you or deactivate an exposed key on its own, but "Stripe doesn't guarantee detection of all exposed or compromised keys" (source).

Did the webhook secret leak too?

If the same file or bundle also held your whsec_ signing secret, anyone with it can sign fake "payment succeeded" events that your webhook will accept as real. Roll it: in Workbench → Webhooks, open the endpoint, overflow menu → Roll secret. You "can choose to immediately expire the current secret or delay its expiration for up to 7 days" (Stripe docs: Roll endpoint signing secrets). For a leaked secret, expire it now and update your server's env var. The signing secret belongs on the server only, same as the API key (see test vs live webhook secret).

// Client (browser): publishable key only
const stripe = await loadStripe(import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY); // pk_live_...

// Server route / Edge Function / Worker: restricted or secret key, never shipped to the browser
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);          // rk_live_... preferred
const secret = process.env.STRIPE_WEBHOOK_SECRET;                    // whsec_..., server only

// Never:
// VITE_STRIPE_SECRET_KEY=sk_live_...
// NEXT_PUBLIC_STRIPE_SECRET_KEY=sk_live_...
// const stripe = new Stripe('sk_live_...')  (hard-coded in any file)

Variable names are examples; use your host's secret settings (Vercel, Supabase Edge Function secrets, Cloudflare Worker secrets) for the values.

Prompt for your AI builder

My Stripe secret key was exposed. I have already rotated it in the Stripe Dashboard. 1) Search the whole codebase and config for sk_live_, sk_test_, rk_live_, rk_test_ and whsec_ and remove every hard-coded value. 2) Make sure no Stripe secret or webhook secret is in a VITE_ or NEXT_PUBLIC_ variable or anything bundled for the browser; the browser may only use the publishable key (pk_). 3) Read STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET only in server-side code (API routes, Edge Functions, Workers). 4) Move every Stripe API call that uses the secret key (creating Checkout Sessions, refunds, reading customers) to a server route the frontend calls. 5) Add .env files to .gitignore. 6) Do not change the webhook's signature verification or fulfillment logic.

Re-test

  1. Rebuild and deploy, then search the live JavaScript bundles again for sk_, rk_ and whsec_: no matches.
  2. Run one sandbox Checkout with card 4242 4242 4242 4242 and confirm the webhook still returns 2xx and unlocks the account.
  3. Send an unsigned POST to your webhook URL; it should return 400 (why that matters). The full pre-launch list: webhook security checklist.
Check yours in 10 seconds (free).

Paste your webhook URL. WebhookProof sends 3 harmless test requests and tells you whether Stripe can reach it and whether it rejects unsigned events. If it finds a problem, the $9 fix pack gives you the exact copy-paste fix for your stack (refundable for 14 days).

Run the free check

FAQ

Is it safe to have my Stripe publishable key in frontend code?

Yes. Stripe says only publishable keys (pk_live_ / pk_test_) are safe to expose outside your back end. Secret (sk_) and restricted (rk_) keys must stay on the server.

My Stripe secret key was in a VITE_ or NEXT_PUBLIC_ variable. Is it exposed?

Yes. Vite exposes VITE_ variables in client-side code after bundling, and Next.js inlines NEXT_PUBLIC_ variables into JavaScript sent to the browser. Rotate the key and move it to a server-only variable.

What should I do if my Stripe secret key was pushed to GitHub?

Rotate it immediately in the Stripe Dashboard, even if you are not sure anyone saw it, then check request logs for calls you did not make. Removing it in a later commit does not remove it from history.

Do I need to change my Stripe webhook secret too?

If the whsec_ secret was exposed as well, roll it from the webhook endpoint in Workbench and expire the old one, because anyone with it can sign fake events your endpoint would accept.

Sources

More guides

Stripe webhook not working? Check these 7 things in order · A customer paid but didn't get access. What went wrong? · No signatures found matching the expected signature for payload · Stripe webhook failing with a 307 or 308 redirect

Last reviewed 2026-11-11. WebhookProof is an independent tool, not affiliated with Stripe, Inc.. Quotes are from the linked documentation.