Webhooks
One POST per changed row
Section titled “One POST per changed row”import { defineTapeDB } from "@tapedb/tapedb/worker";import schema from "./tapedb.schema";
export default defineTapeDB({ schema, webhooks: [ { url: "https://<worker-name>.<account-subdomain>.workers.dev/tapedb", events: ["create", "update", "delete"], models: ["task"], secret: "<the endpoint's signing key>", }, ],});| Option | |
|---|---|
url |
where the POST goes; http or https, checked when the Worker is defined |
events |
at least one of create, update, delete |
models |
the models by API name; every model when left out, and a name the schema does not have is refused at definition time |
secret |
the HMAC-SHA256 key for the signature header |
A typo fails at definition, not at the first write. The key is a string on the option, and defineTapeDB runs at module scope where a Worker’s env does not exist, so the value has to reach the module as a value rather than be read from a binding there.
Capture inside the write, delivery after it
Section titled “Capture inside the write, delivery after it”Change capture is a set of SQLite triggers in the write’s own transaction, shaped like the audit and personal-data triggers: each changed row records (seq, table, row id, action) against the transaction’s log line. That is where three properties come from:
- the event carries the sequence the write was given, so events can be ordered;
- a cascade delete is captured too, because the trigger sees each row it removes;
- a rolled-back transaction leaves nothing behind, so a webhook can never fail or slow a commit.
Delivery happens after the commit, from the durability alarm, once the write has answered. A consumer that is slow, down, or answering 500 costs retries — never a failed write.
One row can be written more than once in a transaction, so the events are folded per row: an insert that is later updated is still a create, anything else takes its last action (an update that ends in a delete is a delete), and a row created and deleted in the same transaction produces nothing.
The request
Section titled “The request”{ "eventId": "8f3c1f8e-6c2a-4f3f-9a1f-2d7a1b0c9e44", "seq": 10432, "workspaceId": "0190a5b2-7c3d-4e8f-9a0b-1c2d3e4f5a6b", "model": "task", "op": "update", "at": 1795200000000, "row": { "id": "0190a5b2-…", "title": "Ship it", "done": true }}at is when the write committed, in epoch milliseconds. row is the row after the write, or the values it had for a delete; it is null when the row could not be read. Personal fields never appear in a payload, neither as plaintext nor as ciphertext.
Each delivery carries its own headers:
| Header | |
|---|---|
X-Tapedb-Signature |
t=<unix seconds>,v1=<hex> |
X-Tapedb-Event |
create, update or delete |
X-Tapedb-Event-Id |
the payload’s eventId |
X-Tapedb-Seq |
the transaction’s sequence |
X-Tapedb-Model |
the model’s API name |
content-type |
application/json |
v1 is HMAC-SHA256 over `${t}.${body}` with the endpoint’s secret, where body is the exact bytes posted — the JSON text byte for byte, never a re-serialisation of a parsed object. The timestamp is part of the signed string so a consumer can reject an old signature, and it is the attempt’s time, not the event’s: a retry carries a fresh signature over the same body. Compare the MACs in constant time, and reject a t far from your own clock — that is what makes a replay refusable.
// Verifying is the receiver's job; this is the shape, not a shipped helper.const [t, v1] = header.split(",").map((part) => part.split("=")[1]);const expected = await hmacSha256Hex(secret, `${t}.${rawBody}`);if (!timingSafeEqual(expected, v1)) return new Response(null, { status: 401 });Delivery guarantees
Section titled “Delivery guarantees”- At least once. An event is dropped from the outbox only after a 2xx, so a delivery whose answer was lost is sent again.
eventIdis generated once, when the event is written, and is identical on every attempt — dedupe on it, not on the payload’s contents. - Ordered per endpoint by
seq. A failed delivery blocks that endpoint’s later events while it retries; a healthy endpoint is not held up by a failing one. - Durable. Pending events live in the workspace’s own SQLite, so hibernation or eviction resumes where it stopped.
- Retries. The first retry waits 30 seconds and the wait doubles per attempt, capped at 30 minutes. Past 24 hours, or 60 attempts, the event is dead-lettered: it stays in the outbox, is no longer attempted, and is counted.
- Counted.
GET /engine/workspaces/:id/statusreports the outbox aswebhooks: { pending, delivered, dead }when the deployment delivers them.
Webhooks are experimental.