Webhooks

Hear of every sandbox change at your own address, signed, and check each delivery

A webhook is your https address that Pod calls each time a sandbox of a project changes state: it was made, it is running, it went to sleep, it stopped, it failed. Your code reacts at once instead of asking again and again. A project holds up to 10.

Make one

Make a webhook in the dashboard under Keys, or with the API and a key holding webhooks:write. Name the events it takes; name none and it takes every one, those added later included.

curl -X POST "https://api.pod.mapier.ai/v1/webhooks" \
  -H "Authorization: Bearer $POD_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/pod-events", "types": ["sandbox.running", "sandbox.error"]}'

The answer holds the signing secret, whsec_…, this once. Keep it where your receiver reads it. The address must be https, carry no user name or password, and not be an internal or reserved address; Pod checks it again before every delivery.

Check every delivery

Each delivery is one event, as JSON, signed by the Standard Webhooks scheme. Check it before you act on it: the SDKs do it in one call, and any Standard Webhooks library does too. Pass the body exactly as it arrived, before any JSON parsing.

import { verifyWebhook } from "@mapier/pod";

const event = await verifyWebhook({ secret, headers: request.headers, body: await request.text() });

A delivery whose signature does not match, or whose webhook-timestamp is more than 300 seconds from your clock, is refused (PodWebhookError). Answer it with a 4xx and do nothing else.

Order, repeats and retries

  • Pod delivers at least once, in order per sandbox: a sandbox's next event waits until you answer this one. Drop a repeat by its webhook-id, and compare seq and sub to drop a change older than one you handled.
  • Answer 2xx within 15 seconds. Anything else, redirects included, is a failure, and Pod tries again, 8 attempts in all over about a day, then marks the event failed and moves on.
  • A webhook whose attempts only fail for days is turned off, and its owners are told. Fix the address, turn it on, then send again what it missed.
  • Each attempt is in the webhook's delivery log: when, the status you answered, how long it took and what happens next. Pod keeps the latest 1,000 and never reads or keeps what your code answers.

Send again

Send again every event since a time, at most 7 days back, or one sandbox's, with the ids they first had, so your receiver drops what it already has. A replay runs beside live delivery, in its own lane.

Rotate the secret

Rotate and Pod makes a new secret, shown once. The old one keeps signing beside it, 24 hours by default and at most 7 days, so every delivery carries both signatures and your receiver switches without missing one.

Read events over a stream

Rather than run a receiver, hold a stream open with a key holding sandboxes:read: the same events, as the same JSON, arrive as Server-Sent Events while the connection lasts. Each carries a cursor. Keep the last one, and when the connection breaks, open the stream again from it to carry on after that event; a browser's EventSource does this by itself. A cursor is good for 7 days. One older than that ends the stream with invalid naming cursor, since events were missed: start again from a time.

for await (const { cursor, event } of pod.events.stream({ cursor: lastCursor })) {
  handle(event);
  lastCursor = cursor;
}