Skip to content
CertadocsGet a sandbox key

Guides

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