// Operate E2B agent sandboxes using the CLI. Use when user needs to run code in isolation, test packages, execute commands safely, or work with binary files in a sandbox environment. Keywords: sandbox, e2b, isolated environment, run code, test code, safe execution.
| name | Agent Sandboxes |
| description | Operate E2B agent sandboxes using the CLI. Use when user needs to run code in isolation, test packages, execute commands safely, or work with binary files in a sandbox environment. Keywords: sandbox, e2b, isolated environment, run code, test code, safe execution. |
This skill provides access to E2B sandboxes through a streamlined CLI for safe code execution and file operations in isolated environments.
.claude/skills/agent-sandboxes/sandbox_cli/../../../../.env43200 (12 hours)Before using sandbox operations, validate the environment:
Check for E2B_API_KEY:
# Verify the API key is set
grep "E2B_API_KEY" `ENVIRONMENT_FILE_PATH`
If missing, instruct the user:
Error: E2B_API_KEY not found in .env file
Please add your E2B API key to the .env file in the project root:
echo "E2B_API_KEY=your_api_key_here" >> .env
Get your API key from: https://e2b.dev/docs
Verify CLI is available:
The sandbox CLI is located at SANDBOX_CLI_PATH
The .env file is automatically loaded from the project root.
upload/download for images, PDFs, executablesPre-built templates with different resource levels. Use --template flag with sbx init:
| Template | vCPU | RAM | Cost | Best For |
|---|---|---|---|---|
fullstack-vue-fastapi-node22 | 2 | 2GB | $0.13/hr | Simple apps (default) |
fullstack-vue-fastapi-node22-lite | 2 | 4GB | $0.15/hr | Browser tests |
fullstack-vue-fastapi-node22-standard | 4 | 4GB | $0.27/hr | Parallel builds |
fullstack-vue-fastapi-node22-heavy | 4 | 8GB | $0.33/hr | Multi-browser |
fullstack-vue-fastapi-node22-max | 8 | 8GB | $0.44/hr | Fastest |
Build new templates: uv run build_template.py --tier <tier> or --list-tiers to see options.
Use agent sandboxes when the user needs to:
The sandbox CLI has four core command groups:
sbx init - Quick sandbox initializationsbx sandbox - Lifecycle management (create, connect, kill, pause, extend-lifetime, info)sbx files - File operations (ls, read, write, upload, download, rm, mkdir, mv)sbx exec - Unified command execution (the most powerful command)sbx browser - Browser automation for UI validation (visual testing)Get CLI Help: Use sbx --help to see all available commands and options:
cd SANDBOX_CLI_PATH
uv run sbx --help # Main help
uv run sbx init --help # Help for init command
uv run sbx sandbox --help # Help for sandbox commands
uv run sbx files --help # Help for file operations
uv run sbx exec --help # Help for exec command
sbx execThe exec command is the primary interface for running commands in sandboxes:
uv run sbx exec $SANDBOX_ID "command" [options]
Options:
--cwd PATH Working directory
--env KEY=VALUE Environment variables (multiple allowed)
--root Run as root user
--shell Enable shell features (pipes, redirections, wildcards)
--timeout SECONDS Command timeout (default: 60)
--background Run command in background
Important: All commands must be run from SANDBOX_CLI_PATH directory.
sbx browserBrowser automation for visual validation of sandbox applications using Playwright's isolated Chromium.
Commands (all support --port PORT for parallel agents):
sbx browser init # First-time setup (once per machine)
sbx browser start [--headed] # Start browser (--headed shows window)
sbx browser nav <url> # Navigate to URL
sbx browser eval <code> # Run JavaScript, returns result
sbx browser screenshot [--path P] [--full] # Screenshot (--full for entire page)
sbx browser click <selector> # Click element by CSS selector
sbx browser type <selector> <text> # Type text into input field
sbx browser scroll <direction> # up | down | top | bottom
sbx browser a11y # Get accessibility tree (JSON)
sbx browser dom [--full] # Get DOM (--full for raw HTML)
sbx browser status # Check if browser is running
sbx browser close # Close browser and kill process
For parallel agents: Use --port flag with unique ports (9222-9999).
CRITICAL: Multiple agents may be running sandboxes simultaneously. Each agent MUST:
--timeout TIMEOUT_DURATION_IN_SECONDS when initializing (unless the user specifies a different timeout)sbx init output and remember it in your contextexport SANDBOX_ID=... (conflicts with other agents).sandbox_id file (gets overwritten by other agents)Example of proper ID handling:
# When you run this:
uv run sbx init
# Capture the sandbox ID from output (e.g., "sbx_abc123def456")
# Store it in YOUR context/memory as: sandbox_id = "sbx_abc123def456"
# Then use it directly in all commands:
uv run sbx exec sbx_abc123def456 "python --version"
uv run sbx files write sbx_abc123def456 /home/user/test.py "print('hello')"
Extended documentation for specific features. Read these when you need to use the feature.
| Feature | When to Read | Documentation |
|---|---|---|
| Browser Automation | When validating UIs, taking screenshots, or interacting with web pages | cookbook/browser.md |
Always start by checking for the E2B_API_KEY:
cd SANDBOX_CLI_PATH
grep "E2B_API_KEY" `ENVIRONMENT_FILE_PATH`
If not found, stop and request the user to add their API key.
Create a new sandbox and capture the sandbox ID from the output:
cd SANDBOX_CLI_PATH
uv run sbx init --timeout TIMEOUT_DURATION_IN_SECONDS
CRITICAL:
--timeout TIMEOUT_DURATION_IN_SECONDSsbx_abc123def456)Additional options (optional):
--template NAME - Use a specific template (default: base)--env KEY=VALUE - Set environment variablesExample:
uv run sbx init --timeout TIMEOUT_DURATION_IN_SECONDS
# Output: Created sandbox: sbx_abc123def456
# YOU remember: sandbox_id = "sbx_abc123def456"
Use the appropriate command based on the task, using the sandbox ID you captured:
Run commands (replace <sandbox_id> with your captured ID):
uv run sbx exec <sandbox_id> "python --version"
uv run sbx exec <sandbox_id> "pip list" --cwd /home/user/project
File operations:
uv run sbx files write <sandbox_id> /home/user/script.py "print('hello')"
uv run sbx files read <sandbox_id> /home/user/output.txt
uv run sbx files upload <sandbox_id> ./local.png /home/user/image.png
uv run sbx files edit <sandbox_id> /home/user/config.ts --old "old text" --new "new text"
uv run sbx files edit <sandbox_id> /home/user/config.ts --old "find" --new "replace" --all
Download directories (clone sandbox code locally):
# Download directory (excludes .venv, node_modules, .git, __pycache__ by default)
uv run sbx files download-dir <sandbox_id> /home/user/project ./local/project
# Include everything (no exclusions)
uv run sbx files download-dir <sandbox_id> /home/user/project ./local/project --all
IMPORTANT - Writing Files with Special Characters:
The sbx files write command has limitations with special characters like brackets [] when passing content as an argument due to shell glob expansion. Use one of these workarounds:
Option 1: Use --stdin flag (recommended):
# Pipe content through stdin to avoid shell escaping issues
echo 'const arr = [1, 2, 3]; const val = obj["key"];' | uv run sbx files write <sandbox_id> /home/user/file.js --stdin
# Or with heredoc
uv run sbx files write <sandbox_id> /home/user/file.js --stdin << 'EOF'
const arr = [1, 2, 3];
const value = obj["key"];
EOF
Option 2: Write locally, then upload:
# Write file locally first (using Write tool)
# Then upload to sandbox - bypasses shell escaping entirely
uv run sbx files upload <sandbox_id> ./local/file.js /home/user/file.js
Option 3: Use sbx exec with heredoc or Python:
# Use cat heredoc for complex content
uv run sbx exec <sandbox_id> "cat > /home/user/file.js << 'EOF'
const arr = [1, 2, 3];
const value = obj['key'];
EOF
" --shell
# Or use Python to write files
uv run sbx exec <sandbox_id> "python3 << 'EOF'
with open('/home/user/file.js', 'w') as f:
f.write('''const arr = [1, 2, 3];
const value = obj[\"key\"];''')
EOF
" --shell
Install packages:
# Install uv package manager
uv run sbx exec <sandbox_id> "curl -LsSf https://astral.sh/uv/install.sh | sh" --shell --timeout 120
# Install Python packages
uv run sbx exec <sandbox_id> "/home/user/.local/bin/uv pip install --system requests"
When building applications with a frontend/UI, create a lightweight server to host your work and expose it:
Always default to port 5173 and ensure your frontend is configured to use this port:
# For Python/Flask
uv run sbx exec <sandbox_id> "python -m http.server 5173" --background --cwd /home/user/project
# For Node/React (Vite)
uv run sbx exec <sandbox_id> "npm run dev -- --port 5173" --background --cwd /home/user/project
# For static HTML/CSS/JS
uv run sbx exec <sandbox_id> "python -m http.server 5173" --background --cwd /home/user/dist
# For a custom Python server
uv run sbx files write <sandbox_id> /home/user/server.py "from flask import Flask; app = Flask(__name__); app.run(host='0.0.0.0', port=5173)"
uv run sbx exec <sandbox_id> "python /home/user/server.py" --background
Key points:
--background flag to keep server runningCRITICAL: Always use the get-host command to retrieve the actual public URL. Do NOT try to construct or infer the URL.
uv run sbx sandbox get-host <sandbox_id> --port 5173
This command returns the authoritative public URL (format: https://5173-<sandbox_id>.e2b.app).
Example:
uv run sbx sandbox get-host sbx_abc123def456 --port 5173
# Output: https://5173-sbx_abc123def456.e2b.app
# YOU capture and remember this URL in your context
Important:
--port 5173 to match your server portGet the URL using get-host and test it:
# Get the URL (captures output: https://5173-<sandbox_id>.e2b.app)
uv run sbx sandbox get-host <sandbox_id> --port 5173
# YOU remember the URL in your context, then test it
curl https://5173-<sandbox_id>.e2b.app
Note: Capture the URL from get-host output and remember it in your context. Use the exact URL in subsequent commands.
Important:
0.0.0.0 (not localhost or 127.0.0.1)Pause a sandbox (beta feature):
uv run sbx sandbox pause <sandbox_id>
Resume a paused sandbox (use connect):
uv run sbx sandbox connect <sandbox_id>
Extend sandbox lifetime (adds time to remaining):
uv run sbx sandbox extend-lifetime <sandbox_id> <seconds_to_add>
Examples:
# Add 1 hour to remaining time
uv run sbx sandbox extend-lifetime <sandbox_id> 3600
# Add 3 hours
uv run sbx sandbox extend-lifetime <sandbox_id> 10800
# Add 12 hours
uv run sbx sandbox extend-lifetime <sandbox_id> 43200
The extend-lifetime command adds the specified seconds to the remaining lifetime. If a sandbox has 30m left and you add 1h, it will have 1h 30m remaining.
Report the results to the user with the sandbox information:
If you built a frontend or web application, use get-host to retrieve the public URL:
uv run sbx sandbox get-host <sandbox_id> --port 5173
This returns the actual URL (e.g., https://5173-<sandbox_id>.e2b.app).
Do NOT construct the URL manually - always use the get-host command.
Provide the user with:
Example report:
โ Sandbox created successfully!
Sandbox ID: sbx_abc123def456
Application URL: [Use: uv run sbx sandbox get-host sbx_abc123def456 --port 5173]
Your sandbox will automatically terminate after TIMEOUT_DURATION_IN_SECONDS.
Note: Always get the actual URL using sbx sandbox get-host <sandbox_id> --port 5173 - never construct it manually.
IMPORTANT:
Progressive Disclosure: Read only the example you need for your specific task.
Read when: User needs to run/test Python code in isolation. See: examples/01_run_python_code.md
Covers: Basic sandbox workflow, writing scripts, executing Python code, capturing sandbox ID.
Read when: User needs to install and test Python packages. See: examples/02_test_package.md
Covers: Installing uv package manager, installing Python packages, testing package functionality.
Read when: User needs to clone a GitHub repo and run tests or commands in it. See: examples/03_clone_and_test_repo.md
Covers: Git operations, using --cwd flag, running commands in repository context, longer timeouts.
Read when: User needs to upload, process, or download binary files (images, PDFs). See: examples/04_process_binary_files.md
Covers: Binary file upload/download, image processing, using appropriate file operations for binary vs text.
Read when: User wants to build a web app, UI, dashboard, or any frontend accessible via browser. See: examples/05_host_frontend.md
Covers: Exposing frontends, using port 5173, starting servers in background, getting public URLs, keeping sandboxes alive.
Built-in CLI Help:
cd SANDBOX_CLI_PATH
uv run sbx --help # Overview of all commands
uv run sbx <command> --help # Detailed help for specific command
For complete command reference and advanced usage, see:
uv run sbx --help for interactive command referenceSANDBOX_CLI_PATH/README.md"Need help with a command":
uv run sbx --help for all available commandsuv run sbx <command> --help for specific command details"E2B_API_KEY not found":
.env file exists in project rootgrep E2B_API_KEY .envecho "E2B_API_KEY=key" >> .env"Command not found: sbx":
uv run sbx instead of just sbx"Sandbox timeout":
sbx init --timeout 900--timeout flag on long-running exec commands"Permission denied":
--root flag for system operations/home/user/ directory"Forgot command syntax":
uv run sbx --help to see command structureBrowser issues: See cookbook/browser.md for troubleshooting.