الويب هوك
Register an endpoint, verify the signature, survive retries, and never process the same delivery twice.
Webhook endpoints belong to the tracking family and authenticate with a tracking key. An endpoint subscribes to an explicit list of event types; an empty list is rejected rather than silently meaning everything.
- Create the endpoint
POST /v1/webhooks with an HTTPS url and the subscribed_events list. The signing secret is returned once, in the creation response. Store it before the response leaves your handler.
- Verify every request
Compute the signature over the raw request body and compare it in constant time. Reject anything that does not match before parsing JSON.
- Acknowledge quickly
Return 2xx as soon as you have durably queued the work. A slow handler is indistinguishable from a failing one.
- Deduplicate
Retries reuse the same delivery identifier. Upsert on it instead of appending, or you will process one event twice.
Signature verification
The signature is HMAC-SHA256 over the string "<timestamp>.<raw body>", keyed with your endpoint secret, hex encoded and prefixed v1=. Three headers accompany each delivery.
| Header | Content |
|---|---|
| X-Bardiya-Webhook-Id | Stable delivery identifier, also used as the deduplication key |
| X-Bardiya-Timestamp | Unix seconds when the delivery was signed |
| X-Bardiya-Signature | v1=<hex hmac-sha256 of "<timestamp>.<raw body>"> |
import { createHmac, timingSafeEqual } from "node:crypto";
export function verify(rawBody: string, headers: Record<string, string | undefined>, secret: string) {
const timestamp = headers["x-bardiya-timestamp"];
const signature = headers["x-bardiya-signature"];
if (!timestamp || !signature) return false;
// Reject stale deliveries: bound the window so a captured request cannot be
// replayed indefinitely. Five minutes is generous for a retry.
const age = Math.abs(Date.now() / 1000 - Number(timestamp));
if (!Number.isFinite(age) || age > 300) return false;
const expected = createHmac("sha256", secret)
.update(`${timestamp}.${rawBody}`)
.digest("hex");
const received = Buffer.from(signature.replace(/^v1=/, ""), "hex");
const computed = Buffer.from(expected, "hex");
return received.length === computed.length && timingSafeEqual(received, computed);
}Retries and failures
- A delivery that does not receive a 2xx is retried with backoff. Each attempt keeps the same delivery identity and appears in GET /v1/webhooks/{endpoint_id}/deliveries.
- POST /v1/webhooks/{endpoint_id}/deliveries/{delivery_id}/retry re-sends a failed attempt manually. It is charged and recorded as a new attempt.
- Endpoint URLs must be HTTPS and must not resolve to a private address range. That is enforced, not advisory.
- Disable an endpoint with PATCH status instead of deleting it: the secret and the delivery history survive.
Building an idempotent consumer
def handle(headers, raw_body):
if not verify(raw_body, headers, SECRET):
return 400, {"error": "bad signature"}
delivery_id = headers["x-bardiya-webhook-id"]
if store.seen(delivery_id): # upsert, do not append
return 200, {"status": "duplicate"}
store.record(delivery_id, raw_body) # durable, before any side effect
queue.enqueue(raw_body) # ack fast, work later
return 200, {"status": "accepted"}