Webhooks
Webhook Setup and Signatures
Set your webhook URL, check the signature on every event, and see how retries work.
A webhook is a URL on your server. When something happens on your number (a new message, a read tick, a call), we send it there right away.
Set it up
- On the Developer API page, pick the number, enter your webhook URL and choose the events. See Webhook Events.
- The URL must be https on the standard port (443).
- Each number has its own webhook URL, events and signing secret.
What we send
Each event is a POST with the event's JSON as the body and these headers:
| Header | Value |
|---|---|
Content-Type | application/json |
X-Webhook-Event | The event name, for example Message |
X-Number-Id | The number the event belongs to |
X-Webhook-Signature | sha256= plus the hex HMAC-SHA256 of the exact body, made with your signing secret |
Answers and retries
- Answer with any 2xx status. Anything else counts as a failure.
- We wait up to 10 seconds for your answer.
- We try up to 3 times: now, after 2 seconds, and after 8 seconds.
- We never follow redirects. Give the final URL.
Check the signature
Check every event before you trust it. Make the HMAC-SHA256 of the raw body (the exact bytes, before you parse the JSON) with your signing secret, add sha256= in front, and compare it with X-Webhook-Signature.
import crypto from "node:crypto";
// rawBody: the exact bytes you received. header: X-Webhook-Signature.
function isValid(rawBody, header, secret) {
const expected = "sha256=" + crypto.createHmac("sha256", secret).update(rawBody).digest("hex");
const a = Buffer.from(header ?? ""), b = Buffer.from(expected);
return a.length === b.length && crypto.timingSafeEqual(a, b);
}import hashlib
import hmac
# raw_body: the exact bytes you received. header: X-Webhook-Signature.
def is_valid(raw_body: bytes, header: str, secret: str) -> bool:
expected = "sha256=" + hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
return hmac.compare_digest((header or "").encode(), expected.encode())<?php
// $rawBody = file_get_contents("php://input");
// $header = $_SERVER["HTTP_X_WEBHOOK_SIGNATURE"] ?? "";
function is_valid(string $rawBody, string $header, string $secret): bool {
$expected = "sha256=" . hash_hmac("sha256", $rawBody, $secret);
return hash_equals($expected, $header);
}Important
Use the raw body. If you parse the JSON and turn it back into text, spaces or order can change and the signature will not match.
If your signing secret leaks, make a new one on the Developer API page. The old one stops working at once.
Send a test event
Press Send test event on the Developer API page. We send this body, signed the same way:
{"type":"Test","event":{"message":"Test event from your Developer API"}}