Webhooks
Signed, retried notifications of every change, and how to receive them well.
Add an endpoint and choose the events it wants. Each event is sent as a POST with a small JSON body: what happened and to which record. Fetch the record with your key for the details.
POST /webhooks HTTP/1.1
Content-Type: application/json
webhook-id: msg_01JD3XC4Q8F2N6R0T4V8X2Z6B0
webhook-timestamp: 1764576000
webhook-signature: v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=Check the signature
Each request is signed with your endpoint's secret the Standard Webhooks way. Refuse a request whose signature does not match or whose timestamp is more than five minutes old.
import crypto from "node:crypto";
export function verify(rawBody, headers, secret, toleranceSec = 300) {
const id = headers["webhook-id"], ts = headers["webhook-timestamp"], sig = headers["webhook-signature"];
if (!id || !ts || !sig || Math.abs(Date.now() / 1000 - Number(ts)) > toleranceSec) return false;
const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
const expected = crypto.createHmac("sha256", key).update(`${id}.${ts}.${rawBody}`).digest("base64");
return sig.split(" ").some((p) => {
const [v, s] = p.split(",");
return v === "v1" && s && s.length === expected.length && crypto.timingSafeEqual(Buffer.from(s), Buffer.from(expected));
});
}<?php
function verify(string $rawBody, array $headers, string $secret, int $toleranceSec = 300): bool
{
$id = $headers['webhook-id'] ?? null;
$ts = $headers['webhook-timestamp'] ?? null;
$sig = $headers['webhook-signature'] ?? null;
if (!$id || !$ts || !$sig || abs(time() - (int) $ts) > $toleranceSec) {
return false;
}
$key = base64_decode(preg_replace('/^whsec_/', '', $secret));
$expected = base64_encode(hash_hmac('sha256', "{$id}.{$ts}.{$rawBody}", $key, true));
foreach (explode(' ', $sig) as $part) {
[$version, $signature] = array_pad(explode(',', $part, 2), 2, '');
if ($version === 'v1' && hash_equals($expected, $signature)) {
return true;
}
}
return false;
}import base64, hashlib, hmac, time
def verify(raw_body: bytes, headers: dict, secret: str, tolerance_sec: int = 300) -> bool:
msg_id = headers.get("webhook-id")
ts = headers.get("webhook-timestamp")
sig = headers.get("webhook-signature")
if not msg_id or not ts or not sig or abs(time.time() - int(ts)) > tolerance_sec:
return False
key = base64.b64decode(secret.removeprefix("whsec_"))
signed = f"{msg_id}.{ts}.".encode() + raw_body
expected = base64.b64encode(hmac.new(key, signed, hashlib.sha256).digest()).decode()
return any(
part.split(",", 1)[0] == "v1" and hmac.compare_digest(part.split(",", 1)[1], expected)
for part in sig.split(" ") if "," in part
)Retries and order
- Answer with a 2xx within five seconds; do the work afterwards.
- A failed delivery is retried eight times over about 32 hours. Redirects are not followed.
- The same event may arrive twice: skip a webhook-id you have already handled.
- Events may arrive out of order: fetch the record for its current state.
- An endpoint failing for three days is disabled; when you fix it, replay what it missed from the panel.
Security checklist
- Use https with a valid certificate.
- Check the signature on the raw body, before parsing it.
- Keep the signing secret on your server and roll it if it leaks.