Sandbox SDK
Providers

E2B

Run E2B hosted sandboxes through the normalized SDK API.

Provider docs

E2B provides hosted Linux sandboxes with files, processes, authenticated previews, and template snapshots.

Installation

Terminal
bun add @opencoredev/sandbox-sdk ai zod e2b

Authentication

Set E2B_API_KEY, or pass apiKey to e2b().

Run a command

e2b.ts
import { createSandbox } from "@opencoredev/sandbox-sdk";
import { e2b } from "@opencoredev/sandbox-sdk/e2b";

await using sandbox = await createSandbox({ provider: e2b() });
const result = await sandbox.run("node --version");
console.log(result.stdout);

Run an AI SDK agent

This provider works with AI SDK ToolLoopAgent through the normalized sandbox session. Pass the language model from your existing AI SDK provider or AI Gateway setup.

sandbox-agent.ts
import { ToolLoopAgent, type LanguageModel } from "ai";import { createSandbox } from "@opencoredev/sandbox-sdk";import {  createSandboxToolApproval,  createSandboxTools,  toAISandboxSession,} from "@opencoredev/sandbox-sdk/ai";import { e2b } from "@opencoredev/sandbox-sdk/e2b";export async function runSandboxAgent(model: LanguageModel) {  await using sandbox = await createSandbox({    provider: e2b(),  });  const aiSandbox = toAISandboxSession(sandbox);  const agent = new ToolLoopAgent({    model,    instructions: `Work only in the provided sandbox.\n\n${aiSandbox.description}`,    tools: createSandboxTools(),    toolApproval: createSandboxToolApproval(),  });  return await agent.generate({    prompt: "Inspect the repository, run its tests, and summarize the result.",    experimental_sandbox: aiSandbox,  });}

See the AI SDK guide for approval flows, direct session access, and HarnessAgent alternatives.

Files and processes

workspace.ts
await sandbox.files.write("index.mjs", `console.log("ready")`);
const process = await sandbox.processes.start("node index.mjs");

for await (const event of process.output()) {
  console.log(event.stream, event.data);
}

E2B supports background processes, stdin, cancellation, and separate stdout and stderr streams.

Ports and snapshots

Restricted previews keep the E2B access token inside preview.request(). Creating and deleting template snapshots is normalized; restoring one creates a new native sandbox and stays on sandbox.raw.

preview.ts
const preview = await sandbox.ports.expose(3000);
const response = await preview.request!("/");

Options

OptionTypeDefaultBehavior
apiKeystringE2B_API_KEYAuthenticates E2B requests.
templatestringE2B defaultCreates the sandbox from a named template.
timeoutnumberCreation timeoutOverrides the creation timeout in milliseconds.

Behavior

  • Requires Node.js 20.18.1 or newer and an E2B account.
  • Files are ephemeral unless captured in an E2B template snapshot.
  • Managed resume keeps the live sandbox reference in the current process.
  • PTY, templates, and native network controls remain available through sandbox.raw.

See Ports, compare exact modes in Compatibility, or connect E2B to HarnessAgent.

On this page