Skip to content

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.

user-42sandboxrunningpausedrunningEventsWorkercreatedpausedresumedkilleduser-42sandboxrunningpausedrunningEventsWorkercreatedpausedresumedkilled
Each change in the sandbox's state is one event, and E2B calls the Worker once for each.
src/Events.ts
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.

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.

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.

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().