Skip to content

E2B

E2B runs each sandbox in its own Firecracker microVM. Declare the image your sandboxes boot from in your Stack, then create, run, pause, fork, and kill sandboxes from any Worker or Function.

New here? Set up credentials first, then build the tutorial. Concepts explains how the pieces fit.

src/Sandbox.ts
import * as Dockerfile from "@alchemy.run/docker/Dockerfile";
import * as E2B from "@alchemy.run/e2b";
export const Agent = E2B.Template("Agent", {
dockerfile: Dockerfile.inline`
FROM node:22-bookworm
RUN apt-get update && apt-get install -y git ripgrep
`,
cpuCount: 2,
memoryMB: 2048,
});

E2B builds the template on its side, so deploys need no Docker. See Templates.

src/Api.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as E2B from "@alchemy.run/e2b";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Agent } from "./Sandbox.ts";
export default class Api extends Cloudflare.Worker<Api>()(
"Api",
{ main: import.meta.url },
Effect.gen(function* () {
const sandboxes = yield* E2B.CreateSandbox(Agent);
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const sandbox = yield* Effect.acquireRelease(
sandboxes.create({ timeout: 120, network: { allowOut: [] } }),
(sandbox) => Effect.ignore(sandbox.kill()),
);
const code = yield* request.text;
yield* sandbox.files.write("main.mjs", code);
const result = yield* sandbox.exec("node main.mjs");
return yield* HttpServerResponse.json(result);
}).pipe(Effect.scoped, Effect.orDie),
};
}).pipe(Effect.provide(E2B.CreateSandboxHttp)),
) {}

create takes E2B’s own parameters. The sandbox is killed when the request ends. See Sandboxes.

E2B’s /admin and /nodes endpoints are for E2B’s own operators and are not part of this provider. envd’s init, freeze, fsfreeze and collapse calls are made by E2B’s orchestrator, not by users. The team list, status and limits views need a personal access token and describe your account rather than infrastructure; use the dashboard.