API and webhooks
Verify webhook signatures
Anyone can send a request to your webhook URL. Check the Plandalf-Signature header on every request, and only act on ones that match. It takes a few lines.
The signature header
Plandalf-Signature: t=1768487400,v1=5f0c…e9a1
tis when Plandalf sent the request, in Unix seconds.v1is the hex HMAC-SHA256 oft, a full stop and the raw request body, keyed with your endpoint's secret (whsec_…, returned once when you created the endpoint).
To check it, compute the same HMAC over the raw body, compare it with v1 in constant time, and reject a t more than 300 seconds from now so an old request can't be replayed.
Compute the HMAC over the bytes you received, before any JSON parsing. Parsing and re-encoding changes spacing and escapes, and the signature won't match.
Node.js
import crypto from "node:crypto";
export function verifyPlandalf(rawBody, header, secret, tolerance = 300) {
const parts = Object.fromEntries(header.split(",").map((p) => p.trim().split("=")));
if (!/^\d+$/.test(parts.t ?? "") || !parts.v1) return false;
if (Math.abs(Date.now() / 1000 - Number(parts.t)) > tolerance) return false;
const expected = crypto.createHmac("sha256", secret).update(`${parts.t}.${rawBody}`).digest("hex");
const a = Buffer.from(expected);
const b = Buffer.from(parts.v1);
return a.length === b.length && crypto.timingSafeEqual(a, b);
}
With Express, read the body raw on this route:
app.post("/webhooks/plandalf", express.raw({ type: "application/json" }), (req, res) => {
const body = req.body.toString("utf8");
if (!verifyPlandalf(body, req.get("Plandalf-Signature") ?? "", process.env.PLANDALF_WEBHOOK_SECRET)) {
return res.sendStatus(400);
}
const event = JSON.parse(body);
// handle event.type, then:
res.sendStatus(200);
});
PHP
function plandalf_verify(string $body, string $header, string $secret, int $tolerance = 300): bool
{
$parts = [];
foreach (explode(',', $header) as $part) {
[$key, $value] = array_pad(explode('=', trim($part), 2), 2, null);
if ($key !== null && $value !== null) {
$parts[$key] = $value;
}
}
if (!isset($parts['t'], $parts['v1']) || !ctype_digit($parts['t'])) {
return false;
}
if (abs(time() - (int) $parts['t']) > $tolerance) {
return false;
}
$expected = hash_hmac('sha256', $parts['t'] . '.' . $body, $secret);
return hash_equals($expected, $parts['v1']);
}
$body = file_get_contents('php://input');
if (!plandalf_verify($body, $_SERVER['HTTP_PLANDALF_SIGNATURE'] ?? '', getenv('PLANDALF_WEBHOOK_SECRET'))) {
http_response_code(400);
exit;
}
$event = json_decode($body, true);
In Laravel, use $request->getContent() for the body and $request->header('Plandalf-Signature') for the header.
Python
import hashlib
import hmac
import time
def verify_plandalf(body: bytes, header: str, secret: str, tolerance: int = 300) -> 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.isdigit() or not v1:
return False
if abs(time.time() - int(t)) > tolerance:
return False
expected = hmac.new(secret.encode(), t.encode() + b"." + body, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, v1)
In Flask, pass request.get_data() and request.headers.get("Plandalf-Signature", ""). In Django, request.body and request.headers.get("Plandalf-Signature", "").
After it passes
- Skip events whose
Plandalf-Event-Idyou've already handled. A retry sends the same id. - Answer
2xxquickly. See Delivery and retries. - If every request fails the check, make sure the secret is the one for this endpoint, and that a framework or proxy isn't changing the body before your code sees it.