Webhooks
How to verify and deduplicate signed webhooks
Verify SendMQ webhook signatures against the raw body and keep retried deliveries from repeating a business update.
By SendMQ 1 min read
How do SendMQ webhook signatures work?
SendMQ signs each webhook with HMAC-SHA256 using the endpoint’s secret. The signed input is the timestamp, a period, and the exact raw request body. Parse X-SendMQ-Signature for the timestamp and signature, enforce a timestamp tolerance, and compare the expected signature in constant time.
Capture the raw request body before parsing JSON. Parsing and re-serializing the payload can change whitespace or key order, which can invalidate the signature.
How do you process a webhook only once?
After verifying the signature, use X-SendMQ-Delivery-Id as a deduplication key. It stays the same across attempts of one delivery. Create a unique constraint on that ID and save the deduplication record and the business update in one transaction.
For an order.shipped event, update the order and record the verified delivery ID together. If the same delivery comes back, return 2xx without applying the update again. When multiple events can change the same record, include a resource version so a late retry cannot overwrite newer data.
verify_signature(raw_body, signature_header)
begin transaction
if insert_delivery_id_if_new(delivery_id):
update_order_if_version_is_newer(payload)
commit
return 204What should a receiver return?
Use 2xx after durably accepting a verified event. Return 429 or 5xx for a temporary problem so SendMQ can retry. Use a permanent 4xx for requests you cannot accept, such as an invalid signature. Delivery attempt history helps you diagnose failures before replaying a webhook.