Webhooks: a signed notice when a long job is over

Reading a scan, comparing two documents, compressing, converting, a correction on every page it is on and a batch of documents are long jobs: they answer 202 with a job_id and are worked in the background. Instead of asking GET /v1/jobs/{job_id} until one is over, name an address of yours and CECO sends it a signed notice the moment it is.

Adding an endpoint

Send the address with your key. It must be https, on port 443, and a public name: CECO never sends a notice to a private, local or reserved address, and checks where the name leads each time it sends one.

set -e
curl -sf -H "Authorization: Bearer $CECO_API_KEY" -H "Content-Type: application/json" \
  -d '{"url":"https://hooks.example.com/ceco","description":"invoice pipeline"}' \
  "$CECO_URL/v1/webhook-endpoints"

The answer carries the endpoint and its signing secret, whsec_…. It is shown once: store it where your receiver can read it. The list, GET /v1/webhook-endpoints, shows only its first characters, and how the last notice to each address went: last_status, last_error and failures in a row. An account has at most five endpoints. To change a secret, add the address again and remove the old endpoint once the new secret is deployed; DELETE /v1/webhook-endpoints/{endpoint_id} stops notices to it at once.

POST /v1/webhook-endpoints/{endpoint_id}:ping sends a test notice, of type ping, so you can see your receiver take and verify one before a real job finishes. Ten test notices an hour can be sent to one endpoint; past that the answer is 429 too_many_webhook_pings. Endpoints are also listed, and can be removed, on the account page under API.

The notice

A POST with a JSON body:

{
  "id": "evt_3f0c9a1e8b7d4c2a9e6f1b05",
  "type": "job.finished",
  "created_at": "2026-11-17T09:41:07Z",
  "api_version": "v1",
  "data": {
    "job_id": "job_6d1f0a9c2e8b47f3a5c1",
    "kind": "ocr",
    "status": "succeeded",
    "url": "/v1/ocr/job_6d1f0a9c2e8b47f3a5c1",
    "job_url": "/v1/jobs/job_6d1f0a9c2e8b47f3a5c1"
  }
}

Answer with any 2xx within ten seconds; take the notice in and do the work afterwards. A try is over after twenty seconds in all, looking up your name, connecting and your answer's headers included, however the bytes are paced. Where your name has both IPv4 and IPv6 addresses, the IPv4 ones are tried first, then each in turn until one connects. Any other answer, a redirect or no answer is tried again after 10 seconds, 1 minute, 5 minutes, 30 minutes, 2 hours, 6 hours and 12 hours: eight tries over about a day. Every try of one notice has the same id, so a receiver that has already handled it answers 200 and does nothing. Notices are sent while the account holds the API; they stop with the Business plan, as the keys do.

Checking the signature

Each notice carries these headers:

Header What it is
Ceco-Signaturets=<unix seconds>;h1=<hex>: the time it was signed, and HMAC-SHA256, keyed with the endpoint's secret, of the time, a colon and the body
Ceco-Event-IdThe notice's id, the same on every try
Ceco-Event-Typejob.finished or ping
Ceco-Delivery-AttemptWhich try this is, from 1

It is the scheme Paddle uses for its own notices. Check three things before believing one: the digest, computed over the raw bytes received, never the JSON parsed and written again; the time, within five minutes of your clock, so a notice somebody captured cannot be sent to you again later; and the id, not one you have already handled, for a copy sent again within those five minutes. Each try is signed when it is sent, so a notice tried again hours later still carries a fresh time.

import hashlib
import hmac
import json
import time

TOLERANCE = 300  # seconds


def verify(body: bytes, header: str, secret: str, now: float | None = None) -> bool:
    """Whether a notice is CECO's: the raw body, the Ceco-Signature header, the secret."""
    parts = dict(chunk.split("=", 1) for chunk in header.split(";") if "=" in chunk)
    stamp, digest = parts.get("ts", ""), parts.get("h1", "")
    if not stamp.isdigit() or not digest:
        return False
    if abs((time.time() if now is None else now) - int(stamp)) > TOLERANCE:
        return False
    expected = hmac.new(secret.encode(), stamp.encode() + b":" + body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, digest)


seen: set[str] = set()


def receive(body: bytes, headers: dict[str, str], secret: str) -> int:
    """What a receiver answers: 401 for a notice that is not CECO's, 200 for one it has
    already had, and 200 once it has taken a new one in."""
    if not verify(body, headers.get("Ceco-Signature", ""), secret):
        return 401
    event = json.loads(body)
    if event["id"] in seen:
        return 200
    seen.add(event["id"])
    # Queue the work here -- read event["data"]["url"] with your key -- and answer at once.
    return 200


# Try it on a notice signed the way CECO signs one.
secret = "whsec_example_only"
body = json.dumps({"id": "evt_1", "type": "ping", "data": {}}).encode()
stamp = str(int(time.time()))
header = f"ts={stamp};h1=" + hmac.new(
    secret.encode(), stamp.encode() + b":" + body, hashlib.sha256
).hexdigest()

assert receive(body, {"Ceco-Signature": header}, secret) == 200
assert receive(body, {"Ceco-Signature": header}, secret) == 200  # the same notice again: nothing done twice
assert receive(body + b" ", {"Ceco-Signature": header}, secret) == 401  # a body changed on the way
assert not verify(body, header, secret, now=time.time() + 3600)  # replayed an hour later
assert not verify(body, header, "whsec_another_secret")
print("the receiver takes CECO's notices and refuses the rest")

Without a webhook

Nothing about a job depends on a webhook: GET /v1/jobs/{job_id} and each kind's own route answer as before, and GET /v1/queue says how long a new job would wait. A notice that never arrives -- your receiver was down for a day -- changes nothing about the job; read it there.

Last updated