Webhooks
Webhooks allow your platform to receive real-time notifications from Stream when key events occur—such as payment successes, failures, or cancellations. This enables you to react to events programmatically, such as updating your database, sending notifications, or triggering internal workflows.
Stream's webhook infrastructure is designed for reliability and includes features like:
- Event-specific subscriptions
- Secure payload signing
- Delivery tracking for observability
- Automatic retries using exponential backoff
- One idempotency id per event, so you can recognise a repeat
- An ordering value on status changes, so you can tell which change is newer
Creating a Webhook
To create or manage your webhooks, go to Webhook Settings.
There you can:
- Register a new webhook endpoint
- Select which event types you want to subscribe to
- Enable, disable, or delete webhooks
Webhook Flow Logic
Understanding which webhooks are sent in different scenarios:
Single Payment Succeeds
When a single payment succeeds:
PAYMENT_SUCCEEDEDwebhook is sent (one per payment)- If the payment completes the invoice (all payments are now paid),
INVOICE_COMPLETEDwebhook is also sent
Example: Invoice with 1 payment of 1000 SAR
- Payment succeeds →
PAYMENT_SUCCEEDEDwebhook - Invoice completes →
INVOICE_COMPLETEDwebhook
Multiple Payments Succeed (Installments)
When multiple payments succeed together (e.g., installments):
PAYMENT_SUCCEEDEDwebhook is sent for each payment that succeeded (one per payment)- If all payments complete the invoice,
INVOICE_COMPLETEDwebhook is also sent
Example: Invoice with 3 payments of 1000 SAR each
- Payments 1, 2, 3 all succeed together:
PAYMENT_SUCCEEDEDwebhook for payment_1PAYMENT_SUCCEEDEDwebhook for payment_2PAYMENT_SUCCEEDEDwebhook for payment_3INVOICE_COMPLETEDwebhook
Example: Invoice with 5 payments, payments 1-3 succeed together:
PAYMENT_SUCCEEDEDwebhook for payment_1PAYMENT_SUCCEEDEDwebhook for payment_2PAYMENT_SUCCEEDEDwebhook for payment_3- No
INVOICE_COMPLETEDwebhook (invoice not yet fully paid)
Payment Fails
When a payment fails:
PAYMENT_FAILEDwebhook is sent- No invoice completion webhook is sent
Payment Refunded (Partial or Full)
Stream supports multiple refunds on a single payment. Webhook events depend on whether the payment is fully refunded or only partially refunded:
Partial refund (cumulative refunded amount is still less than the payment amount):
PAYMENT_PARTIALLY_REFUNDEDwebhook is sent- Payment status becomes
PARTIALLY_REFUNDED - Invoice status does not change
Full refund (cumulative refunded amount equals the payment amount):
PAYMENT_REFUNDEDwebhook is sent- Payment status becomes
REFUNDED - Invoice status does not change
Example: Payment of 1000 SAR
- First refund of 300 SAR →
PAYMENT_PARTIALLY_REFUNDED(amount_refunded= 300,total_amount_refunded= 300) - Second refund of 200 SAR →
PAYMENT_PARTIALLY_REFUNDED(amount_refunded= 200,total_amount_refunded= 500) - Final refund of 500 SAR →
PAYMENT_REFUNDED(amount_refunded= 500,total_amount_refunded= 1000)
PAYMENT_REFUNDED means the payment is fully refunded. If you care about partial refunds, also subscribe to PAYMENT_PARTIALLY_REFUNDED.
Payment Marked as Paid
When a payment is manually marked as paid:
PAYMENT_MARKED_AS_PAIDwebhook is sent- If this completes the invoice,
INVOICE_COMPLETEDwebhook is also sent
Subscription Renewal
When a subscription cycle renews:
- New invoice created →
INVOICE_CREATEDwebhook - Payment succeeds →
PAYMENT_SUCCEEDEDwebhook - Invoice completes →
INVOICE_COMPLETEDwebhook
Note: For subscription renewals, the INVOICE_COMPLETED webhook indicates that the subscription cycle has been renewed successfully. The invoice will have a subscription_id in its payload, and you can check if it's a renewal invoice by comparing it with previous invoices for that subscription.
Event Types
Payment Events
PAYMENT_SUCCEEDED- Payment completed successfullyPAYMENT_FAILED- Payment attempt failedPAYMENT_CANCELED- Payment was canceledPAYMENT_EXPIRED- Payment expired because its invoice passed its expiry date unpaidPAYMENT_PARTIALLY_REFUNDED- A refund was issued and the payment is still only partially refundedPAYMENT_REFUNDED- Payment was fully refundedPAYMENT_MARKED_AS_PAID- Payment was manually marked as paid
Invoice Events
INVOICE_CREATED- New invoice createdINVOICE_SENT- Invoice sent to consumerINVOICE_ACCEPTED- Invoice accepted by consumerINVOICE_REJECTED- Invoice rejected by consumerINVOICE_COMPLETED- Invoice completed (all payments done)INVOICE_CANCELED- Invoice canceledINVOICE_EXPIRED- Invoice passed itsexpires_atdate without being paid, and can no longer be paidINVOICE_UPDATED- Invoice details updated
Subscription Events
SUBSCRIPTION_CREATED- New subscription createdSUBSCRIPTION_ACTIVATED- Subscription changed toACTIVE(initial activation or reactivation)SUBSCRIPTION_INACTIVATED- Subscription deactivatedSUBSCRIPTION_CANCELED- Subscription canceledSUBSCRIPTION_FROZEN- Subscription frozenSUBSCRIPTION_CYCLE_RENEWED_SUCCESSFULLY- Renewal payment succeeded and the subscription was renewedSUBSCRIPTION_CYCLE_RENEWAL_FAILED- Automated/tokenized renewal payment failedSUBSCRIPTION_CANCEL_AT_PERIOD_END- Subscription scheduled to cancel at period endSUBSCRIPTION_FREEZE_NOW- Subscription frozen immediatelySUBSCRIPTION_UNFREEZE_NOW- Subscription unfrozen immediatelySUBSCRIPTION_UNFREEZE_FUTURE- Subscription scheduled to unfreeze in futureSUBSCRIPTION_FREEZE_CANCEL- Subscription freeze canceledSUBSCRIPTION_PLAN_CHANGE_SCHEDULED- A plan change was scheduled to take effect atcurrent_period_end.SUBSCRIPTION_PLAN_CHANGE_CANCELED- A previously scheduled plan change was canceled before it took effect.SUBSCRIPTION_PLAN_CHANGED- The scheduled plan change was actually applied — items, coupons, interval, and amount are now live on the subscription.SUBSCRIPTION_PLAN_CHANGE_INVOICE_REISSUED- While scheduling a plan change, an existing prepared next-cycle invoice was canceled and regenerated from the new target plan. This is triggered alongsideSUBSCRIPTION_PLAN_CHANGE_SCHEDULEDif it affects the plan's invoice.SUBSCRIPTION_PLAN_UPDATED- Subscription plan change was applied immediately. This applies for changing seats, adding add-ons, and changing number of subscription cycles.
Payment Link Events
-
PAYMENT_LINK_PAY_ATTEMPT_FAILED- Payment attempt failed for a payment linkNote: This webhook is sent when a payment link payment fails. Unlike regular payment failures, no invoice, payment, or subscription entities are created on failure, so
PAYMENT_FAILEDwebhook cannot be sent. This event exists specifically to notify about payment link failures when no entities exist.
Webhook Payload
The webhook POST request will have a JSON body like:
{
"event_type": "PAYMENT_SUCCEEDED",
"entity_type": "PAYMENT",
"entity_id": "uuid",
"entity_url": "<https://stream-app-service.streampay.sa/api/v2/payments/{id}>",
"status": "SUCCEEDED",
"previous_status": "FAILED",
"data": { /* payment or event-specific data */ },
"timestamp": "2025-07-15T14:41:21.705Z",
"event_idempotency_id": "0b6f1c2e-6a3d-4f7e-9b1a-2c5d8e4f7a90",
"event_occurred_at": "2025-07-15T14:41:20.913Z",
"event_sorting_order": 1048213
}
Webhook Payload Example
{
"data": {
"invoice": {
"id": "2df0f7e0-2634-46ab-829a-bfcb0a797d87",
"url": "https://stream-app-service.streampay.sa/api/v2/invoices/2df0f7e0-2634-46ab-829a-bfcb0a797d87"
},
"payment": {
"id": "e2182d3d-b4cf-4972-bcc0-ec6d963c066d",
"url": "https://stream-app-service.streampay.sa/api/v2/payments/e2182d3d-b4cf-4972-bcc0-ec6d963c066d"
},
"payment_link": {
"id": "6361941e-3a81-4aa5-aaa6-60d6e052883a",
"url": "https://stream-app-service.streampay.sa/api/v2/payment_links/6361941e-3a81-4aa5-aaa6-60d6e052883a"
},
"metadata": {
"customer_id": "cust_12345",
"nested_data": {
"sub_field": "sub_value",
"sub_number": 678
},
"affiliate_id": "aff_987",
"custom_field_1": "value_1",
"custom_field_2": 12345,
"campaign_source": "email_marketing"
}
},
"status": "SUCCEEDED",
"previous_status": "PENDING",
"entity_id": "e2182d3d-b4cf-4972-bcc0-ec6d963c066d",
"timestamp": "2025-07-22T14:40:31.485576",
"event_idempotency_id": "7c1e9a52-3f0b-4d61-8a2e-5b9f0c6d1e34",
"event_occurred_at": "2025-07-22T14:40:30.912044Z",
"event_sorting_order": 1048213,
"entity_url": "https://stream-app-service.streampay.sa/api/v2/payments/e2182d3d-b4cf-4972-bcc0-ec6d963c066d",
"event_type": "PAYMENT_SUCCEEDED",
"entity_type": "PAYMENT"
}
Webhook Request Headers
Content-Type: application/jsonUser-Agent: StreamApp-Webhook/1.0X-Webhook-Event: Event type (e.g.,PAYMENT_SUCCEEDED)X-Webhook-Entity-Type: Entity type (e.g.,PAYMENT)X-Webhook-Entity-ID: Entity UUIDX-Webhook-Event-Idempotency-ID: The event'sevent_idempotency_id(same value as in the body)X-Webhook-Signature: Format:t={timestamp},v1={signature}timestampis the UNIX timestamp when the signature was generated.signatureis an HMAC-SHA256 of{timestamp}.{payload}using your webhook’ssecret_key.
X-Webhook-Timestamp: The timestamp used in the signature.
Verifying Webhook Authenticity
Always verify the X-Webhook-Signature before treating a webhook as authentic or updating your system (fulfillment, access, financial state). Reject requests with a missing or invalid signature. Unsigned or unverified payloads could be forged; never trust the JSON body alone.
To verify a webhook:
- Extract the
X-Webhook-Signatureheader and split into timestamp and signature. - Compute the HMAC-SHA256 of
{timestamp}.{raw_request_body}using your webhook’ssecret_key. - Compare your computed signature to the value in the header.
Example (Python):
import hmac, hashlib
def verify_webhook_signature(secret: str, raw_body: bytes, signature_header: str):
# signature_header: "t=TIMESTAMP,v1=SIGNATURE"
parts = dict(x.split('=') for x in signature_header.split(','))
timestamp = parts['t']
signature = parts['v1']
message = f"{timestamp}.{raw_body.decode()}"
computed = hmac.new(secret.encode(), message.encode(), hashlib.sha256).hexdigest()
return hmac.compare_digest(computed, signature)
Duplicates and ordering
Stream delivers every webhook at least once. The same event can reach you more than once: after a retry, a manual re-send from the dashboard, or a recovery on our side. Webhooks are also not delivered in order: a later change can arrive before an earlier one.
Every webhook body carries these fields:
| Field | On | Meaning |
|---|---|---|
event_idempotency_id | every webhook | One id per event. It is the same on every retry and re-send, and the same for every endpoint that receives the event. Also sent as the X-Webhook-Event-Idempotency-ID header. |
event_occurred_at | every webhook | When the change happened (UTC). timestamp stays the time the webhook was built. |
event_sorting_order | status webhooks | For one payment, invoice or subscription, a later change has a higher value. Values are not consecutive, so a gap does not mean you missed an event. null on notices. |
previous_status | payment, invoice and subscription status webhooks | The status before this change. null on notices and plan-change webhooks. |
Status webhooks report a change of the entity's status. Their status is the status the entity moved to:
- Payments:
PAYMENT_SUCCEEDED,PAYMENT_FAILED,PAYMENT_CANCELED - Invoices:
INVOICE_SENT,INVOICE_ACCEPTED,INVOICE_REJECTED,INVOICE_COMPLETED,INVOICE_CANCELED - Subscriptions:
SUBSCRIPTION_ACTIVATED,SUBSCRIPTION_INACTIVATED,SUBSCRIPTION_CANCELED,SUBSCRIPTION_FROZEN - Plan changes:
SUBSCRIPTION_PLAN_CHANGE_SCHEDULED,SUBSCRIPTION_PLAN_CHANGE_CANCELED,SUBSCRIPTION_PLAN_CHANGED,SUBSCRIPTION_PLAN_UPDATED,SUBSCRIPTION_PLAN_CHANGE_INVOICE_REISSUED. Theirstatusis the subscription's status when the change was made.
Notices report something other than a status change: every other event type, including refunds, PAYMENT_MARKED_AS_PAID, INVOICE_CREATED, INVOICE_UPDATED, renewals, freezes, SUBSCRIPTION_CANCEL_AT_PERIOD_END and PAYMENT_LINK_PAY_ATTEMPT_FAILED. Their status field is not the entity's current status. For a refund, read the refund's state from event_type and data.
How to handle them
- De-duplicate on
event_idempotency_id. Store the ids you have processed, and acknowledge a repeat with a2xxwithout acting on it again. - Process every event, even one that arrives late. Each partial refund, for example, carries its own facts.
- Keep the entity's status from the status webhook with the highest
event_sorting_order. If a status webhook arrives with a lower value than one you already applied, record it but do not move the status back. - When in doubt, fetch the entity from
entity_urlto read its current state.
def handle_webhook(body: dict) -> None:
if already_processed(body["event_idempotency_id"]):
return # a repeat: acknowledge with 2xx and do nothing
record_event(body) # every event is processed, including late ones
order = body.get("event_sorting_order")
if order is not None and order > last_applied_order(body["entity_id"]):
set_status(body["entity_id"], body["status"], order)
mark_processed(body["event_idempotency_id"])
Webhooks created before these fields were introduced do not carry them, and neither does a re-send of such a webhook. Treat a missing event_idempotency_id as "no id": de-duplicate those by entity_id, event_type and status, or fetch the entity.
Webhook Delivery & Retry Logic
-
Delivery Attempts:
Every webhook event triggers one delivery record per endpoint. Each delivery attempt and its status (pending, delivered, failed, retrying) is tracked for auditing. Retries and manual re-sends reuse the same delivery record and send the same
event_idempotency_id. -
Retry Schedule:
If your endpoint does not respond with a 2xx status, Stream will retry delivery up to 5 times.
The default retry delays (in minutes) are:
- 1st retry: 5 minutes after the first failure
- 2nd retry: 30 minutes after the second failure
- 3rd retry: 2 hours (120 minutes) after the third failure
- 4th retry: 6 hours (360 minutes) after the fourth failure
- 5th retry: 12 hours (720 minutes) after the fifth failure
After the fifth attempt, if delivery is still unsuccessful, the webhook delivery is marked as failed and no further attempts will be made.
-
Auditing:
All delivery attempts, responses, and errors are logged and can be audited.