Prova webhook
Acceda come proprietario o amministratore del numero, oppure incolli una chiave API del numero (ambito webhooks:manage) e inserisca il numero sopra. Fino ad allora: solo esempi e codice di verifica.
Invia a un URL
Verifica che il Suo URL risponda.
CorpoInvariato = l'esempio dell'API per quel tipo. Al massimo 16 KB.
Segreto, formato, intestazioni personalizzate
URL di prova
Un URL temporaneo che conserva ciò che gli viene inviato (1 ora, 50 richieste). Lo metta in un webhook oppure invii a lui qui sopra; le richieste compaiono qui in tempo reale.
Webhook salvati
Verifica codice
import crypto from "node:crypto";
// rawBody: the body exactly as received (Buffer or string), eg express.raw({ type: "application/json" })
export function verifyWebhook(rawBody, header, secret, toleranceSeconds = 300) {
const t = /(?:^|,)\s*t=(\d+)/.exec(header ?? "")?.[1];
const v1 = [...(header ?? "").matchAll(/(?:^|,)\s*v1=([0-9a-f]+)/g)].map((m) => m[1]); // every v1=: two while an old secret is kept
if (!t || v1.length === 0 || Math.abs(Date.now() / 1000 - Number(t)) > toleranceSeconds) return false;
const expected = crypto.createHmac("sha256", secret).update(t + ".").update(rawBody).digest("hex");
return v1.some((v) => v.length === expected.length && crypto.timingSafeEqual(Buffer.from(v), Buffer.from(expected)));
}
// const ok = verifyWebhook(req.body, req.get("X-App2email-Signature"), process.env.WEBHOOK_SECRET); import hashlib, hmac, re, time
def verify_webhook(raw_body: bytes, header: str, secret: str, tolerance: int = 300) -> bool:
t = re.search(r"(?:^|,)\s*t=(\d+)", header or "")
v1 = re.findall(r"(?:^|,)\s*v1=([0-9a-f]+)", header or "") # every v1=: two while an old secret is kept
if not t or not v1 or abs(time.time() - int(t.group(1))) > tolerance:
return False
expected = hmac.new(secret.encode(), t.group(1).encode() + b"." + raw_body, hashlib.sha256).hexdigest()
return any(hmac.compare_digest(v, expected) for v in v1)
# ok = verify_webhook(request.get_data(), request.headers.get("X-App2email-Signature"), WEBHOOK_SECRET) <?php
function verify_webhook(string $rawBody, string $header, string $secret, int $tolerance = 300): bool {
if (!preg_match('/(?:^|,)\s*t=(\d+)/', $header, $t)) return false;
preg_match_all('/(?:^|,)\s*v1=([0-9a-f]+)/', $header, $m); // every v1=: two while an old secret is kept
if (!$m[1] || abs(time() - (int)$t[1]) > $tolerance) return false;
$expected = hash_hmac('sha256', $t[1] . '.' . $rawBody, $secret);
foreach ($m[1] as $v) if (hash_equals($expected, $v)) return true;
return false;
}
// $ok = verify_webhook(file_get_contents('php://input'), $_SERVER['HTTP_X_APP2EMAIL_SIGNATURE'] ?? '', getenv('WEBHOOK_SECRET'));