---
title: "Claude Code integration"
description: "Run Claude Code headless in an E2B sandbox with its own filesystem, terminal and git, then read back the diff. Starts from the prebuilt claude template."
url: "https://e2b.dev/integrations/claude-code"
---

# Claude Code

> Run Claude Code in its own sandbox with a real filesystem, terminal and git. The prebuilt claude template has it installed, so it can work unattended and you collect the diff.

Coding agents · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** E2B
- **How it works:** Prebuilt claude sandbox template
- **Package:** e2b
- **Language:** TypeScript, Python
- **Upstream:** [anthropics/claude-code · ★ 156k](https://github.com/anthropics/claude-code)
- **Example:** [e2b-cookbook/anthropic-claude-code-in-sandbox-js](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/anthropic-claude-code-in-sandbox-js)

## Get started

Paste the prompt into your coding agent, or follow the steps yourself.

### Paste this into your coding agent

```text
Run Claude Code in an E2B sandbox from this project.

1. Install the e2b SDK using the project's package manager.
2. The SDK needs the E2B_API_KEY environment variable, and Claude Code needs ANTHROPIC_API_KEY. If E2B_API_KEY isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode either key.
3. Follow the Claude Code guide at https://docs.e2b.dev/agents/claude-code: create a sandbox from the claude template, pass ANTHROPIC_API_KEY in envs, and run claude -p with --dangerously-skip-permissions and a command timeout of 0. Kill the sandbox in a finally block.
4. Run one task and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Install the E2B SDK

```bash
npm init -y && npm pkg set type=module
npm install e2b
npm install -D tsx
```

### 2. Set your API keys

```bash
export E2B_API_KEY="e2b_..."
export ANTHROPIC_API_KEY="..."
```

### 3. Run Claude Code headless

```typescript
import { Sandbox } from 'e2b'

const apiKey = process.env.ANTHROPIC_API_KEY
if (!apiKey) throw new Error('Set ANTHROPIC_API_KEY')

const sandbox = await Sandbox.create('claude', {
  envs: { ANTHROPIC_API_KEY: apiKey },
})

try {
  const result = await sandbox.commands.run(
    `claude --dangerously-skip-permissions -p "Create a hello world HTTP server in Go"`,
    { cwd: '/home/user', timeoutMs: 0 }
  )
  console.log(result.stdout)
} finally {
  await sandbox.kill()
}
```

### 4. Work on a repository

```typescript
await sandbox.commands.run(
  'git clone --depth 1 https://github.com/e2b-dev/claude-code-fastapi.git /home/user/repo'
)

await sandbox.commands.run(
  `claude --dangerously-skip-permissions -p "Add error handling to all API endpoints"`,
  { cwd: '/home/user/repo', timeoutMs: 0 }
)

const diff = await sandbox.commands.run('git diff', { cwd: '/home/user/repo' })
console.log(diff.stdout)
```

## What runs in the sandbox

Claude Code and every command it runs live in the sandbox, away from your machine and its files.

- **Agent:** The claude CLI, auto-approving its own tool calls. The sandbox contains them, so it can't touch your host.
- **Files:** The repo you clone and everything Claude Code writes. Read results back with files.read() or git diff.
- **Sessions:** Conversations persist in the sandbox. Pass --resume with the session ID to send a follow-up task.
- **Network:** Internet access is on by default. Restrict outbound traffic with network rules for untrusted input or secrets.

## More coding agents

Other coding agents that run in E2B sandboxes.

- [OpenAI Codex](https://e2b.dev/integrations/codex.md): Run OpenAI's Codex CLI headless in a sandbox and collect the diff when it's done.
- [OpenCode](https://e2b.dev/integrations/opencode.md): Run OpenCode in a sandbox, or keep it local and point its workspaces at E2B with the @e2b/opencode plugin.
- [Gemini CLI](https://e2b.dev/integrations/gemini-cli.md): Run Google's Gemini CLI in a sandbox with full filesystem, terminal, and git access.

## Links

- [Get API key](https://console.e2b.dev/?tab=keys)
- [Read the docs](https://docs.e2b.dev/agents/claude-code)
- [E2B documentation](https://docs.e2b.dev)
