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.
Declare a template
Section titled “Declare a template”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.
Run code from a Worker
Section titled “Run code from a Worker”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.
Where next
Section titled “Where next”- Snapshots & forks: a warm base every sandbox starts from
- Template programs: your Effect program, running in every sandbox
- Volumes: files that outlive sandboxes, one per user if you like
- Network & secrets: egress rules and credentials injected on the way out
- Lifecycle events: react when sandboxes start, pause, and die
- API keys
Out of scope
Section titled “Out of scope”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.