Endpoint
POST/api/webhook/stripe
This endpoint verifies the Stripe signature and handles various billing lifecycle events.
Authentication
This endpoint uses Stripe webhook signature verification instead of user authentication. You must configure theSTRIPE_WEBHOOK_SECRET environment variable with your webhook signing secret from the Stripe dashboard.
Headers
The Stripe webhook signature header for event verification
Request body
The request body is the raw Stripe event payload. Stripe sends different event types with varying data structures.Supported events
The webhook currently handles these Stripe event types:checkout.session.completed
Triggered when a customer completes a checkout session.packages/landing/app/api/webhook/stripe/route.ts
The TODO comments indicate where you should integrate with your Supabase database to grant product access to the user.
checkout.session.expired
Triggered when a checkout session expires without completion.customer.subscription.updated
Triggered when a subscription is updated (plan change, renewal, etc.).customer.subscription.deleted
Triggered when a subscription is canceled or expires.invoice.paid
Triggered when an invoice payment succeeds (subscription renewal, etc.).invoice.payment_failed
Triggered when an invoice payment fails.Response
Success (200)
Error (400)
Implementation notes
The webhook handler verifies the Stripe signature before processing any events:packages/landing/app/api/webhook/stripe/route.ts
Setup
1. Configure webhook in Stripe
- Go to the Stripe Dashboard → Developers → Webhooks
- Click Add endpoint
- Enter your webhook URL:
https://yourdomain.com/api/webhook/stripe - Select the events you want to receive (or select “all events” for development)
- Copy the Signing secret
2. Add environment variable
Add the webhook signing secret to your environment variables:3. Test locally with Stripe CLI
Use the Stripe CLI to forward webhook events to your local development server:.env.local file.
4. Trigger test events
Integration checklist
The current implementation includes TODO comments where you need to integrate with your database:- checkout.session.completed: Grant product access in Supabase
- customer.subscription.updated: Update subscription status
- customer.subscription.deleted: Revoke access
- invoice.paid: Confirm or extend access
- invoice.payment_failed: Notify customer and handle grace period
Store the Stripe
customer_id, subscription_id, and price_id in your user profiles table to track billing status.Security considerations
- Signature verification: Always verify the
stripe-signatureheader - Idempotency: Handle duplicate webhook deliveries gracefully
- Logging: Log webhook events for debugging and audit trails
- Error handling: Return 200 even if your internal processing fails (Stripe will retry)
Related
Billing configuration
Configure Stripe API keys and webhook secrets
Checkout API
Create checkout sessions and customer portals