Always verify a webhook before handling it.
Identifiers and Timestamps
Each webhook request carries three request headers alongside the payload, and the payload carries both an eventid and the object. Each value serves a different purpose and none can be substituted for another.
Acknowledge Then Process
Your endpoint must respond with a 2xx status code within 15 seconds, as described in Processing Events. If it takes longer, the delivery is treated as failed and retried, even if your endpoint is still working on the first attempt. This creates the duplicate and concurrent processing that the rest of this page guards against. To avoid this, keep the work done during the request to a minimum:- Verify the webhook.
- Store the event, for example in a queue or a table of received events.
- Respond with a 2xx status code.
- Process the event separately, for example in a background worker.
Handling Duplicate Events
Duplicate processing of events can be prevented at the request-level by processing events in an idempotent-safe manner. Each webhook request includes two identifiers you can use for this. Thewebhook-id request header uniquely identifies the message. It stays the same across every retry of that message, while the webhook-timestamp changes with each delivery attempt.
The event id at the root of the message body uniquely identifies the event itself across all retries or replays. It is a separate value from the webhook-id, and also from the id of the object inside data.
To prevent duplicate deliveries, store the webhook-id in a persistent data store and ensure that a request with the same identifier has not been processed before.
To prevent duplicate logical events, store the event id in the same way. This is the most reliable key for business logic, because it identifies the event regardless of how or where it was delivered. Use it when you register more than one endpoint that feeds the same system, so an event received at each endpoint is only processed once.
Checking both the webhook-id and the event id ensures that a retried delivery of the same message, and a repeat of the same event, are each processed only once.
Keep stored identifiers for at least as long as deliveries can be retried. The retry schedule spans about 28 hours, so keeping identifiers for a few days also covers events that are replayed later.
Beyond request-level handling, integrations should always implement operation-level idempotency, as request-level deduplication is often only applied over a limited time window. Operation-level idempotency ensures that repeated or delayed business events do not cause inconsistent state changes after that window has expired.
To achieve operation-level idempotency, compare the object’s updated_at with the value you have stored before applying an event, as described in Handling Out of Order Events. A duplicate or delayed event never carries a later updated_at than the state you have stored, so it is ignored.
The updated_at comparison, together with side effects that run only once as described in Act on a Terminal Outcome Once, is what keeps your data correct. Checking the webhook-id and event id is a fast way to skip work that has already been done, but correctness should not depend on it.
Handling Out of Order Events
Why Events Arrive Out of Order
Several events can be emitted for the same object within a short period of time, and each one is delivered independently. Events can therefore arrive out of order when:- An earlier event fails delivery and is retried after a later event has already been delivered.
- Multiple events for the same object are delivered concurrently, and your endpoint processes them in parallel.
- Network conditions delay one request relative to another.
payment_session.completed and only afterwards receive a retry of an earlier payment_session.updated event for the same payment session. Applying the second event blindly would move your stored payment session back to an older state.
Use updated_at to Order Events
Every event carries a full snapshot of the object in the data field, and that object includes an updated_at field. The updated_at value records when the object last changed state, so it is the reliable way to tell whether an event is newer or older than the state you have already stored.
Store the updated_at value alongside each object you persist. When an event arrives:
- Look up the stored object using the object’s
idinsidedata, for example the payment session ID. Do not use the eventidat the root of the payload, which identifies the event rather than the object. - If no stored object exists, apply the event and store its
updated_at. - If the event’s
updated_atis later than the storedupdated_at, apply the event and store the newupdated_at. - Otherwise the event is stale or a duplicate. Ignore it.
Compare
updated_at values only between events for the same object. A payment_session and its related payment are separate objects, each with its own updated_at, and should be tracked independently.payment_id on a payment session, process the event anyway rather than responding with an error. The related object’s own events provide its details when they arrive.
Handling Terminal Outcomes
Many objects, such as payment sessions, follow a lifecycle that ends in a terminal state. For example, apayment_session ends as completed, expired, or cancelled. The terminal state is usually the point at which your integration acts, for example by fulfilling an order.
The lifecycle page for each product defines its states, which of them are terminal, and the action to take on each outcome. For example, see the Payment Session Lifecycle and Handling fulfilment.
Respect the Lifecycle
Once an object reaches a terminal state, its status does not change again. If an event appears to move a stored object out of a terminal state, it is an older event that arrived late, and theupdated_at comparison described in Use updated_at to Order Events ignores it.
A terminal status does not always mean the object stops changing. Some objects can still be updated after reaching a terminal state, and each update emits a new event. Continue to apply newer events to an object in a terminal state using the updated_at comparison.
As an additional safeguard, you can also reject any update that would move a stored object out of a terminal state, or that is not a valid transition in the object’s lifecycle.
Act on a Terminal Outcome Once
More than one event can report the same terminal state for an object. For example, both apayment_session.updated and a payment_session.completed event can carry a payment_session with a status of completed. These are separate events, each with its own event id and webhook-id, so deduplicating on those identifiers alone does not prevent a side effect from running twice.
To act on a terminal outcome exactly once:
- Decide what to do from the object’s fields, for example
statusandpayment_outcomeon apayment_session, rather than from the eventtype. - Record each side effect against the object’s
data.id, for example by marking the payment session as fulfilled, and skip the side effect if it has already been recorded. - Make the check and the record a single atomic operation, for example with a unique constraint on
data.idin the table that records fulfilment, so that two events processed concurrently cannot both trigger it.
Apply Updates Atomically
If your endpoint processes requests concurrently, two events for the same object can be handled at the same time. Reading the storedupdated_at and then writing in a separate step leaves room for a stale event to overwrite a newer one.
Perform the comparison and the write in a single conditional operation instead. For example, in PostgreSQL:
updated_at is later than the stored one. If the incoming event is older than or equal to the stored state, no row is changed and the event is safely ignored.
Reconciling Missed Events
Some events may never reach your endpoint. An event is marked as failed once all retry attempts are exhausted, and an endpoint is disabled if all attempts to it fail for 5 days. If events are persistently failing, or your endpoint has been disabled, contact customer success to re-enable your endpoint and to have events from the affected period replayed. Replayed events keep their eventid, so the deduplication and ordering rules on this page handle them safely.
Sample Code
The examples below run in the background worker described in Acknowledge Then Process, after the webhook has been verified and acknowledged. They use two helpers:upsertIfNewerruns the conditional upsert from Apply Updates Atomically, so stale and duplicate events change nothing.fulfilOrderOncefulfils an order at most once per payment session, even when events are retried or processed concurrently.
Code Snippet - Handle a payment session event
Code Snippet - Handle a payment session event
fulfilOrderOnce records fulfilment in a fulfilments table keyed on the payment session ID:
- The first event for a session creates the record. Later events find the existing record instead of creating a second one.
FOR UPDATElocks the record, so a concurrent event for the same session waits until the first transaction finishes, then sees that fulfilment isdoneand skips it.- If fulfilment fails, the transaction rolls back and the record is not marked
done, so a retry attempts fulfilment again. - If fulfilment succeeds but the transaction fails to commit, a retry runs fulfilment again. Passing the session ID as an idempotency key to downstream systems prevents a second order in that case.
Store
updated_at in a timestamp column rather than as a string, so that the database compares the values as timestamps.
