|
| 1 | +# E2B Code Interpreter — Go SDK |
| 2 | + |
| 3 | +Go SDK for the [E2B](https://e2b.dev) Code Interpreter. It lets you run |
| 4 | +AI-generated code inside secure, isolated E2B sandboxes and get back rich, |
| 5 | +structured results (text, HTML, images, chart data, …). |
| 6 | + |
| 7 | +This package mirrors the features of the official |
| 8 | +[Python](../python) and [JavaScript](../js) SDKs. |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +```bash |
| 13 | +go get github.com/e2b-dev/codeinterpreter-go |
| 14 | +``` |
| 15 | + |
| 16 | +> **Go version**: 1.21+ |
| 17 | +
|
| 18 | +## Get your API key |
| 19 | + |
| 20 | +1. Sign up at [e2b.dev](https://e2b.dev). |
| 21 | +2. Grab an API key from the [dashboard](https://e2b.dev/dashboard?tab=keys). |
| 22 | +3. Export it: |
| 23 | + |
| 24 | +```bash |
| 25 | +export E2B_API_KEY=e2b_*** |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick start |
| 29 | + |
| 30 | +```go |
| 31 | +package main |
| 32 | + |
| 33 | +import ( |
| 34 | + "context" |
| 35 | + "fmt" |
| 36 | + "log" |
| 37 | + "time" |
| 38 | + |
| 39 | + codeinterpreter "github.com/e2b-dev/codeinterpreter-go" |
| 40 | +) |
| 41 | + |
| 42 | +func main() { |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + sbx, err := codeinterpreter.Create(ctx, &codeinterpreter.SandboxOpts{ |
| 46 | + Timeout: 60 * time.Second, |
| 47 | + }) |
| 48 | + if err != nil { log.Fatal(err) } |
| 49 | + defer sbx.Kill(ctx) |
| 50 | + |
| 51 | + _, _ = sbx.RunCode(ctx, "x = 1", nil) |
| 52 | + |
| 53 | + exec, err := sbx.RunCode(ctx, "x += 1; x", nil) |
| 54 | + if err != nil { log.Fatal(err) } |
| 55 | + |
| 56 | + fmt.Println(exec.Text()) // "2" |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Features |
| 61 | + |
| 62 | +The Go SDK exposes the same surface as the Python / JS SDKs: |
| 63 | + |
| 64 | +### Sandbox lifecycle |
| 65 | + |
| 66 | +| Function | Description | |
| 67 | +|---|---| |
| 68 | +| `Create(ctx, opts)` | Start a new sandbox. | |
| 69 | +| `Connect(ctx, id, opts)` | Attach to a running sandbox. | |
| 70 | +| `List(ctx, opts)` | List sandboxes for the API key. | |
| 71 | +| `Sandbox.Kill(ctx)` | Terminate the sandbox. | |
| 72 | +| `Sandbox.SetTimeout(ctx, d)` | Extend the sandbox lifetime. | |
| 73 | +| `Sandbox.IsRunning(ctx)` | Health check. | |
| 74 | +| `Sandbox.GetInfo(ctx)` | Metadata, start time, etc. | |
| 75 | +| `Sandbox.GetHost(port)` | Hostname for a port exposed by the sandbox. | |
| 76 | + |
| 77 | +### Code execution |
| 78 | + |
| 79 | +| Function | Description | |
| 80 | +|---|---| |
| 81 | +| `Sandbox.RunCode(ctx, code, opts)` | Execute code (any supported language). | |
| 82 | + |
| 83 | +`RunCodeOpts` lets you pass: |
| 84 | + |
| 85 | +* `Language` — `"python"` / `"javascript"` / `"typescript"` / `"r"` / `"java"` / `"bash"` or any custom kernel id. |
| 86 | +* `Context` — a pre-created `*Context` (mutually exclusive with `Language`). |
| 87 | +* `Envs` — extra environment variables. |
| 88 | +* `Timeout` / `RequestTimeout` — execution / request timeouts. |
| 89 | +* `OnStdout`, `OnStderr`, `OnResult`, `OnError` — streaming callbacks. |
| 90 | + |
| 91 | +### Code contexts (Jupyter kernels) |
| 92 | + |
| 93 | +| Function | Description | |
| 94 | +|---|---| |
| 95 | +| `Sandbox.CreateCodeContext(ctx, opts)` | Create a fresh kernel. | |
| 96 | +| `Sandbox.ListCodeContexts(ctx)` | List known kernels. | |
| 97 | +| `Sandbox.RestartCodeContext(ctx, c)` | Restart a kernel (clears state). | |
| 98 | +| `Sandbox.RemoveCodeContext(ctx, c)` | Terminate a kernel. | |
| 99 | + |
| 100 | +### Result / Execution model |
| 101 | + |
| 102 | +Every `RunCode` call returns an `*Execution`: |
| 103 | + |
| 104 | +```go |
| 105 | +type Execution struct { |
| 106 | + Results []*Result |
| 107 | + Logs Logs // stdout / stderr lines |
| 108 | + Error *ExecutionError // nil on success |
| 109 | + ExecutionCount int |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +Each `Result` may carry multiple representations of the same value: `Text`, |
| 114 | +`HTML`, `Markdown`, `SVG`, `PNG`, `JPEG`, `PDF`, `LaTeX`, `JSON`, `JavaScript`, |
| 115 | +`Data`, `Chart`, plus arbitrary `Extra` MIME types. |
| 116 | + |
| 117 | +### Charts |
| 118 | + |
| 119 | +`Result.Chart` is a `Chart` interface — type-assert it to inspect the |
| 120 | +structured data: |
| 121 | + |
| 122 | +```go |
| 123 | +switch c := result.Chart.(type) { |
| 124 | +case *codeinterpreter.LineChart: |
| 125 | + for _, series := range c.Points { ... } |
| 126 | +case *codeinterpreter.BarChart: |
| 127 | + for _, bar := range c.Bars { ... } |
| 128 | +case *codeinterpreter.PieChart: |
| 129 | + for _, slice := range c.Slices { ... } |
| 130 | +case *codeinterpreter.BoxAndWhiskerChart: |
| 131 | + ... |
| 132 | +case *codeinterpreter.ScatterChart: |
| 133 | + ... |
| 134 | +case *codeinterpreter.SuperChart: |
| 135 | + for _, sub := range c.Charts { ... } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Streaming output |
| 140 | + |
| 141 | +```go |
| 142 | +exec, err := sbx.RunCode(ctx, "for i in range(5): print(i)", &codeinterpreter.RunCodeOpts{ |
| 143 | + OnStdout: func(msg codeinterpreter.OutputMessage) { |
| 144 | + fmt.Println(">", msg.Line) |
| 145 | + }, |
| 146 | + OnResult: func(r *codeinterpreter.Result) { |
| 147 | + fmt.Println("got result with formats:", r.Formats()) |
| 148 | + }, |
| 149 | + OnError: func(e *codeinterpreter.ExecutionError) { |
| 150 | + fmt.Println("error:", e.Name, e.Value) |
| 151 | + }, |
| 152 | +}) |
| 153 | +``` |
| 154 | + |
| 155 | +## Error types |
| 156 | + |
| 157 | +| Error | When | |
| 158 | +|---|---| |
| 159 | +| `*AuthenticationError` | Invalid / missing API key. | |
| 160 | +| `*NotFoundError` | Resource (sandbox, context) not found. | |
| 161 | +| `*TimeoutError` | Request or execution timed out. | |
| 162 | +| `*RateLimitError` | Hit E2B rate limit. | |
| 163 | +| `*InvalidArgumentError` | Bad arguments (e.g. both `Language` + `Context`). | |
| 164 | +| `*SandboxError` | Generic error from the backend. | |
| 165 | + |
| 166 | +Use Go's type assertion / `errors.As` to discriminate between them. |
| 167 | + |
| 168 | +## Check docs |
| 169 | + |
| 170 | +Visit the [E2B documentation](https://e2b.dev/docs) for more details. |
| 171 | + |
0 commit comments