Webhooks

Project webhooks for sandbox lifecycle events: switched on in the beta (sandal capabilities lists webhooks), with the delivery contract still being finalized. How to verify a delivery, and how to poll.


What to do today#

Every Sandal mutation returns an operation you can wait on, so you rarely need push notifications for your own actions:

  • Lifecycle (create, start, stop, archive, recover, fork, delete): sandal.sandboxes.waitForOperation(operation.id, { timeoutMs }), or poll GET /v1/organizations/{organizationId}/projects/{projectId}/sandbox-operations/{operationId} until state is succeeded or failed.
  • Snapshots: poll sandboxes.getSnapshot(sandboxId, snapshotId) until state is ready.
  • Port exposures: poll sandboxes.getPortExposure(sandboxId, exposureId) until state is ready.
  • Processes: processes.streamEvents(sandboxId, processId) long-polls output until the process ends.
  • Changes you didn't make (automatic stop/archive/delete from a lifecycle policy): read sandboxes.get(id) or sandboxes.list({ state }) on a schedule.

See Sandboxes and the API reference for the exact calls.

What is planned#

The API spec already describes project webhooks, and we'll document them here once they are live on the beta deployment. The planned contract, so you can design for it:

  • You register an HTTPS endpoint per project and receive a signing secret once.
  • Deliveries are signed: the sandal-webhook-signature header carries one or more comma-separated v1= entries, each an HMAC-SHA256 of {timestamp}.{raw body} in lowercase hex, and sandal-webhook-timestamp carries the timestamp in epoch milliseconds. During a secret rotation both secrets sign, so either keeps verifying.
  • Delivery is at-least-once; deduplicate by the sandal-delivery-id header.
  • Failed deliveries are retried, and a delivery log supports manual redelivery.

Retry schedule and delivery guarantees are not final. This page will list them once they are final.

Sandbox lifecycle events#

A project webhook can select sandbox.running, sandbox.paused, sandbox.stopped, sandbox.archived, sandbox.failed and sandbox.deleted (or workload.lifecycle for all of them; the event name is then in event). The body carries apiVersion: "sandal.dev/v1", id, type, createdAtEpochMs and data with action, workload { id, kind: "linux-sandbox", generation, revision, phase }, state and lifecycle { trigger, action, operationId } (trigger is api, expiry, system, auto-stop, auto-archive-start, auto-archive or auto-delete). Ignore fields you do not know; new ones are added without notice.

From API 13.59 a host loss is visible in the payload:

  • sandbox.failed carries data.failure { code, message? }; code: "host_lost" means the sandbox's host was lost and it could not move to another one.
  • A sandbox restarted on another host from its newest checkpoint carries data.recovery { reason: "host-loss", checkpointStartedAt, checkpointStartedAtEpochMs, hostLossDetectedAt, hostLossDetectedAtEpochMs, dataLossWindowMs }. Work done after the checkpoint started may be lost; dataLossWindowMs is an upper bound.

The SDKs type these bodies (0.1.0-beta.7): TypeScript isSandalSandboxLifecycleWebhookEvent(body) narrows to SandalSandboxLifecycleWebhookEvent, Python is_sandbox_lifecycle_webhook_event(body) with the SandalSandboxLifecycleWebhookEvent TypedDict, Go ParseSandalSandboxLifecycleWebhookEvent(body). All three export the host_lost code as a constant.

Verifying a signature#

Verify against the exact raw body before parsing JSON, reject timestamps outside a 5-minute window, and compare in constant time:

import { createHmac, timingSafeEqual } from "node:crypto";

export function verifySandalWebhook(rawBody: Buffer, signatureHeader: string, timestampHeader: string, secret: string) {
  if (!/^\d{1,16}$/.test(timestampHeader)) throw new Error("malformed timestamp");
  if (Math.abs(Date.now() - Number(timestampHeader)) > 5 * 60_000) throw new Error("stale webhook");

  const expected = createHmac("sha256", secret)
    .update(Buffer.concat([Buffer.from(`${timestampHeader}.`), rawBody]))
    .digest("hex");
  const candidates = signatureHeader
    .split(",")
    .map((entry) => entry.trim())
    .filter((entry) => entry.startsWith("v1="))
    .map((entry) => entry.slice(3));
  const ok = candidates.some(
    (candidate) => candidate.length === expected.length && timingSafeEqual(Buffer.from(candidate), Buffer.from(expected)),
  );
  if (!ok) throw new Error("invalid webhook signature");
}

The TypeScript SDK also ships this check as verifySandalWebhook from @sandal-dev/sdk.

If you need webhooks for a beta integration, tell us at hello@sandal.dev.