Webhooks
How the fragment processes Stripe webhook events
Overview
Stripe reports subscription changes through webhook events. The fragment's webhook route verifies the event signature, records the event as a durable hook and responds right away. A hook processor then handles the event, retrying when Stripe or your callbacks fail.
Stripe delivers events at least once and in any order. The fragment therefore does not apply the snapshot an event carries: an event prompts it to fetch the subscription from Stripe and store the state Stripe reports at that moment. A late or repeated event cannot roll a subscription back. Repeated deliveries of the same event are recorded once.
Without a running hook processor, events are recorded but never processed.
Handled Events
checkout.session.completed(subscription mode) - Syncs the subscription the checkout createdcustomer.subscription.*- Syncs the subscription (created, updated, deleted, paused, resumed, trial ending, pending updates)customer.deleted- Unlinks the customer from its entity, so the entity's next checkout creates a new customer
Configure your Stripe webhook endpoint to send at least these events.
Webhook Endpoint URL
The webhook endpoint is available under the {mountPath}/webhook path, for example:
https://yourdomain.com/api/stripe/webhookHere /api/stripe is the default mount path where you have
mounted the Stripe fragment.
Reacting to Changes
Use onSubscriptionChanged to act on subscription state, for example to grant or revoke access. It
runs after a sync changed a subscription and receives the subscription as it is stored when the
callback runs, so a retried callback never sees an outdated state.
export const stripeFragment = createStripeFragment(
{
// ... other config
onSubscriptionChanged: async ({ subscription }, { idempotencyKey }) => {
if (subscription.referenceId) {
await setPlan(subscription.referenceId, subscription.priceId, subscription.status);
}
},
},
{ databaseAdapter },
);Custom Webhook Event Handlers
onStripeEvent receives every verified Stripe event. It runs as its own durable hook, so a failing
handler retries on its own without holding back the fragment's subscription sync. Events arrive in
any order; read subscription state from onSubscriptionChanged instead of event payloads.
export const stripeFragment = createStripeFragment(
{
// ... other config
onStripeEvent: async ({ event }, { idempotencyKey }) => {
switch (event.type) {
case "invoice.payment_failed":
// notify the customer
break;
}
},
},
{ databaseAdapter },
);All callbacks run at least once. Use the idempotencyKey of the hook context (second argument) to
make side effects idempotent.
Testing Webhooks Locally
Use the Stripe CLI to forward webhook events to your local server.
stripe listen --forward-to localhost:3000/api/stripe/webhookThen use the webhook secret provided by the CLI in your fragment config.
When not to rely on webhooks
Webhooks are not guaranteed to be delivered in real-time, therefore there are cases where you may need to get the latest subscription information from Stripe directly. See Manual Synchronization for additional details.