E2B and Claude Code Sandbox Integration

Why Use Claude Code in a Sandbox?

Running Claude Code in an isolated cloud environment provides multiple security and practical benefits:

🔒 Security Benefits

Benefit Description
Complete Isolation Code runs in a separate cloud environment with no access to your local system, files, or network.
No Local Impact Zero risk of system modifications, file corruption, or security vulnerabilities on your machine.
Temporary Environment Sandbox is automatically destroyed after execution, leaving no traces or residual files.
Controlled Execution Only specified components and prompts are executed, preventing unexpected operations.

🚀 Development Benefits

Benefit Description
Clean Environment Fresh Ubuntu environment with Claude Code pre-installed, no dependency conflicts.
Reproducible Results Consistent execution environment across different machines and operating systems.
Remote Execution Execute intensive operations without consuming local CPU, memory, or storage resources.
Component Testing Safely test new agents, commands, and configurations without affecting your local setup.

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.
Environment Template Pre-configured environment variables template for API keys and configuration.

Browse Sandbox Components

Before installing, explore the available sandbox components on the official Claude Code Templates website:

Visit aitmpl.com and search for "sandbox" to see all available cloud development environments.

Prerequisites

Before using the E2B sandbox, you'll need to set up API keys:

1. Get E2B API Key

  • Visit e2b.dev/dashboard
  • Sign up for a free E2B account
  • Generate your API key from the dashboard
  • Free tier includes generous usage limits for development

2. Get Anthropic API Key

  • Visit console.anthropic.com
  • Sign up or log in to your Anthropic account
  • Generate an API key for Claude Code usage

3. Install Python 3.11+

The E2B sandbox requires Python 3.11 or higher. Most systems will have this automatically handled, but if needed:

# macOS with Homebrew
brew install python3

# Ubuntu/Debian
sudo apt update && sudo apt install python3.11

# Windows
# Download from https://python.org/downloads

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:

  • ✓ Download and install E2B sandbox component
  • ✓ Check and install Python dependencies
  • ✓ Interactively select an agent (if not provided)
  • ✓ Ask for your prompt (if not provided)
  • ✓ Confirm execution with Y/n prompt
  • ✓ Create secure E2B cloud sandbox
  • ✓ Execute your prompt using Claude Code
  • ✓ Show complete output and generated files in real-time
  • ✓ Download files to current directory in `sandbox-xxxxx` folder
  • ✓ Automatically cleanup the sandbox

💡 Interactive Features:

  • 🤖 Agent Selection: When --agent is not provided, browse and select from 600+ available agents
  • 📝 Prompt Input: When --prompt is not provided, enter your requirements interactively
  • ✅ Execution Confirmation: Review and confirm before cloud execution starts
  • ⏹️ Cancel Anytime: Press ESC during execution to cancel
  • 📁 Local Download: Files are downloaded to your current working directory

Advanced Usage - With Components

# Install specific components and execute in sandbox
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent frontend-developer \
  --command setup-react \
  --prompt "Create a modern todo app with TypeScript and Tailwind"

Full Stack Development

# Complete full-stack app with multiple components
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent fullstack-developer \
  --command setup-node,setup-database \
  --mcp postgresql \
  --prompt "Create a Node.js API with PostgreSQL and authentication"

Data Science and Analysis

# Data analysis with specialized components
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent data-scientist \
  --command python-analysis,visualization-setup \
  --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
    └── ...

Set Up Environment Variables

Create a .env file in the .claude/sandbox/ directory:

# Create .env file with your API keys
cd .claude/sandbox
cp .env.example .env

# Edit .env and add your keys:
# E2B_API_KEY=your_e2b_api_key_here
# ANTHROPIC_API_KEY=your_anthropic_api_key_here

How It Works

The E2B sandbox integration follows this execution flow:

🔄 Execution Process

Step Description
1. Agent Selection If --agent not provided, shows interactive list of 600+ available agents to choose from.
2. Prompt Input If --prompt not provided, asks for your project requirements interactively.
3. Component Download Downloads E2B launcher script, requirements, and environment template to .claude/sandbox/.
4. Environment Check Validates Python 3.11+ installation and API key availability.
5. Dependencies Automatically installs E2B Python SDK and required packages.
6. Execution Confirmation Shows execution summary and asks for Y/n confirmation before proceeding.
7. Sandbox Creation Creates E2B sandbox with anthropic-claude-code template pre-installed.
8. Component Installation Installs any specified agents, commands, MCPs, settings, or hooks inside the sandbox.
9. Prompt Execution Runs your prompt through Claude Code in the isolated sandbox environment.
10. Real-time Output Shows Claude Code execution logs and progress in real-time. Press ESC to cancel.
11. File Download Downloads generated files to sandbox-xxxxx folder in your current directory.
12. Automatic Cleanup Destroys the sandbox after execution, leaving no traces or residual resources.

Use Cases and Examples

🌐 Web Development

# Create modern web applications safely
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --prompt "Create a responsive portfolio website with dark mode toggle"

🛡️ Security Testing

# Security analysis in isolated environment
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent security-auditor \
  --command security-audit \
  --prompt "Analyze this codebase for security vulnerabilities"

🤖 AI and Machine Learning

# ML model development and training
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent ml-engineer \
  --command python-ml-setup \
  --prompt "Create and train a sentiment analysis model"

📊 Data Processing

# Large-scale data processing
npx claude-code-templates@latest --sandbox e2b \
  --e2b-api-key your_e2b_key \
  --anthropic-api-key your_anthropic_key \
  --agent data-engineer \
  --command data-pipeline \
  --prompt "Process and analyze large CSV datasets with pandas"

Future Sandbox Providers

The sandbox system is designed to support multiple cloud providers:

🚀 Coming Soon

  • Docker Sandbox - Local containerized execution with Docker
  • AWS CodeBuild - AWS-based cloud development environment
  • GitHub Codespaces - GitHub's cloud development environment
  • Custom Providers - User-defined sandbox configurations

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