Prompt
receive Stripe webhooks in Node
Latest observation
Nov 18, 2026
OpenAI APIWeb search: off
To receive Stripe webhooks in Node.js, you need to:
- Create a webhook endpoint in your server
- Read the raw request body exactly as Stripe sends it
- Verify the webhook signature using your Stripe signing secret
- Handle the event types you care about
Example with Express
const express = require('express');
const Stripe = require('stripe');
const app = express();
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// IMPORTANT: use raw body for this route only
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.sendStatus(400);
}
switch (event.type) {
case 'payment_intent.succeeded':
console.log('Payment succeeded:', event.data.object);
break;
case 'invoice.paid':
console.log('Invoice paid:', event.data.object);
break;
default:
console.log(`Unhandled event type: ${event.type}`);
}
res.sendStatus(200);
});
app.listen(3000, () => console.log('Server running on port 3000'));
Install
npm install express stripe
Important notes
- Do not use
express.json()on the webhook route beforeexpress.raw(), or signature verification will fail. - Use your Stripe webhook signing secret from the Stripe Dashboard.
- Your endpoint must be accessible from Stripe, so for local development use something like Stripe CLI or ngrok.
Local testing with Stripe CLI
stripe listen --forward-to localhost:3000/webhook
Then trigger a test event:
stripe trigger payment_intent.succeeded
If you use Express globally
If you already use JSON middleware elsewhere, do this:
app.use('/webhook', express.raw({ type: 'application/json' }));
app.use(express.json());
If you want, I can also give you:
- a TypeScript version
- an Express + Next.js example
- an example for Fastify / NestJS / Koa