Skip to main content
Claude Code is Anthropic’s agentic coding tool. E2B provides a pre-built claude template with Claude Code already installed.

CLI

Create a sandbox with the E2B CLI.
Once inside the sandbox, start Claude Code.

Setup

The SDK examples below need two API keys in your environment:
  • E2B_API_KEY: your E2B API key.
  • ANTHROPIC_API_KEY: an Anthropic API key. The examples pass it into the sandbox, where Claude Code reads it.
Install the SDK. The TypeScript examples use top-level await, so the project must be an ES module.
Each example checks the key before it creates a sandbox, and kills the sandbox in a finally block (a with block in Python), so a failed run does not leave the sandbox running until it times out. Agent runs take minutes, so every claude command passes timeoutMs: 0 (timeout=0 in Python): the SDK stops a command after 60 seconds by default. The sandbox’s own timeoutMs (timeout) is how long the sandbox lives, which is a separate limit.

Run headless

Use -p for non-interactive mode and --dangerously-skip-permissions to auto-approve all tool calls. The sandbox isolates the agent from your host machine, but sandboxes can reach the open internet by default, so restrict outbound traffic with network rules if the agent processes untrusted input or handles secrets.
Auto-approving tool calls is contained by the sandbox: the agent cannot touch your host machine, local files, or credentials. It can still make outbound network requests — internet access is enabled by default. To limit where an auto-approved agent can connect, configure outbound network rules (allowInternetAccess, plus allow/deny lists by CIDR or hostname). Hostname rules apply to HTTP(S) traffic only; use CIDR rules for other protocols.
Claude Code writes files inside the sandbox. Read them back with sandbox.files.read(), or run git diff as in the next example.

Example: work on a cloned repository

Clone a repository, then run Claude Code inside it. The example clones a public repository. For a private one, put a token in the URL (https://x-access-token:$GITHUB_TOKEN@github.com/<owner>/<repo>.git) and pass GITHUB_TOKEN in the envs of that commands.run call.

Structured output

Use --output-format json to get a machine-readable result envelope with the session ID, cost, and the answer. The answer in result is free text; add --json-schema to get a parsed object that matches your schema in structured_output.

Streaming output

Use --output-format stream-json with --verbose (required in -p mode) to get a real-time JSONL event stream, including tool calls, token usage, and result metadata. Output arrives in chunks that can end mid-line (a large tool result is one long event), so buffer the stream and parse only complete lines.

Resume a session

Claude Code persists conversations that can be resumed with follow-up tasks using --resume.

Custom system prompt

Write a CLAUDE.md file into the repository for project context or use --system-prompt to provide task-specific instructions.

Connect MCP tools

Claude Code has built-in support for MCP. E2B provides an MCP gateway that gives Claude access to 200+ tools from the Docker MCP Catalog.

Build a custom template

If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own template on top of the pre-built claude template.
Run the build script to create the template.

MCP tools

Connect Claude Code to 200+ MCP tools

Sandbox persistence

Auto-pause, resume, and manage sandbox lifecycle