Webhook Implementation
The API implements webhook handling at/api/webhooks/stripe with signature verification and event processing. Here’s how it works:
The webhook endpoint requires raw request body for signature verification. Make sure your middleware doesn’t parse the body as JSON before the webhook handler receives it.
Testing Methods
There are two primary methods for testing webhooks locally:Stripe CLI
Official Stripe tool that forwards webhook events to your local server
ngrok
Creates a public URL that tunnels to your localhost
Method 1: Using Stripe CLI
The Stripe CLI is the recommended approach for testing webhooks locally.Copy the webhook signing secret and add it to your
.env file as STRIPE_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxxMethod 2: Using ngrok
ngrok creates a public URL that tunnels to your local server, allowing Stripe to send real webhook events.https://abc123.ngrok.io/api/webhooks/stripe.env file as STRIPE_WEBHOOK_SECRETCommon Webhook Events
The API handles these webhook events:| Event Type | Description | When It Fires |
|---|---|---|
payment_intent.succeeded | Payment completed successfully | After confirming a payment intent |
payment_intent.payment_failed | Payment failed | When card is declined or has insufficient funds |
refund.created | Refund initiated | When a refund is created |
charge.refunded | Charge refunded | When a charge is refunded |
Verifying Webhook Signatures
The API automatically verifies webhook signatures to ensure events are from Stripe:Security Best Practice
Always verify webhook signatures in production to prevent unauthorized parties from sending fake webhook events to your endpoint.
Error Handling
Missing Signature
Missing Webhook Secret
Invalid Signature
Testing Workflow
Troubleshooting
Webhook signature verification fails
Webhook signature verification fails
Make sure you’re using the correct webhook secret for your environment. The Stripe CLI generates a different secret than the Stripe Dashboard.
- CLI secret: Starts with
whsec_(fromstripe listenoutput) - Dashboard secret: Starts with
whsec_(from webhook endpoint settings)
Events not being received
Events not being received
- Verify your server is running on the correct port
- Check that the webhook URL is correct
- Ensure no firewall is blocking the connection
- For ngrok, confirm the tunnel is active
Raw body parsing issue
Raw body parsing issue
The webhook endpoint requires the raw request body. If you’re using body parsing middleware, you may need to configure it to skip webhook routes:
Next Steps
- Review the complete payment workflow
- Learn about error handling patterns
- Set up Docker deployment