E2B SDK

2.20.0 · active · verified Thu Apr 09

E2B is an open-source infrastructure that provides isolated cloud sandboxes for AI agents to safely execute code, process data, and run tools. The Python SDK, currently at version 2.20.0, enables starting and managing these environments. Releases are frequent, often weekly or bi-weekly, addressing minor changes, patch fixes, and new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an E2B sandbox, run a shell command inside it, and print the output. It highlights the importance of setting the E2B_API_KEY environment variable for authentication.

import os
from e2b import Sandbox

# Ensure E2B_API_KEY is set in your environment
# You can get your API key from the E2B dashboard: https://e2b.dev/docs/quickstart/running-your-first-sandbox
api_key = os.environ.get('E2B_API_KEY', '')
if not api_key:
    print("Warning: E2B_API_KEY environment variable not set. Sandbox creation may fail or default to a limited scope.")

with Sandbox.create(api_key=api_key) as sandbox:
    print(f"Sandbox created with ID: {sandbox.sandbox_id}")
    result = sandbox.commands.run('echo "Hello from E2B!"')
    print(f"Stdout: {result.stdout}")
    if result.stderr:
        print(f"Stderr: {result.stderr}")

print("Sandbox session ended.")

view raw JSON →