Skip to the content
YallaRoute

Help · Developers: API keys and webhooks

Verify webhook signatures

Every webhook carries the header X-YallaRoute-Signature with a timestamp t and a signature v1. Compute an HMAC-SHA256 of the timestamp, a full stop and the raw request body, keyed with the endpoint's signing secret, and compare it with v1 in constant time. Reject the message if they differ or if t is more than five minutes old.

Updated

Where
Your webhook receiver
Who can do it
Developers
Plans
Webhooks is included in Growth, Scale and Enterprise

In detail

Anyone who learns your endpoint's address can post to it. The signature is what tells a real event from a forged or replayed one, so check it on every request before you act on the body.

The header

The header looks like X-YallaRoute-Signature: t=1764670000000,v1=5f2b.... t is the time of signing in milliseconds since 1970 (Unix time in milliseconds). v1 is the hexadecimal HMAC-SHA256 of the text t, a full stop, and the raw body, keyed with the signing secret of the endpoint.

Step by step

  1. Read the body exactly as it arrived, as text or bytes, before any JSON parsing. Parsing and re-encoding changes the bytes and breaks the check.
  2. Split the header on the comma into t and v1.
  3. Compute the HMAC-SHA256 of t, a full stop and the raw body, with your signing secret as the key, as hexadecimal.
  4. Compare it with v1 using a constant-time comparison.
  5. Reject the request if they differ, or if t is more than five minutes away from your clock.
  6. Only then parse the JSON and act on it.
Node.jsjs
import { createHmac, timingSafeEqual } from "node:crypto";

// rawBody: the request body exactly as received, before JSON parsing
export function verify(rawBody, header, secret, toleranceMs = 5 * 60 * 1000) {
  const parts = Object.fromEntries(
    String(header ?? "").split(",").map((p) => p.trim().split("=")),
  );
  const t = Number(parts.t);
  if (!Number.isFinite(t) || !parts.v1) return false;
  if (Math.abs(Date.now() - t) > toleranceMs) return false;
  const expected = createHmac("sha256", secret).update(`${parts.t}.${rawBody}`).digest("hex");
  const a = Buffer.from(expected, "hex");
  const b = Buffer.from(parts.v1, "hex");
  return a.length === b.length && timingSafeEqual(a, b);
}
Pythonpython
import hashlib, hmac, time

def verify(raw_body: bytes, header: str, secret: str, tolerance_ms: int = 300_000) -> bool:
    parts = dict(p.strip().split("=", 1) for p in header.split(",") if "=" in p)
    t, v1 = parts.get("t"), parts.get("v1")
    if not t or not v1 or not t.isdigit():
        return False
    if abs(time.time() * 1000 - int(t)) > tolerance_ms:
        return False
    signed = t.encode() + b"." + raw_body
    expected = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, v1)

Replacing the secret

Choose Replace signing secret on the endpoint's menu. A new secret is made and the old one stops signing at once, so put the new secret on your server first, or accept both for the few minutes it takes to deploy. Showing the current secret again is possible for owners and is recorded in the audit log.

Try it in your own workspace

A new workspace has the Scale features for its first 14 days. Open one and follow this article with your own data.