A Go SDK for the E2B Code Interpreter - secure cloud sandboxes for running code.
- Execute code in secure cloud sandboxes
- Support for multiple programming languages (Python, JavaScript, TypeScript, R, Java, Bash)
- Streaming output with real-time callbacks
- Isolated execution contexts with persistent state
- Full filesystem operations (read, write, list, mkdir, remove, rename, watch)
- Chart data extraction from matplotlib and other plotting libraries
- Functional options pattern for idiomatic Go configuration
go get github.com/xerpa-ai/e2b-gopackage main
import (
"context"
"fmt"
"log"
"github.com/xerpa-ai/e2b-go"
)
func main() {
// Create a new sandbox (uses E2B_API_KEY environment variable)
sandbox, err := e2b.New()
if err != nil {
log.Fatal(err)
}
defer sandbox.Close()
// Execute Python code
execution, err := sandbox.RunCode(context.Background(), "x = 1 + 1; x")
if err != nil {
log.Fatal(err)
}
fmt.Println(execution.Text()) // Output: 2
}Set your E2B API key via environment variable or option:
// Via environment variable
os.Setenv("E2B_API_KEY", "your-api-key")
sandbox, err := e2b.New()
// Via option
sandbox, err := e2b.New(e2b.WithAPIKey("your-api-key"))sandbox, err := e2b.New(
e2b.WithAPIKey("your-api-key"),
e2b.WithTemplate("custom-template"),
e2b.WithTimeout(60 * time.Second),
e2b.WithRequestTimeout(30 * time.Second),
)Execute code in different programming languages:
// Python (default)
execution, _ := sandbox.RunCode(ctx, "print('Hello from Python')")
// JavaScript
execution, _ := sandbox.RunCode(ctx, "console.log('Hello from JS')",
e2b.WithLanguage(e2b.LanguageJavaScript))
// TypeScript
execution, _ := sandbox.RunCode(ctx, "const msg: string = 'Hello'; console.log(msg)",
e2b.WithLanguage(e2b.LanguageTypeScript))
// Bash
execution, _ := sandbox.RunCode(ctx, "echo 'Hello from Bash'",
e2b.WithLanguage(e2b.LanguageBash))Receive output in real-time using callbacks:
execution, err := sandbox.RunCode(ctx, code,
e2b.OnStdout(func(msg e2b.OutputMessage) {
fmt.Printf("[stdout] %s\n", msg.Line)
}),
e2b.OnStderr(func(msg e2b.OutputMessage) {
fmt.Printf("[stderr] %s\n", msg.Line)
}),
e2b.OnResult(func(result *e2b.Result) {
fmt.Printf("[result] %s\n", result.Text)
}),
e2b.OnError(func(err *e2b.ExecutionError) {
fmt.Printf("[error] %s: %s\n", err.Name, err.Value)
}),
)Create isolated execution contexts with persistent state:
// Create a new context
execCtx, err := sandbox.CreateContext(ctx,
e2b.WithContextLanguage(e2b.LanguagePython),
e2b.WithCWD("/home/user/project"),
)
if err != nil {
log.Fatal(err)
}
// Execute code in the context - state persists
sandbox.RunCode(ctx, "x = 42", e2b.WithContext(execCtx))
sandbox.RunCode(ctx, "y = x * 2", e2b.WithContext(execCtx))
execution, _ := sandbox.RunCode(ctx, "y", e2b.WithContext(execCtx))
fmt.Println(execution.Text()) // Output: 84
// List all contexts
contexts, _ := sandbox.ListContexts(ctx)
// Restart context (clears state)
sandbox.RestartContext(ctx, execCtx.ID)
// Remove context
sandbox.RemoveContext(ctx, execCtx.ID)Pass environment variables to code execution:
execution, err := sandbox.RunCode(ctx,
"import os; print(os.environ.get('MY_VAR'))",
e2b.WithRunEnvVars(map[string]string{
"MY_VAR": "hello",
}),
)Extract data from matplotlib and other plotting libraries:
code := `
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Square Numbers')
plt.show()
`
execution, _ := sandbox.RunCode(ctx, code)
for _, result := range execution.Results {
if result.Chart != nil {
fmt.Printf("Chart type: %s\n", result.Chart.ChartType())
fmt.Printf("Chart title: %s\n", result.Chart.ChartTitle())
// Access chart-specific data
if lineChart, ok := result.Chart.(*e2b.LineChart); ok {
for _, series := range lineChart.Data {
fmt.Printf("Series: %s, Points: %d\n",
series.Label, len(series.Points))
}
}
}
}Supported chart types:
LineChartScatterChartBarChartPieChartBoxAndWhiskerChartSuperChart(contains multiple sub-charts)
The SDK provides full filesystem access to the sandbox via the Files field:
// Write a file
info, err := sandbox.Files.Write(ctx, "/home/user/hello.txt", "Hello, World!")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Wrote file: %s\n", info.Path)
// Read a file
content, err := sandbox.Files.Read(ctx, "/home/user/hello.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(content) // Output: Hello, World!
// Read as bytes
data, err := sandbox.Files.ReadBytes(ctx, "/home/user/binary.bin")
// Write multiple files at once
infos, err := sandbox.Files.WriteFiles(ctx, []e2b.WriteEntry{
{Path: "/home/user/file1.txt", Data: "Content 1"},
{Path: "/home/user/file2.txt", Data: "Content 2"},
})// Create a directory (creates parent directories if needed)
created, err := sandbox.Files.MakeDir(ctx, "/home/user/mydir/subdir")
if created {
fmt.Println("Directory created")
}
// List directory contents
entries, err := sandbox.Files.List(ctx, "/home/user")
for _, entry := range entries {
fmt.Printf("%s (%s, %d bytes)\n", entry.Name, entry.Type, entry.Size)
}
// List with depth
entries, err := sandbox.Files.List(ctx, "/home/user", e2b.WithDepth(3))// Check if file exists
exists, err := sandbox.Files.Exists(ctx, "/home/user/file.txt")
if exists {
fmt.Println("File exists")
}
// Get file information
info, err := sandbox.Files.GetInfo(ctx, "/home/user/file.txt")
fmt.Printf("Name: %s\n", info.Name)
fmt.Printf("Type: %s\n", info.Type) // "file" or "dir"
fmt.Printf("Size: %d bytes\n", info.Size)
fmt.Printf("Permissions: %s\n", info.Permissions) // e.g., "rw-r--r--"
fmt.Printf("Owner: %s\n", info.Owner)
fmt.Printf("Modified: %v\n", info.ModifiedTime)// Rename/move a file
info, err := sandbox.Files.Rename(ctx, "/home/user/old.txt", "/home/user/new.txt")
// Remove a file or directory
err := sandbox.Files.Remove(ctx, "/home/user/file.txt")// Watch a directory for changes
handle, err := sandbox.Files.WatchDir(ctx, "/home/user",
func(event e2b.FilesystemEvent) {
fmt.Printf("Event: %s on %s\n", event.Type, event.Name)
},
e2b.WithRecursive(true), // Watch subdirectories too
)
if err != nil {
log.Fatal(err)
}
defer handle.Stop()
// Event types: create, write, remove, rename, chmod
// Alternative: Non-streaming watcher (poll-based)
watcherID, err := sandbox.Files.CreateWatcher(ctx, "/home/user")
defer sandbox.Files.RemoveWatcher(ctx, watcherID)
// Poll for events
events, err := sandbox.Files.GetWatcherEvents(ctx, watcherID)
for _, event := range events {
fmt.Printf("%s: %s\n", event.Type, event.Name)
}// Set user for operations (affects path resolution and ownership)
content, err := sandbox.Files.Read(ctx, "file.txt", e2b.WithReadUser("root"))
// Set request timeout
info, err := sandbox.Files.Write(ctx, "/path", data,
e2b.WithWriteRequestTimeout(30*time.Second))Handle SDK errors using standard Go error handling:
execution, err := sandbox.RunCode(ctx, code)
if err != nil {
if errors.Is(err, e2b.ErrTimeout) {
// Handle execution timeout
}
if errors.Is(err, e2b.ErrNotFound) {
// Handle resource not found
}
if errors.Is(err, e2b.ErrSandboxClosed) {
// Handle closed sandbox
}
log.Fatal(err)
}Errors in the executed code are returned in the Execution.Error field:
execution, err := sandbox.RunCode(ctx, "1 / 0")
if err != nil {
log.Fatal(err) // SDK error
}
if execution.Error != nil {
// Code execution error
fmt.Println(execution.Error.Name) // "ZeroDivisionError"
fmt.Println(execution.Error.Value) // "division by zero"
fmt.Println(execution.Error.Traceback) // Full traceback
}Access execution results in multiple formats:
execution, _ := sandbox.RunCode(ctx, code)
// Main result text
fmt.Println(execution.Text())
// Logs
fmt.Println(execution.Logs.Stdout)
fmt.Println(execution.Logs.Stderr)
// All results (including display outputs)
for _, result := range execution.Results {
// Available formats
fmt.Println(result.Formats()) // ["text", "html", "png", ...]
// Access specific formats
if result.Text != "" {
fmt.Println(result.Text)
}
if result.HTML != "" {
fmt.Println(result.HTML)
}
if result.PNG != "" {
// Base64-encoded PNG image
}
if result.Chart != nil {
// Extracted chart data
}
}| Method | Description |
|---|---|
New(opts ...Option) |
Create a new sandbox |
Connect(id string, opts ...Option) |
Connect to an existing sandbox |
RunCode(ctx, code, opts ...RunOption) |
Execute code |
CreateContext(ctx, opts ...ContextOption) |
Create execution context |
ListContexts(ctx) |
List all contexts |
RemoveContext(ctx, contextID) |
Remove a context |
RestartContext(ctx, contextID) |
Restart a context |
Close() |
Close the sandbox |
| Method | Description |
|---|---|
Read(ctx, path, opts...) |
Read file content as string |
ReadBytes(ctx, path, opts...) |
Read file content as bytes |
Write(ctx, path, data, opts...) |
Write content to a file |
WriteFiles(ctx, files, opts...) |
Write multiple files |
List(ctx, path, opts...) |
List directory contents |
MakeDir(ctx, path, opts...) |
Create a directory |
Remove(ctx, path, opts...) |
Remove a file or directory |
Rename(ctx, oldPath, newPath, opts...) |
Rename/move a file |
Exists(ctx, path, opts...) |
Check if path exists |
GetInfo(ctx, path, opts...) |
Get file/directory metadata |
WatchDir(ctx, path, callback, opts...) |
Watch directory for changes |
CreateWatcher(ctx, path, opts...) |
Create a poll-based watcher |
GetWatcherEvents(ctx, watcherID, opts...) |
Get events from watcher |
RemoveWatcher(ctx, watcherID, opts...) |
Remove a watcher |
WithAPIKey(key)- Set API keyWithTemplate(template)- Set sandbox templateWithTimeout(duration)- Set default execution timeoutWithRequestTimeout(duration)- Set HTTP request timeoutWithHTTPClient(client)- Set custom HTTP clientWithDebug(bool)- Enable debug mode
WithLanguage(lang)- Set programming languageWithContext(ctx)- Use specific execution contextWithRunEnvVars(envs)- Set environment variablesWithRunTimeout(duration)- Set execution timeoutOnStdout(handler)- Callback for stdoutOnStderr(handler)- Callback for stderrOnResult(handler)- Callback for resultsOnError(handler)- Callback for errors
WithContextLanguage(lang)- Set context languageWithCWD(path)- Set working directoryWithContextRequestTimeout(duration)- Set request timeout
WithUser(user)- Set user for operationsWithFilesystemRequestTimeout(duration)- Set request timeoutWithDepth(depth)- Set directory listing depthWithRecursive(bool)- Enable recursive directory watchingWithWatchTimeout(ms)- Set watch timeout in millisecondsOnWatchExit(handler)- Callback when watch stops
The SDK supports MCP (Model Context Protocol) for connecting AI models to tools:
// Create sandbox with MCP configuration
sandbox, err := e2b.New(
e2b.WithAPIKey("your-api-key"),
e2b.WithMcp(map[string]any{
"browserbase": map[string]any{
"apiKey": os.Getenv("BROWSERBASE_API_KEY"),
},
}),
)
// Get MCP gateway URL and token for client connections
mcpUrl := sandbox.GetMcpUrl()
token, err := sandbox.GetMcpToken(ctx)
// Connect your MCP client to mcpUrl with Authorization: Bearer {token}This Go SDK provides feature parity with the official Python and JavaScript SDKs for:
- Core sandbox management (create, connect, kill, list, pause/resume)
- Filesystem operations
- Command execution and PTY
- Code execution with streaming
- Chart data extraction
- MCP configuration and integration
- Template building (with extended programmatic builder API)
The following features from the official E2B ecosystem are not yet implemented:
- Desktop SDK - The official
@e2b/desktoppackage provides browser/desktop automation (mouse, keyboard, screenshots, video streaming). This is a separate product that may be considered for a futuree2b-desktop-gomodule if there is demand.
MIT License - see LICENSE for details.