Complete E2B Code Interpreter Tutorial: Secure Code Execution for AI Applications
E2B Code Interpreter is a sandboxed code execution platform that enables AI applications to run code safely in isolated environments. In this tutorial, we'll learn how to use E2B to build AI applications that can execute Python code, generate visualizations, and process data in real-time.
What Is E2B?
E2B (Environment to Binary) is a cloud platform that provides sandboxes for code execution. These sandboxes run in isolated microVMs, ensuring that executed code cannot affect the host system. E2B is particularly useful for building AI agents that need to run code, code interpreters, data analysis tools, and other interactive AI applications.
Key advantages of E2B:
- Security: Each sandbox runs in an isolated microVM with separate filesystem and network
- Speed: Sandboxes can be spun up in milliseconds
- Flexibility: Supports Python, JavaScript, R, and other languages
- Integration: SDKs available for Python and JavaScript/TypeScript
- Persistence: Supports file upload/download and data persistence between executions
Installation
Prerequisites
Before getting started, make sure you have:
- Python 3.8+ or Node.js 18+
- An E2B account (free for basic usage)
- An API key from the E2B dashboard
Python SDK Installation
pip install e2b-code-interpreter
JavaScript/TypeScript SDK Installation
npm install @e2b/code-interpreter
Getting Your API Key
export E2BAPIKEY="e2bxxxxxxxxxxxxxxxxxxxx"
Or create a .env file:
E2BAPIKEY=e2bxxxxxxxxxxxxxxxxxxxx
Basic Usage
Running Simple Python Code
Here's a basic example of running Python code in an E2B sandbox:
from e2bcodeinterpreter import Sandbox
Create a new sandbox
sandbox = Sandbox()
Run Python code
execution = sandbox.runcode("print('Hello from E2B sandbox!')")
Get the output
print(execution.text) # Output: Hello from E2B sandbox!
Close the sandbox when done
sandbox.close()
Using Context Manager
A more idiomatic approach using context managers:
from e2bcodeinterpreter 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)
Running Multiple Cells
You can run multiple code blocks sequentially, and state is preserved between executions:
from e2bcodeinterpreter import Sandbox
with Sandbox() as sandbox:
# Cell 1: Define variables
sandbox.runcode("""
x = 10
y = 20
data = [1, 2, 3, 4, 5]
""")
# Cell 2: Use variables from the previous cell
execution = sandbox.runcode("""
result = x + y
filtered = [d for d in data if d > 2]
print(f"Sum: {result}")
print(f"Filtered: {filtered}")
""")
print(execution.text)
# Output:
# Sum: 30
# Filtered: [3, 4, 5]
Error Handling
E2B provides detailed error information:
from e2bcodeinterpreter import Sandbox
with Sandbox() as sandbox:
execution = sandbox.runcode("""
This code will produce an error
result = 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)
Working with Files
Uploading Files to Sandbox
from e2bcodeinterpreter import Sandbox
with Sandbox() as sandbox:
# Upload a file from the local filesystem