How to set up webhooks on Stripe
Setting up webhooks on Stripe involves creating an endpoint in your Stripe Dashboard, configuring the events you want to listen for, and implementing the webhook handler in your application. You'll need to verify webhook signatures for security and test the integration thoroughly.
Prerequisites
- Active Stripe account
- Basic understanding of HTTP requests
- Access to your application's server or webhook endpoint
- Developer permissions on your Stripe account
Step-by-Step Instructions
Access the Webhooks section in Stripe Dashboard
Configure your endpoint URL
https://yourdomain.com/webhooks/stripe). Add an optional description to identify the webhook's purpose.Select events to listen for
payment_intent.succeeded, customer.created, invoice.payment_failed). Alternatively, select Listen to all events if you need comprehensive coverage.Common events include:
payment_intent.succeeded- successful paymentssubscription.updated- subscription changescustomer.subscription.deleted- subscription cancellations
Save and retrieve your webhook secret
whsec_). Copy and securely store this secret in your application's environment variables.Implement webhook handling in your application
- Accepts POST requests at your configured endpoint
- Verifies the webhook signature using Stripe's libraries
- Processes the event data based on event type
Example signature verification:
const sig = req.headers['stripe-signature'];
const event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);Test your webhook endpoint
Monitor and maintain your webhooks
Common Issues & Troubleshooting
Webhooks are not being received
Verify your endpoint URL is publicly accessible and returns a 200 status code. Check your firewall settings and ensure your server is running. Test the URL directly with a tool like curl or Postman.
Webhook signature verification fails
Ensure you're using the correct signing secret from the webhook configuration. Verify you're passing the raw request body (not parsed JSON) to the signature verification function. Check that your endpoint secret matches the webhook in the dashboard.
Webhook deliveries show as failed
Check your application logs for errors in webhook processing. Ensure your endpoint returns a 200 HTTP status code for successful processing. Verify your handler doesn't timeout - Stripe expects responses within 30 seconds.
Duplicate webhook events are being processed
Implement idempotency by checking the event.id against previously processed events. Store processed event IDs in your database to prevent duplicate handling. Use Stripe's idempotency_key for API calls triggered by webhooks.