E2B and Claude Code Sandbox Integration

Benefits of Secure Sandbox Execution

Running Claude Code in an isolated E2B cloud sandbox provides essential security and development advantages:

Benefit Description
🔒 Complete Isolation Code runs in a separate cloud environment with zero access to your local system, files, or network.
🛡️ No Local Impact Eliminates risk of system modifications, file corruption, or security vulnerabilities on your machine.
🚀 Clean Environment Fresh Ubuntu with Claude Code pre-installed, ensuring consistent execution without dependency conflicts.
☁️ Remote Execution Intensive operations run in the cloud without consuming your local CPU, memory, or storage.
🧪 Safe Testing Test new agents and commands risk-free, with automatic cleanup after execution.

E2B Claude Code Sandbox Component

Claude Code Templates provides a ready-to-use E2B sandbox integration:

🔧 What's Included

Component Description
E2B Launcher Script Python script that creates, configures, and manages E2B sandbox instances with Claude Code.
Automatic Setup Downloads dependencies, checks Python environment, and installs E2B SDK automatically.
Component Integration Installs any specified agents, commands, MCPs, settings, and hooks inside the sandbox.

Prerequisites

You'll need two API keys to use the E2B sandbox:

Installation and Usage

Quick Start - Simple Execution

# Execute a prompt in secure E2B sandbox with API keys
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --prompt "Create a React todo app with TypeScript"

# Or use environment variables (set E2B_API_KEY and ANTHROPIC_API_KEY)
npx claude-code-templates@latest --sandbox e2b --prompt "Create a React todo app with TypeScript"

# Interactive mode - let the CLI guide you
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key

This command will:

  • ✓ Create secure E2B cloud sandbox
  • ✓ Execute your prompt using Claude Code
  • ✓ Download generated files to `sandbox-xxxxx` folder
  • ✓ Automatically cleanup after execution

Advanced Usage - With Specific Agent

# Execute with a specific agent in sandbox
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent development-team/frontend-developer \
  --prompt "Create a modern todo app with TypeScript and Tailwind"

Full Stack Development

# Complete full-stack app development
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent development-team/fullstack-developer \
  --prompt "Create a Node.js API with PostgreSQL and authentication"

Data Science and Analysis

# Data analysis with specialized agent
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent ai-ml-specialists/data-scientist \
  --prompt "Analyze this dataset and create interactive visualizations"

API Key Configuration

You can provide your E2B and Anthropic API keys in two convenient ways:

🔑 Option 1: CLI Parameters (Recommended)

# Pass API keys directly as command parameters
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_api_key_here \
  --anthropic-api-key your_anthropic_api_key_here \
  --prompt "Your prompt here"

Benefits:

  • ✓ No need to set up environment variables
  • ✓ Works immediately without additional configuration
  • ✓ Perfect for CI/CD pipelines and automation
  • ✓ No risk of accidentally committing keys to repositories

🌍 Option 2: Environment Variables

# Set environment variables in your shell
export E2B_API_KEY=your_e2b_api_key_here
export ANTHROPIC_API_KEY=your_anthropic_api_key_here

# Then run without API key parameters
npx claude-code-templates@latest --sandbox e2b --prompt "Your prompt here"

Note: CLI parameters take precedence over environment variables, so you can override environment settings when needed.

Environment Configuration

After first execution, the sandbox component creates these files in your project:

your-project/
├── .claude/
│   └── sandbox/
│       ├── e2b-launcher.py      # Python launcher script
│       ├── requirements.txt     # E2B SDK dependencies
│       └── .env.example         # Environment variables template
└── sandbox-xxxxxxxx/            # Generated project files (after execution)
    ├── index.html               # Your generated files
    ├── src/                     # Project source code
    └── ...

Use Cases - When You Need Sandbox Isolation

🔥 System-Level Operations

# Modify system configurations without risk
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent devops-cloud/system-administrator \
  --prompt "Set up nginx with multiple virtual hosts, SSL certificates, and firewall rules"

🧪 Experimental Package Installation

# Test unknown dependencies and packages safely
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent development-team/fullstack-developer \
  --prompt "Install and configure a complex tech stack with Redis, RabbitMQ, Elasticsearch, and their dependencies"

🛡️ Security Vulnerability Testing

# Run penetration testing tools without affecting your system
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent security-testing/penetration-tester \
  --prompt "Set up and run OWASP ZAP, Metasploit, and Nmap to test this application"

💣 Database Operations

# Perform destructive database migrations and schema changes
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent data-analytics/database-administrator \
  --prompt "Create a PostgreSQL database, run complex migrations, drop tables, and test backup/restore procedures"

Troubleshooting

Common Issues and Solutions

Issue Solution
Python Not Found Install Python 3.11+ from python.org/downloads
API Keys Not Set Create .env file in .claude/sandbox/ with your E2B and Anthropic API keys
Dependencies Failed Run pip3 install -r .claude/sandbox/requirements.txt manually
Sandbox Timeout Large prompts may take time; configured with 15-minute timeout for complex operations
Back to Blog