Skip to content

An open-source, faster Devin alternative

TypeScript

webhooks rev5: one canonical camelCase contract + close 9 review gaps

Igor Zalutskibad9e07b8147
diff --git a/.agents/work/sandbox-lifecycle-webhooks.md b/.agents/work/sandbox-lifecycle-webhooks.md
index 93bc3109..5f2e594a 100644
--- a/.agents/work/sandbox-lifecycle-webhooks.md
+++ b/.agents/work/sandbox-lifecycle-webhooks.md
@@ -18,6 +18,12 @@ without the conversation that produced it.
(base64 secret, SDK/docs convergence); **deletion cancels** non-terminal deliveries (new
`canceled` terminal status); a **skew-free watermark** (Redis stream id, not producer ts);
`hibernated`/`resumed` moved to the durable path; `/test` + redelivery semantics; P3 leftovers.
+- **Rev 5** makes **one canonical public JSON contract** (new §2b) and forces the design doc,
+ docs, API reference, and SDK to use it verbatim — the whole wire is **camelCase** (matching
+ core: `templateID`/`sandboxID`), DB columns stay snake_case. Plus: idempotent replay returns
+ the original response incl. the one-time secret; `sandboxId` scope is **immutable**; secret
+ rotation is `rotateSecret: true`; redeliver returns the delivery; event `data` is camelCase;
+ at-least-once is scoped to "once accepted."
## Goal
@@ -113,7 +119,7 @@ adopts the stronger form; where sessions-api differs today, a follow-up brings i
| Aspect | The contract (core, the new standard) | sessions-api today | Action |
|---|---|---|---|
-| **Envelope casing** | body is **camelCase** (`type`, `sandboxId`/`sessionId`, `eventId`, `metadata`, `event{…}`); the REST *management* API stays snake_case | camelCase already (`sessionId`, `eventId`) | core match; consistent |
+| **Wire casing** | the **whole** wire JSON is **camelCase** — envelope *and* management request/response (`eventTypes`, `sandboxId`, `hasSecret`, `retryCount`, …), matching core (`templateID`/`sandboxID`). DB columns stay snake_case; the API maps. See §2b. | camelCase already (`sessionId`, `eventId`) | core match; consistent |
| **`webhook-id` meaning** | = **`delivery.id`** (the message; stable across retries *and* manual redelivery); `eventId` lives in the body | uses `event_id` | **sessions-api follow-up** + **SDK/docs**: switch to delivery id and update the `WebhookDelivery` type + verifier comment (until then the two products differ — "one verifier" is true only *after* this lands, P2-c) |
| **Signing** | every delivery signed; secret **auto-generated** (`whsec_…`) if none supplied, returned **once**; no unsigned path | unsigned allowed when no secret | **sessions-api follow-up** |
| **Secret echo** | return the secret **only when we generated it**; never echo a caller-supplied one | n/a | core decides |
@@ -126,6 +132,38 @@ adopts the stronger form; where sessions-api differs today, a follow-up brings i
The sessions-api follow-ups (delivery-id `webhook-id`, mandatory generated secrets, soft-delete)
are **out of scope for this branch** but should be filed so the two products converge.
+## 2b. The canonical public JSON contract
+
+**This is the single source of truth for the wire JSON** — request bodies, responses, and the
+delivered envelope. It is **camelCase**, matching the rest of the OpenComputer API. **DB
+columns are snake_case (§4); the API layer maps between them.** The design doc, user docs, API
+reference, SDK types, and every example MUST use these field names verbatim. (sessions-api is
+snake_case on its wire — a separate, pre-existing product; the SDKs present camelCase for both.)
+
+```
+Destination (response)
+ { id, name, url, eventTypes, sandboxId, enabled, hasSecret, createdAt, updatedAt }
+ + secret ← present ONCE, only on create or a generated rotation
+
+Create request { url, name?, secret?, eventTypes?, sandboxId?, enabled? } + Idempotency-Key header
+Update request { url?, eventTypes? (null = clear), enabled?, secret?, rotateSecret? }
+ — sandboxId (scope) is IMMUTABLE; not accepted here
+
+Delivery (record)
+ { id, destination, eventId, eventType, status, attempts, retryCount,
+ lastAttemptAt, responseCode?, error?, createdAt, updatedAt, deliveredAt? }
+Delivery list { data, nextCursor, hasMore }
+Test result { delivered, responseCode?, error? }
+
+Envelope (delivered body; webhook-id header == deliveryId)
+ { type, sandboxId, eventId, deliveryId, metadata,
+ event: { id, ts, orgId, sandboxId, type, data } }
+
+Event `data` is camelCase too:
+ sandbox.stopped → { reason } sandbox.forked → { parentId }
+ sandbox.scaled → { cpuCount, memoryMB } sandbox.preview_url.changed → { port, url }
+```
+
## 3. Event taxonomy (the public surface)
Each delivered event is one sandbox lifecycle moment. **Several already emit on the Redis
@@ -147,7 +185,7 @@ booted and usable for exec; `resumed` (not "woke") pairs with `hibernated`.
| `sandbox.migrated` | B | ✅ (`"migrated"`) | worker migrate | |
| `sandbox.stopped` | A | ✅ (`"stopped"`) | `db/store.go:UpdateSandboxSessionStatus` (project in-tx) | `data.reason` incl. `user_requested`, `expired`, `crash` (P2-g/O6: no separate `crashed` type) |
| `sandbox.checkpoint.created` | A | ❌ **gap** | `api/sandbox.go:createCheckpoint` | net-new emit |
-| `sandbox.forked` | A | ⚠️ partial | `api/sandbox.go:forkSandbox` | child also emits `created`; this carries `data.parent_id` |
+| `sandbox.forked` | A | ⚠️ partial | `api/sandbox.go:forkSandbox` | child also emits `created`; this carries `data.parentId` |
| `sandbox.scaled` | A | ❌ **gap** | `api/sandbox.go:setSandboxResourceLimits` + `OnSandboxScale` | net-new emit |
| `sandbox.preview_url.changed` | A | ❌ **gap** | `api/sandbox.go:updateSandboxPort` | net-new emit |
@@ -251,12 +289,16 @@ CREATE INDEX webhook_deliveries_due_idx ON webhook_deliveries(next_attempt_at)
CREATE INDEX webhook_deliveries_dest_idx ON webhook_deliveries(destination_id, created_at DESC);
-- Idempotency-Key storage for POST /api/webhooks (P1-b). Same (org,key) + same request → replay
--- the stored result; same key + different request → 409. Prune rows older than the TTL (~24h).
+-- the stored ORIGINAL RESPONSE (incl. the one-time generated secret) so a client that lost the
+-- first response still gets the secret it needs to verify; same key + different request → 409.
+-- response_enc holds the rendered create response, encrypted (it carries the plaintext secret);
+-- it (and the replayability of the secret) expires with the row at the TTL (~24h). Prune past TTL.
CREATE TABLE webhook_idempotency_keys (
org_id uuid NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
key text NOT NULL, -- caller's Idempotency-Key header
- request_hash text NOT NULL, -- fingerprint of the create body (url+event_types+sandbox_id+name)
+ request_hash text NOT NULL, -- fingerprint of the create body (url+eventTypes+sandboxId+name)
destination_id text NOT NULL REFERENCES webhook_destinations(id) ON DELETE CASCADE,
+ response_enc bytea NOT NULL, -- the original create response (incl. secret), encrypted
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (org_id, key)
);
@@ -428,14 +470,14 @@ Echo handlers in `internal/api/webhooks.go`, wired in `router.go`, store methods
| Method · Path | Purpose |
|---|---|
-| `POST /api/webhooks` | Register a standalone destination: `{ url, name?, secret?, event_types?, sandbox_id?, enabled? }` → SSRF-validate. Idempotency + secret-echo below. |
+| `POST /api/webhooks` | Register a standalone destination: `{ url, name?, secret?, eventTypes?, sandboxId?, enabled? }` → SSRF-validate. Idempotency + secret-echo below. |
| `GET /api/webhooks` | List org destinations (excludes soft-deleted) |
| `GET /api/webhooks/:id` | One destination (no secret; `has_secret: true`) |
-| `PATCH /api/webhooks/:id` | Update url/event_types/sandbox_id/enabled; rotate secret. A **generated** rotation returns the new value once; a caller-supplied one is not echoed. |
+| `PATCH /api/webhooks/:id` | Update `url`/`eventTypes`/`enabled`; set a caller-supplied `secret`, or `rotateSecret: true` to mint a new one (returned once). **`sandboxId` (scope) is immutable** — not accepted here. A generated rotation returns the new value once; a caller-supplied one is not echoed. |
| `DELETE /api/webhooks/:id` | **Soft-delete** (P2-f): set `deleted_at`, disable, and **cancel non-terminal deliveries** for it (`status='canceled'`, `error='destination_deleted'` — P1-d). History is retained; the destination drops out of `GET`. |
| `GET /api/webhooks/:id/deliveries?status=&cursor=&limit=` | Delivery history (paginated, see below) |
| `GET /api/webhooks/:id/deliveries/:deliveryId` | One delivery, detail |
-| `POST /api/webhooks/:id/deliveries/:deliveryId/redeliver` | Re-enqueue **any** delivery: `status='pending'`, `next_attempt_at=now()`, clear lock, **reset `retry_count=0`** (fresh budget), **preserve lifetime `attempts`** (P1-c). **Same `webhook-id` (= delivery id)** — so a receiver that dedupes will treat it as the *same* message; redelivery means "send this delivery again," for when the original never landed, not "emit a new logical event" (P2-e). |
+| `POST /api/webhooks/:id/deliveries/:deliveryId/redeliver` | Re-enqueue **any** delivery: `status='pending'`, `next_attempt_at=now()`, clear lock, **reset `retry_count=0`** (fresh budget), **preserve lifetime `attempts`** (P1-c). **Same `webhook-id` (= delivery id)** — so a receiver that dedupes will treat it as the *same* message; redelivery means "send this delivery again," for when the original never landed, not "emit a new logical event" (P2-e). **Returns the re-enqueued delivery record.** |
| `POST /api/webhooks/:id/test` | **Synchronous** connectivity check: signs + POSTs a synthetic `sandbox.test` event to the URL and returns the HTTP status inline. It **bypasses `event_types`**, does **not** create a delivery row, does **not** appear in history, and is **not** retried/dead-lettered (P2-d). |
**Subscribe at create (P1-a).** A per-sandbox integrator usually doesn't know `sandbox_id`
@@ -449,11 +491,15 @@ an **org-wide** destination (no `sandbox_id`) + route on the body's `sandboxId`/
**Idempotent create (P1-b).** Two mechanisms, explicit conflict semantics:
- **`name`** (unique per org): `POST` with an existing `name` whose incoming config (url +
- event_types + sandbox_id) **matches** → `200` with the existing destination; config
- **differs** → **`409 Conflict`** (never silently reuse or overwrite).
-- **`Idempotency-Key` header**: stored in `webhook_idempotency_keys` with a request fingerprint
- (§4). Same key + same fingerprint → replay the original result; same key + **different**
- fingerprint → **`409`**. Prune keys past a ~24h TTL.
+ eventTypes + sandboxId) **matches** → `200` with the existing destination; config
+ **differs** → **`409 Conflict`** (never silently reuse or overwrite). NB: a `name`-only
+ match returns the destination *without* the secret (it's write-only) — so a client that
+ needs guaranteed secret recovery should use an `Idempotency-Key` (below) or supply its own.
+- **`Idempotency-Key` header**: the **original create response is stored encrypted**
+ (`webhook_idempotency_keys.response_enc`, §4) with a request fingerprint. Same key + same
+ fingerprint → **replay that exact response, including the one-time generated `secret`** (so a
+ client that lost the first response still gets it); same key + **different** fingerprint →
+ **`409`**. Both the stored response and the secret-replay expire with the row at a ~24h TTL.
**Secret echo (P2-d).** Return `secret` **only when we generated it** (caller supplied none),
on create or a generated rotation; never echo a caller-supplied secret (`has_secret: true`).
@@ -462,11 +508,11 @@ on create or a generated rotation; never echo a caller-supplied secret (`has_sec
**opaque stable cursor** (encodes `(created_at, id)`), not a bare timestamp. `next_cursor` is
`null` when `has_more` is false.
-Destination response: `{ id, name, url, event_types, sandbox_id, enabled, has_secret,
-created_at, updated_at }` — plus `secret` **only** when freshly generated (create, or a
-generated rotation). Delivery response: `{ id, destination, event_id, event_type, status,
-attempts, retry_count, last_attempt_at, response_code, error, created_at, updated_at,
-delivered_at }` (all backed by columns — §4; `updated_at` included per P3).
+Destination response: `{ id, name, url, eventTypes, sandboxId, enabled, hasSecret, createdAt,
+updatedAt }` — plus `secret` **only** when freshly generated (create, or a generated rotation).
+Delivery response: `{ id, destination, eventId, eventType, status, attempts, retryCount,
+lastAttemptAt, responseCode, error, createdAt, updatedAt, deliveredAt }`. List responses:
+`{ data, nextCursor, hasMore }`. (camelCase wire per §2b; backed by snake_case columns in §4.)
## 11. Reliability semantics (the contract we're promising)
diff --git a/docs/agent-sessions/webhooks.mdx b/docs/agent-sessions/webhooks.mdx
index 946fa742..19813d93 100644
--- a/docs/agent-sessions/webhooks.mdx
+++ b/docs/agent-sessions/webhooks.mdx
@@ -161,6 +161,6 @@ function verify(headers, rawBody, secret) {
}
```
-<Note>These deliver a **session's** events (turns, messages, results). To react to **sandbox lifecycle** — boot, hibernate, stop, scale — see [Sandbox webhooks](/sandboxes/webhooks): the same envelope shape, Standard Webhooks signing, and the same `verifyWebhook` helper.</Note>
+<Note>These deliver a **session's** events (turns, messages, results). To react to **sandbox lifecycle** — boot, hibernate, stop, scale — see [Sandbox webhooks](/sandboxes/webhooks): verified with the same `verifyWebhook` helper. (Sandbox webhooks are always signed and use the delivery id as `webhook-id`.)</Note>
<Note>Today, delivery is over **generic webhooks**. Slack, GitHub comments, Jira, and email are coming soon — all on this same event and delivery model.</Note>
diff --git a/docs/api-reference/sandboxes/create.mdx b/docs/api-reference/sandboxes/create.mdx
index da19b24d..22af45ea 100644
--- a/docs/api-reference/sandboxes/create.mdx
+++ b/docs/api-reference/sandboxes/create.mdx
@@ -71,9 +71,12 @@ If both `cpuCount` and `memoryMB` are provided, they must match a platform tier.
"status": "running",
"region": "use2",
"workerID": "w-use2-abc123",
- "previewAuthToken": "qx2sSi5IYXWBvnnRqwK9Ky_cIAI-x0Vx1bPCt0XMxsI"
+ "previewAuthToken": "qx2sSi5IYXWBvnnRqwK9Ky_cIAI-x0Vx1bPCt0XMxsI",
+ "webhooks": [
+ { "id": "whk_3f9a2c", "url": "https://app.example.com/oc-webhook", "secret": "whsec_Hk9…" }
+ ]
}
```
-`previewAuthToken` is only present when `previewAuth` was set in the request. Read it once and store it durably — the server will not return it again.
+`previewAuthToken` is only present when `previewAuth` was set in the request. `webhooks` is present only when `webhooks` were requested; each `secret` (`whsec_…`) is returned here exactly once. Read both once and store them durably — the server will not return them again.
</ResponseExample>
diff --git a/docs/api-reference/webhooks/redeliver.mdx b/docs/api-reference/webhooks/redeliver.mdx
index 3feddffa..de586dd7 100644
--- a/docs/api-reference/webhooks/redeliver.mdx
+++ b/docs/api-reference/webhooks/redeliver.mdx
@@ -17,7 +17,20 @@ Re-send any delivery (not only dead-lettered). Re-enqueues it with a fresh retry
```json 202
{
"id": "whd_8c1d",
- "status": "pending"
+ "destination": "whk_3f9a2c",
+ "eventId": "sb-3f9a…:3:142",
+ "eventType": "sandbox.stopped",
+ "status": "pending",
+ "attempts": 2,
+ "retryCount": 0,
+ "lastAttemptAt": "2026-06-24T12:00:01Z",
+ "responseCode": null,
+ "error": null,
+ "createdAt": "2026-06-24T12:00:00Z",
+ "updatedAt": "2026-06-24T12:10:00Z",
+ "deliveredAt": null
}
```
</ResponseExample>
+
+Returns the re-enqueued delivery — `status: "pending"`, `retryCount` reset, lifetime `attempts` preserved.
diff --git a/docs/api-reference/webhooks/update.mdx b/docs/api-reference/webhooks/update.mdx
index fa842bb0..6a0fffb0 100644
--- a/docs/api-reference/webhooks/update.mdx
+++ b/docs/api-reference/webhooks/update.mdx
@@ -22,13 +22,19 @@ Update a destination — pause/resume, retune filters, change the URL, or rotate
</ParamField>
<ParamField body="secret" type="string">
- Rotate the signing secret. A generated rotation is returned once (like create).
+ Set a caller-supplied signing secret (you keep it; it isn't echoed back).
+</ParamField>
+
+<ParamField body="rotateSecret" type="boolean">
+ Generate a **new** signing secret. The new `whsec_…` is returned **once** in the response (like create). Use this instead of `secret` when you want OpenComputer to mint the secret.
</ParamField>
<ParamField body="name" type="string">
Rename the destination.
</ParamField>
+`sandboxId` (scope) is **immutable** — set it at create; it can't be changed here.
+
<ResponseExample>
```json 200
{
diff --git a/docs/sandboxes/webhooks.mdx b/docs/sandboxes/webhooks.mdx
index f7f81609..e9b4fbb4 100644
--- a/docs/sandboxes/webhooks.mdx
+++ b/docs/sandboxes/webhooks.mdx
@@ -7,7 +7,7 @@ tag: "Preview"
<Note>**Preview** — sandbox webhooks are newly available and still evolving; the shape may change. We'd love your feedback.</Note>
-Register a **destination** and OpenComputer delivers a sandbox's lifecycle events to it as a small, signed **delivery envelope** — at-least-once, retried, and redeliverable. Use it to react when a sandbox boots, hibernates, stops, or scales, without polling.
+Register a **destination** and OpenComputer delivers a sandbox's lifecycle events to it as a small, signed **delivery envelope** — signed, retried, and redeliverable. Use it to react when a sandbox boots, hibernates, stops, or scales, without polling.
The envelope is camelCase, with top-level routing fields, your sandbox [`metadata`](/api-reference/sandboxes/create), and the full event nested under `event`:
@@ -178,7 +178,7 @@ Redelivery re-sends the **same** delivery (same `webhook-id`) with a fresh retry
- **HTTPS only.** Private, loopback, link-local, and cloud-metadata addresses are refused (at registration and at send).
- **Always signed** with [Standard Webhooks](https://www.standardwebhooks.com). Every request carries `webhook-id` (= `deliveryId`), `webhook-timestamp`, and `webhook-signature` = `v1,<base64(HMAC-SHA256(secret, "{webhook-id}.{webhook-timestamp}.{rawBody}"))>`. Verify against the **raw request body, before parsing it**; the timestamp guards replay. Plus `X-OC-Delivery-ID` and `X-OC-Sandbox-ID` for correlation. Your `secret` is write-only.
-- **At-least-once.** Retried with exponential backoff; after the max attempts a delivery is **dead-lettered** (inspect and redeliver it). A duplicate can still arrive, so **dedupe on `webhook-id`** (= `deliveryId`).
+- **At-least-once, once accepted.** Once an event is accepted for delivery, it's retried with exponential backoff and, after the max attempts, **dead-lettered** (inspect and redeliver it) — so it arrives at least once. A duplicate can still arrive, so **dedupe on `webhook-id`** (= `deliveryId`).
- A delivery succeeds on HTTP `2xx`.
### Verify a webhook
@@ -199,4 +199,4 @@ if (delivery.type === "sandbox.stopped") {
Or verify with any [Standard Webhooks](https://www.standardwebhooks.com) library directly, against the raw body and the `webhook-id` / `webhook-timestamp` / `webhook-signature` headers.
-<Note>Webhooks here cover **sandbox lifecycle**. To deliver an agent's *session* events (turns, messages, results), see [Agent Sessions webhooks](/agent-sessions/webhooks) — same envelope shape, signing, and verifier.</Note>
+<Note>Webhooks here cover **sandbox lifecycle**. To deliver an agent's *session* events (turns, messages, results), see [Agent Sessions webhooks](/agent-sessions/webhooks) — verified with the same `verifyWebhook` helper. Session webhooks differ in a couple of specifics today: their `webhook-id` is the event id, and signing is opt-in (set a `secret`).</Note>
diff --git a/sdks/typescript/src/sandbox.ts b/sdks/typescript/src/sandbox.ts
index b88aa8ee..b2b3d26a 100644
--- a/sdks/typescript/src/sandbox.ts
+++ b/sdks/typescript/src/sandbox.ts
@@ -83,6 +83,9 @@ interface SandboxData {
/** Plaintext preview-URL bearer token. Returned exactly once on the create
* or rotate response when `previewAuth` was requested. */
previewAuthToken?: string;
+ /** Destinations registered via `create({ webhooks })`. A generated `secret` (`whsec_…`)
+ * is present once here — store it; it isn't returned again. */
+ webhooks?: Array<{ id: string; url: string; secret?: string }>;
}
export interface CheckpointInfo {
@@ -266,6 +269,13 @@ export class Sandbox {
*/
previewAuthToken: string;
+ /**
+ * Webhook destinations registered inline via `Sandbox.create({ webhooks: [...] })`. Each
+ * generated signing `secret` (`whsec_…`) is present here exactly once — read and store it
+ * now; the server won't return it again. Empty when none were requested or after `connect()`.
+ */
+ readonly webhooks: Array<{ id: string; url: string; secret?: string }>;
+
private constructor(data: SandboxData, apiUrl: string, apiKey: string) {
this.sandboxId = data.sandboxID;
this.id = this.sandboxId;
@@ -275,6 +285,7 @@ export class Sandbox {
this.connectUrl = data.connectURL || "";
this.token = data.token || "";
this.previewAuthToken = data.previewAuthToken || "";
+ this.webhooks = data.webhooks ?? [];
this._sandboxDomain = data.sandboxDomain || "";
// Always route through the CP — it handles readiness waiting and proxies to workers.
diff --git a/sdks/typescript/src/webhooks.ts b/sdks/typescript/src/webhooks.ts
index 886909f2..dea731c5 100644
--- a/sdks/typescript/src/webhooks.ts
+++ b/sdks/typescript/src/webhooks.ts
@@ -97,11 +97,15 @@ export interface UpdateWebhookParams {
/** Pass `null` to clear the allow-list (deliver all types). */
eventTypes?: string[] | null;
enabled?: boolean;
- /** Rotate the signing secret; a generated rotation is returned once (see {@link CreateWebhookResult}). */
+ /** Set a caller-supplied signing secret (you keep it; it isn't echoed back). */
secret?: string;
+ /** Generate a NEW signing secret — returned once on the response (see {@link CreateWebhookResult}). */
+ rotateSecret?: boolean;
name?: string;
}
+/** Scope (`sandboxId`) is immutable — set it at create; it can't be changed by update. */
+
// ---- deliveries (the control surface behind a deliveries dashboard) ----
export type WebhookDeliveryStatus =
@@ -201,15 +205,17 @@ export class WebhookDeliveries {
/**
* Re-send any delivery (not only dead-lettered ones). Gives the delivery a fresh retry
- * budget; the redelivery carries the **same** `webhook-id`, so a receiver that dedupes will
- * treat it as the same message — redelivery is for cases where the original never landed.
+ * budget and returns the re-enqueued record; the redelivery carries the **same** `webhook-id`,
+ * so a receiver that dedupes will treat it as the same message — redelivery is for cases
+ * where the original never landed.
*/
- async redeliver(destinationId: string, deliveryId: string): Promise<void> {
+ async redeliver(destinationId: string, deliveryId: string): Promise<WebhookDeliveryRecord> {
const resp = await fetch(`${this.apiUrl}/webhooks/${destinationId}/deliveries/${deliveryId}/redeliver`, {
method: "POST",
headers: this.headers,
});
if (!resp.ok) return fail(resp, "redeliver");
+ return resp.json();
}
}