Handle webhooks safely
Certa POSTs each event to your endpoint and signs it. Check the signature on the raw body, answer 2xx quickly, and handle each event id once.
1. Register an endpoint
const hook = await certa.webhooks.create({
url: "https://example.com/certa/webhooks",
events: ["lock.created", "lock.settled", "lock.cancelled", "lock.defaulted", "deposit.low"],
});
// hook.secret is shown once: store it as a secret (for example CERTA_WEBHOOK_SECRET).
Endpoints must be HTTPS (HTTP only for local runs). For a laptop, use a tunnel that gives you a public HTTPS URL that forwards to localhost.
2. Verify, then parse
import { constructEvent } from "@certa-fx/sdk";
app.post("/certa/webhooks", express.raw({ type: "application/json" }), async (req, res) => {
let event;
try {
event = await constructEvent(
req.body.toString("utf8"), // the raw body, exactly as received
req.header("Certa-Signature"),
process.env.CERTA_WEBHOOK_SECRET!,
);
} catch {
return res.sendStatus(400);
}
res.sendStatus(200); // answer first, work after
await queue.add(event); // dedupe on event.id in the worker
});
In Python:
from certa import construct_event
event = construct_event(request.get_data(as_text=True),
request.headers.get("Certa-Signature"),
os.environ["CERTA_WEBHOOK_SECRET"])
3. Rules that keep you safe
- The header is
Certa-Signature: t=…,v1=…. Signatures older than 5 minutes are refused, which stops replays of captured requests. - A non-2xx answer is retried after 1, 5 and 30 minutes, then 2, 6 and 12 hours, for up to 24 hours. So the same event can arrive more than once: store
event.idand skip ones you have seen. - Events can arrive out of order. Read the current state with
GET /v1/locks/{id}when order matters. - After you rotate the secret (
POST /v1/webhooks/{id}/rotate-secret), deliveries carry both signatures for 24 hours: pass both secrets while you switch. - Missed something?
GET /v1/eventslists every event, andPOST /v1/events/{id}/replaysends one again.