| layout | default |
|---|---|
| title | Chapter 1: Getting Started |
| nav_order | 1 |
| parent | E2B Tutorial |
Welcome to Chapter 1: Getting Started. In this part of E2B Tutorial: Secure Cloud Sandboxes for AI Agent Code Execution, you will install the SDK, get your API key, and run code in your first cloud sandbox.
- sign up for E2B and get an API key
- install the Python or TypeScript SDK
- spin up your first sandbox and execute code
- understand the basic sandbox lifecycle
You need:
- Python 3.8+ or Node.js 18+
- An E2B account (free tier available at e2b.dev)
- Sign up at e2b.dev
- Navigate to your dashboard
- Copy your API key from the settings page
Set it as an environment variable:
export E2B_API_KEY="e2b_your_api_key_here"pip install e2b-code-interpreternpm install @e2b/code-interpreterfrom e2b_code_interpreter import Sandbox
# Create a sandbox --- spins up in <200ms
sandbox = Sandbox()
# Execute Python code inside the sandbox
execution = sandbox.run_code("print('Hello from E2B sandbox!')")
# Read the output
print(execution.text) # "Hello from E2B sandbox!"
# Clean up
sandbox.close()import { Sandbox } from '@e2b/code-interpreter';
async function main() {
// Create a sandbox
const sandbox = await Sandbox.create();
// Execute Python code inside the sandbox
const execution = await sandbox.runCode("print('Hello from E2B sandbox!')");
// Read the output
console.log(execution.text); // "Hello from E2B sandbox!"
// Clean up
await sandbox.close();
}
main();flowchart TD
A[SDK.create] --> B[Sandbox running]
B --> C{Execute code}
C --> D[Read results]
D --> C
D --> E[sandbox.close]
E --> F[Resources released]
B --> G[Timeout expires]
G --> F
Every sandbox follows this lifecycle:
- Create --- the SDK requests a new sandbox from E2B's cloud. A Firecracker microVM boots in under 200ms.
- Execute --- you run code, manage files, and interact with the sandbox as many times as needed.
- Close --- you explicitly close the sandbox, or it auto-terminates after a timeout (default 5 minutes).
The recommended pattern in Python uses a context manager to ensure cleanup:
from e2b_code_interpreter import Sandbox
with Sandbox() as sandbox:
execution = sandbox.run_code("""
import sys
print(f"Python version: {sys.version}")
print(f"Platform: {sys.platform}")
""")
print(execution.text)
# Sandbox is automatically closed hereSandboxes maintain state between executions, just like Jupyter notebook cells:
from e2b_code_interpreter import Sandbox
with Sandbox() as sandbox:
# Cell 1: define a variable
sandbox.run_code("x = 42")
# Cell 2: use the variable
execution = sandbox.run_code("print(f'The answer is {x}')")
print(execution.text) # "The answer is 42"
# Cell 3: import and use a library
execution = sandbox.run_code("""
import math
print(f"Square root of x: {math.sqrt(x)}")
""")
print(execution.text) # "Square root of x: 6.48..."from e2b_code_interpreter import Sandbox
# Sandbox will stay alive for 10 minutes
sandbox = Sandbox(timeout=600)
execution = sandbox.run_code("print('I have 10 minutes to live')")
print(execution.text)
sandbox.close()When code fails, E2B captures the error cleanly:
from e2b_code_interpreter import Sandbox
with Sandbox() as sandbox:
execution = sandbox.run_code("1 / 0")
if execution.error:
print(f"Error type: {execution.error.name}")
print(f"Error message: {execution.error.value}")
print(f"Traceback: {execution.error.traceback}")
else:
print(execution.text)E2B also provides a CLI for managing sandbox templates:
npm install -g @e2b/cli
# Authenticate
e2b auth login
# Verify
e2b auth whoamiYou now have a working E2B setup and have executed code in a cloud sandbox. Key takeaways:
- Sandboxes spin up in under 200ms
- State persists between code cells within a sandbox
- Sandboxes auto-terminate after a configurable timeout
- Error handling is clean and structured
Next: Chapter 2: Sandbox Architecture
Back to E2B Tutorial | Next: Chapter 2: Sandbox Architecture