Skip to content

An open-source, faster Devin alternative

TypeScript

docs/sdk(webhooks): consistency pass across docs + SDK

Igor Zalutskib06a74e75c46
diff --git a/docs/api-reference/sandboxes/create.mdx b/docs/api-reference/sandboxes/create.mdx
index 22af45ea..ae89b65f 100644
--- a/docs/api-reference/sandboxes/create.mdx
+++ b/docs/api-reference/sandboxes/create.mdx
@@ -51,6 +51,8 @@ If both `cpuCount` and `memoryMB` are provided, they must match a platform tier.
- `url` *(string, required)*: HTTPS endpoint.
- `secret` *(string)*: signing secret; omit and one is generated and returned once on the response (`webhooks[].secret`).
- `eventTypes` *(string[])*: event-type allow-list (default all).
+
+ Each spec is validated up-front (same rules as [`POST /api/webhooks`](/api-reference/webhooks/create)); an invalid `url` or `eventTypes` fails the whole create with `400` (the sandbox is not created). The response echoes `webhooks: [{ id, url, secret? }]`.
</ParamField>
<ParamField body="previewAuth" type="object">
diff --git a/docs/api-reference/webhooks/create.mdx b/docs/api-reference/webhooks/create.mdx
index e8fc36f3..34b199d2 100644
--- a/docs/api-reference/webhooks/create.mdx
+++ b/docs/api-reference/webhooks/create.mdx
@@ -29,7 +29,11 @@ Register a webhook destination for sandbox lifecycle events. See [Webhooks](/san
Whether the destination is active. Default `true`. `false` pauses delivery.
</ParamField>
-Pass an **`Idempotency-Key`** header to make a timed-out retry safe — it returns the same destination instead of creating a duplicate.
+Returns **`201`** for a new destination, or **`200`** when an existing destination matched by `name` (same config) — the `200` response does **not** include a `secret`.
+
+**Validation (all `400`):** `url` must be HTTPS and pass SSRF checks (private/loopback/link-local/cloud-metadata addresses are refused); each `eventTypes` entry must be a known type or a `prefix.*` wildcard; `sandboxId` must belong to your org.
+
+**Idempotency.** Pass an **`Idempotency-Key`** header to make a retry safe — a repeat returns the same destination (and the same one-time `secret`) instead of creating a duplicate. The same key with a *different* body returns `409`. If a same-key request is still in flight, you get `409` with `{"code":"idempotency_in_progress"}` and a `Retry-After` header — retryable; just retry after the delay (the SDK does this automatically).
<ResponseExample>
```json 201
diff --git a/docs/api-reference/webhooks/delete.mdx b/docs/api-reference/webhooks/delete.mdx
index 4d1dbc82..7a252a75 100644
--- a/docs/api-reference/webhooks/delete.mdx
+++ b/docs/api-reference/webhooks/delete.mdx
@@ -3,7 +3,7 @@ title: 'Delete Webhook'
api: 'DELETE /api/webhooks/{id}'
---
-Soft-delete a destination. It stops receiving events and drops out of [list](/api-reference/webhooks/list), but its delivery history is retained; any non-terminal deliveries are moved to `canceled`. See [Webhooks](/sandboxes/webhooks).
+Soft-delete a destination. It stops receiving events and drops out of [list](/api-reference/webhooks/list), but its delivery history is retained and still queryable via [deliveries](/api-reference/webhooks/deliveries-list). Any non-terminal deliveries (`pending`, `delivering`, `failed`) are moved to `canceled`; terminal ones (`delivered`, `dead_letter`) are left as-is. See [Webhooks](/sandboxes/webhooks).
<ParamField path="id" type="string" required>
Destination ID (`whk_…`).
diff --git a/docs/api-reference/webhooks/deliveries-list.mdx b/docs/api-reference/webhooks/deliveries-list.mdx
index c39db850..f37c9743 100644
--- a/docs/api-reference/webhooks/deliveries-list.mdx
+++ b/docs/api-reference/webhooks/deliveries-list.mdx
@@ -3,7 +3,7 @@ title: 'List Deliveries'
api: 'GET /api/webhooks/{id}/deliveries'
---
-List a destination's deliveries, cursor-paginated. See [Webhooks](/sandboxes/webhooks#deliveries).
+List a destination's deliveries, cursor-paginated. Works for soft-deleted destinations too — delivery history is retained. See [Webhooks](/sandboxes/webhooks#deliveries).
<ParamField path="id" type="string" required>
Destination ID (`whk_…`).
diff --git a/docs/api-reference/webhooks/update.mdx b/docs/api-reference/webhooks/update.mdx
index 6a0fffb0..b1662246 100644
--- a/docs/api-reference/webhooks/update.mdx
+++ b/docs/api-reference/webhooks/update.mdx
@@ -18,7 +18,7 @@ Update a destination — pause/resume, retune filters, change the URL, or rotate
</ParamField>
<ParamField body="enabled" type="boolean">
- Pause (`false`) or resume (`true`) delivery.
+ Pause (`false`) or resume (`true`) delivery. Pausing does **not** drop events — events that occur while paused are queued and delivered when you re-enable.
</ParamField>
<ParamField body="secret" type="string">
diff --git a/docs/sandboxes/webhooks.mdx b/docs/sandboxes/webhooks.mdx
index ec0b84b3..d8b73be0 100644
--- a/docs/sandboxes/webhooks.mdx
+++ b/docs/sandboxes/webhooks.mdx
@@ -47,7 +47,7 @@ Subscribe to all events, or filter with `eventTypes` (exact like `sandbox.stoppe
## Destinations
-A destination is **org-scoped** by default (all your sandboxes), or pinned to one sandbox with `sandbox_id`.
+A destination is **org-scoped** by default (all your sandboxes), or pinned to one sandbox with `sandboxId`. Set `enabled: false` to **pause** a destination — events that occur while paused are queued and delivered when you re-enable it, not dropped.
<CodeGroup>
@@ -177,9 +177,9 @@ Redelivery re-sends the **same** delivery (same `webhook-id`) with a fresh retry
## Signing, retries, and dedupe
- **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, 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`.
+- **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`, `X-OC-Sandbox-ID` (correlation) and `User-Agent: OpenComputer-Webhooks/1`. Your `secret` is write-only.
+- **At-least-once, once accepted.** Once an event is accepted it's retried until it succeeds or is **dead-lettered** (inspect + redeliver it). A duplicate can still arrive, so **dedupe on `webhook-id`** (= `deliveryId`).
+- **Success / retry / give-up.** A delivery succeeds on HTTP `2xx`. A `5xx`, network error, or timeout is **retried** on a backoff of `10s, 30s, 60s, 5m, 15m` (capped), up to **12 attempts**, then **`dead_letter`**. A `429` is retried honoring its `Retry-After`. A `4xx` (other than 429), a `3xx` redirect, or an SSRF-blocked address is treated as **permanent → immediate `dead_letter`** (no retries). Redirects are not followed.
### Verify a webhook
diff --git a/sdks/typescript/src/index.ts b/sdks/typescript/src/index.ts
index 90f02327..d34e86f5 100644
--- a/sdks/typescript/src/index.ts
+++ b/sdks/typescript/src/index.ts
@@ -33,8 +33,11 @@ export {
type WebhookDeliveryRecord,
type WebhookDeliveryStatus,
type WebhookTestResult,
+ type ListPage,
type SandboxWebhookDelivery,
type SandboxLifecycleEvent,
+ type SandboxLifecycleEventBase,
+ type SandboxStopReason,
type SandboxWebhookEventType,
} from "./webhooks.js";
export { SecretStore, type SecretStoreInfo, type SecretEntryInfo, type SecretStoreOpts, type CreateSecretStoreOpts, type UpdateSecretStoreOpts } from "./project.js";
diff --git a/sdks/typescript/src/webhooks.ts b/sdks/typescript/src/webhooks.ts
index dea731c5..004deb9b 100644
--- a/sdks/typescript/src/webhooks.ts
+++ b/sdks/typescript/src/webhooks.ts
@@ -34,17 +34,37 @@ export type SandboxWebhookEventType =
| "sandbox.preview_url.changed"
| "sandbox.test";
-/** The lifecycle event nested under `event` in a delivery. */
-export interface SandboxLifecycleEvent {
+/** Why a sandbox stopped (`sandbox.stopped` → `event.data.reason`). */
+export type SandboxStopReason = "user_requested" | "expired" | "crash";
+
+/** Fields common to every lifecycle event (nested under `event` in a delivery). */
+export interface SandboxLifecycleEventBase {
id: string;
ts: string;
orgId: string;
sandboxId: string;
- type: SandboxWebhookEventType;
- /** Event-specific fields, e.g. `{ reason }` on stopped, `{ parentId }` on forked. */
- data: Record<string, unknown>;
}
+/**
+ * The lifecycle event nested under `event` in a delivery — a discriminated union
+ * on `type`, so `event.data` is typed per event (e.g. narrow on
+ * `event.type === "sandbox.stopped"` to get `event.data.reason`).
+ */
+export type SandboxLifecycleEvent = SandboxLifecycleEventBase &
+ (
+ | { type: "sandbox.created"; data: { template?: string } }
+ | { type: "sandbox.ready"; data: Record<string, never> }
+ | { type: "sandbox.hibernated"; data: Record<string, never> }
+ | { type: "sandbox.resumed"; data: Record<string, never> }
+ | { type: "sandbox.stopped"; data: { reason: SandboxStopReason } }
+ | { type: "sandbox.migrated"; data: Record<string, never> }
+ | { type: "sandbox.checkpoint.created"; data: { checkpointId: string } }
+ | { type: "sandbox.forked"; data: { parentId: string } }
+ | { type: "sandbox.scaled"; data: { cpuCount: number; memoryMB: number } }
+ | { type: "sandbox.preview_url.changed"; data: { port: number; url: string | null } }
+ | { type: "sandbox.test"; data: Record<string, unknown> }
+ );
+
/**
* The delivered envelope for sandbox webhooks: the shared {@link WebhookDelivery} carrying a
* {@link SandboxLifecycleEvent} under `event`. On these deliveries `sandboxId` and `deliveryId`
@@ -242,16 +262,33 @@ export class Webhooks {
return h;
}
- /** Register a destination. The response includes `secret` once iff OpenComputer generated it. */
+ /**
+ * Register a destination. Returns HTTP 201 (new) or 200 (an existing destination
+ * matched by `name`). The response includes `secret` once iff OpenComputer
+ * generated it. With an `idempotencyKey`, a retried call returns the same
+ * destination + secret; if a same-key request is concurrently in flight the
+ * server replies 409 `idempotency_in_progress` (retryable) — this method
+ * automatically waits (honoring `Retry-After`) and retries a few times.
+ */
async create(params: CreateWebhookParams): Promise<CreateWebhookResult> {
const { idempotencyKey, ...body } = params;
- const resp = await fetch(`${this.apiUrl}/webhooks`, {
- method: "POST",
- headers: this.headers(idempotencyKey ? { "Idempotency-Key": idempotencyKey } : undefined),
- body: JSON.stringify(body),
- });
- if (!resp.ok) return fail(resp, "create webhook");
- return resp.json();
+ for (let attempt = 0; ; attempt++) {
+ const resp = await fetch(`${this.apiUrl}/webhooks`, {
+ method: "POST",
+ headers: this.headers(idempotencyKey ? { "Idempotency-Key": idempotencyKey } : undefined),
+ body: JSON.stringify(body),
+ });
+ if (resp.ok) return resp.json();
+ if (resp.status === 409 && attempt < 5) {
+ const body409 = (await resp.clone().json().catch(() => ({}))) as { code?: string };
+ if (body409.code === "idempotency_in_progress") {
+ const retryAfter = Number(resp.headers.get("Retry-After")) || 1;
+ await new Promise((r) => setTimeout(r, retryAfter * 1000));
+ continue;
+ }
+ }
+ return fail(resp, "create webhook");
+ }
}
async list(): Promise<WebhookDestination[]> {