Stripe · Next.js · Supabase
Verify Stripe webhook signatures (Next.js and Supabase Edge Functions)
An unverified webhook lets anyone fake a payment and unlock your paid plan. Verifying the raw body in Next.js route handlers and Supabase Edge Functions, plus idempotency and what not to trust.
Why it matters
A webhook endpoint is a public URL. If it doesn't check Stripe's signature, one curl request with a made-up checkout.session.completed event upgrades any account. Supabase Edge Functions that receive webhooks must be deployed without JWT verification so Stripe can call them, which makes the signature the only gate.
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 req.json()
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(body, req.headers.get("stripe-signature") ?? "", process.env.STRIPE_WEBHOOK_SECRET!);
} catch {
return new Response("Invalid signature", { status: 400 });
}
if (event.type === "checkout.session.completed") {
const session = await stripe.checkout.sessions.retrieve(event.data.object.id);
if (session.payment_status === "paid") { /* grant access, once per event.id */ }
}
return new Response("ok");
}import Stripe from "npm:stripe";
const stripe = new Stripe(Deno.env.get("STRIPE_SECRET_KEY")!);
const crypto = Stripe.createSubtleCryptoProvider();
Deno.serve(async (req) => {
const body = await req.text();
try {
const event = await stripe.webhooks.constructEventAsync(
body, req.headers.get("stripe-signature") ?? "",
Deno.env.get("STRIPE_WEBHOOK_SECRET")!, undefined, crypto,
);
// handle event …
return new Response("ok");
} catch {
return new Response("Invalid signature", { status: 400 });
}
});
// deploy: supabase functions deploy stripe-webhook --no-verify-jwtWhat not to trust, even after verification
- Metadata you set from the browser when creating the session: a user can choose it. Look the customer up on your side.
- Delivery once and in order: Stripe retries. Store event.id and skip duplicates.
- The test-mode secret in production, or the other way round: each endpoint has its own signing secret.
Check it yourself
Send your endpoint an unsigned POST with a fake event body. It must answer 400 and change nothing. Then run a real test-mode checkout and confirm access is granted exactly once.
See this flaw in the sample security report
Questions
Why does my verification fail with a valid signature?
Almost always because the body was parsed or re-serialised before verifying. Verify the exact raw text Stripe sent.
Do I still need to verify if the URL is secret?
Yes. URLs leak through logs, source maps and client code. The signature is the only proof the event came from Stripe.
Updated 18 November 2026 · by Psychevus