Lifecycle events
E2B reports what happens to each sandbox in the team to a webhook: a URL you register, which E2B calls with one signed event each time a sandbox is created, paused, resumed, checkpointed or killed. The event carries the sandbox’s metadata, so your handler can tell whose sandbox it was.
Each event is a sandbox changing state
Section titled “Each event is a sandbox changing state”Subscribe a Worker to events
Section titled “Subscribe a Worker to events”import * as Cloudflare from "@alchemy.run/cloudflare";import * as E2B from "@alchemy.run/e2b";import * as Effect from "effect/Effect";import * as HttpServerResponse from "effect/http/HttpServerResponse";
export default class Events extends Cloudflare.Worker<Events>()( "Events", { main: import.meta.url }, Effect.gen(function* () { yield* E2B.consumeSandboxEvents({ events: ["sandbox.lifecycle.killed"] }, (event) => Effect.log(`${event.sandbox_id} killed: ${event.event_data?.kill_reason}`), ); return { fetch: Effect.succeed(HttpServerResponse.text("ok")) }; }).pipe(Effect.provide(E2B.SandboxEventsLive)),) {}Deploying creates an E2B webhook that points at the Worker. Destroying the Worker deletes it.
Pick the events you need
Section titled “Pick the events you need”sandbox.lifecycle.created, updated, paused, resumed, checkpointed, and killed. Leave out events to get all of them:
yield* E2B.consumeSandboxEvents({}, (event) => Effect.log(`${event.type} for ${event.event_data?.sandbox_metadata?.user}`),);event.event_data.sandbox_metadata holds the sandbox’s metadata, so you can tell whose sandbox it was.
The Worker checks every delivery
Section titled “The Worker checks every delivery”Each request’s e2b-signature is checked before your handler runs. An unsigned or wrongly signed request gets a 401. Alchemy creates the signing secret unless you pass secret. E2B tries each event up to three times, so keep your handler idempotent on event.id.
Point a webhook at any endpoint
Section titled “Point a webhook at any endpoint”export const Lifecycle = E2B.Webhook("Lifecycle", { url: "https://hooks.example.com/e2b", events: ["sandbox.lifecycle.paused", "sandbox.lifecycle.killed"], signatureSecret: Config.Redacted("E2B_WEBHOOK_SECRET"),});Read its delivery history with E2B.ReadWebhook(Lifecycle): deliveries() and stats().