Verify a delivery
Confirm a delivery came from us before you trust it.
On the Agency plan
Your signing secret
Check the signature
Check svix-signature before you trust a delivery. Svix's own library does it in one call (pip install svix).
Python
from svix.webhooks import Webhook, WebhookVerificationError
def is_from_clearreadout(body: bytes, headers: dict, secret: str) -> bool:
try:
Webhook(secret).verify(body, headers)
return True
except WebhookVerificationError:
return FalseOr check it by hand
- Read the headers
svix-id,svix-timestamp, and eachv1,value insvix-signature. - Sign it yourselfHMAC-SHA256 of the id, a dot, the timestamp, a dot and the raw body, with your secret after
whsec_, base64-decoded. - CompareOne
v1must match your base64 result, and the timestamp must be under five minutes old.
Python
import base64, hashlib, hmac, time
def is_from_clearreadout(body: bytes, headers: dict, secret: str) -> bool:
headers = {k.lower(): v for k, v in headers.items()}
try:
msg_id, stamp = headers["svix-id"], headers["svix-timestamp"]
key = base64.b64decode(secret.removeprefix("whsec_"))
signed = f"{msg_id}.{stamp}.".encode() + body
expected = base64.b64encode(hmac.new(key, signed, hashlib.sha256).digest()).decode()
sent = [s.split(",", 1)[1] for s in headers["svix-signature"].split() if s.startswith("v1,")]
return any(hmac.compare_digest(expected, s) for s in sent) and abs(time.time() - int(stamp)) < 300
except (KeyError, ValueError):
return False