Prompt
receive Stripe webhooks in Node
Latest observation
Oct 5, 2026
OpenAI APIWeb search: off
To receive Stripe webhooks in Node, you need to:
- Create a webhook endpoint
- Read the raw request body
- Verify the Stripe signature
- Handle the event
Example with Express
1) Install Stripe
npm install stripe express
2) Set up the webhook route
Important: Stripe requires the raw body, not express.json() for this route.
const express = require('express');
const Stripe = require('stripe');
const app = express();
const stripe = Stripe(process.env.STRIPE_SECRET_KEY);
app.post(
'/webhook',
express.raw({ type: 'application/json' }),
(req, res) => {
const sig = req.headers['stripe-signature'];
const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
} catch (err) {
console.error('Webhook signature verification failed:', err.message);
return res.status(400).send(`Webhook Error: ${err.message}`);
}
switch (event.type) {
case 'payment_intent.succeeded':
console.log('Payment succeeded:', event.data.object);
break;
case 'checkout.session.completed':
console.log('Checkout completed:', event.data.object);
break;
default:
console.log(`Unhandled event type ${event.type}`);
}
res.json({ received: true });
}
);
app.listen(3000, () => console.log('Server running on port 3000'));
3) Add the webhook secret
From Stripe Dashboard:
- Developers → Webhooks → your endpoint
- Copy the Signing secret
- Put it in
STRIPE_WEBHOOK_SECRET
4) If you already use express.json()
Make sure the webhook route is defined before express.json() or exclude that route:
app.post('/webhook', express.raw({ type: 'application/json' }), handler);
app.use(express.json());
5) Test locally with Stripe CLI
stripe listen --forward-to localhost:3000/webhook
Then trigger a test event:
stripe trigger payment_intent.succeeded
Common gotchas
- Don’t parse the body before verification
- Use the correct webhook signing secret
- Ensure the request uses the exact raw body Stripe sent
If you want, I can also give you:
- an Express + TypeScript version
- a Next.js webhook example
- a Fastify version