Stripe .env Generator
Stripe ships three keys per environment — publishable, secret, and webhook signing. This generator produces a .env block with the right prefixes (sk_test_, pk_test_, whsec_) and comments so you don't accidentally mix test and live values.
STRIPE_SECRET_KEY=sk_test_ STRIPE_PUBLISHABLE_KEY=pk_test_ STRIPE_WEBHOOK_SECRET=whsec_ STRIPE_PRICE_ID_PRO=price_
Secrets regenerate on every page load. Want to combine stacks? Use the full generator.
How to use this .env in Stripe
- 1.Paste the block into your project's .env or .env.local.
- 2.Swap the test-mode prefixes for their live-mode equivalents only in production.
- 3.Set STRIPE_WEBHOOK_SECRET to the value Stripe shows when you create the webhook endpoint — it's unique per endpoint.
- 4.Never prefix STRIPE_SECRET_KEY with NEXT_PUBLIC_ or VITE_ — it would ship to the browser.
Gotchas
- ⚠Webhook signing secrets are per-endpoint, not per-account. Each webhook URL gets its own whsec_.
- ⚠Rotating a secret key invalidates it immediately — coordinate with any long-running jobs first.
- ⚠Stripe's CLI uses a separate restricted key; add STRIPE_API_KEY for local forward runs if needed.
Common keys explained
STRIPE_SECRET_KEYServer-only. Test-mode value in dev, live-mode value only in prod.
STRIPE_PUBLISHABLE_KEYSafe to expose to the browser. Prefix with NEXT_PUBLIC_ or VITE_ if needed.
STRIPE_WEBHOOK_SECRETUnique per webhook endpoint. Used by stripe.webhooks.constructEvent().
STRIPE_PRICE_ID_PROOptional but convention for tying price IDs to env.
Loading and validating the .env in Stripe
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: Request) {
const body = await req.text(); // raw body, not JSON
const sig = req.headers.get("stripe-signature")!;
const event = stripe.webhooks.constructEvent(
body,
sig,
process.env.STRIPE_WEBHOOK_SECRET!, // whsec_... for THIS endpoint
);
if (event.type === "checkout.session.completed") {
// fulfil the order
}
return new Response("ok");
}Stripe: env vars in development, staging and production
Stripe gives every account two separate sets of keys. Test mode uses sk_test_/pk_test_ and live mode uses sk_live_/pk_live_. Keep test keys in development and staging, and put live keys only in production. Data does not cross between modes: products, prices and customers created in test mode do not exist in live mode, so STRIPE_PRICE_ID_PRO differs per environment too.
Each webhook endpoint has its own signing secret. The stripe listen CLI prints a temporary whsec_ for local development. The Dashboard endpoint for production has a different one. Using the wrong secret is the number-one cause of failed webhook verification.
Stripe: .env errors and fixes
No signatures found matching the expected signature for payloadEither STRIPE_WEBHOOK_SECRET belongs to another endpoint (CLI vs Dashboard), or the body was parsed as JSON before verification. Pass the raw request body.
No such price: 'price_…'The price ID comes from the other mode. Test-mode price IDs do not exist with a live key. Create the product in live mode and update STRIPE_PRICE_ID_* in production.
You did not provide an API keySTRIPE_SECRET_KEY is empty where the Stripe client is created, often because the code runs in the browser. Only the publishable key may be used client-side.
Stripe .env FAQ
Which Stripe keys are safe in frontend code?
Only the publishable key (pk_test_/pk_live_). The secret key and restricted keys (rk_) must stay on the server.
Should I use a restricted key instead of the secret key?
Yes for services that only need part of the API. A restricted key (rk_live_) limited to the resources you call reduces the damage if it leaks.
How do I test webhooks locally?
Run stripe listen --forward-to localhost:3000/api/stripe/webhook, copy the whsec_ it prints into .env as STRIPE_WEBHOOK_SECRET, and trigger events with stripe trigger checkout.session.completed.