Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions .github/workflows/go_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Test Go SDK

on:
workflow_call:
secrets:
E2B_API_KEY:
required: false
inputs:
E2B_DOMAIN:
required: false
type: string
E2B_TESTS_TEMPLATE:
required: false
type: string

permissions:
contents: read

jobs:
test:
defaults:
run:
working-directory: ./go
name: Go SDK - Build and test
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Parse .tool-versions
uses: wistia/parse-tool-versions@v2.1.1
with:
filename: '.tool-versions'
uppercase: 'true'
prefix: 'tool_version_'

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '${{ env.TOOL_VERSION_GOLANG }}'
cache-dependency-path: go/go.sum

- name: Download dependencies
run: go mod download

- name: Verify dependencies
run: go mod verify

- name: Build
run: go build ./...

- name: Vet
run: go vet ./...

- name: Run tests
run: go test -race -coverprofile=coverage.out -covermode=atomic ./...
env:
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
E2B_DOMAIN: ${{ inputs.E2B_DOMAIN }}
E2B_TESTS_TEMPLATE: ${{ inputs.E2B_TESTS_TEMPLATE }}

- name: Upload coverage report
if: always()
uses: actions/upload-artifact@v4
with:
name: go-coverage
path: go/coverage.out
if-no-files-found: ignore
21 changes: 21 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ jobs:
poetry install --with dev
pip install ruff=="0.11.12"

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '${{ env.TOOL_VERSION_GOLANG }}'
cache-dependency-path: go/go.sum

- name: Check Go formatting
working-directory: go
run: |
if [[ -n "$(gofmt -l .)" ]]; then
echo "❌ Go files are not formatted properly:"
gofmt -d .
exit 1
else
echo "✅ Go files are properly formatted."
fi

- name: Go vet
working-directory: go
run: go vet ./...

- name: Run linting
run: |
pnpm run lint
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ jobs:
with:
E2B_DOMAIN: ${{ vars.E2B_DOMAIN }}
E2B_TESTS_TEMPLATE: ${{ needs.build-template.outputs.template_id }}
go-sdk:
uses: ./.github/workflows/go_tests.yml
needs: build-template
secrets:
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
with:
E2B_DOMAIN: ${{ vars.E2B_DOMAIN }}
E2B_TESTS_TEMPLATE: ${{ needs.build-template.outputs.template_id }}
performance-tests:
uses: ./.github/workflows/performance_tests.yml
needs: build-template
Expand All @@ -46,7 +54,7 @@ jobs:
E2B_TESTS_TEMPLATE: ${{ needs.build-template.outputs.template_id }}
cleanup-build-template:
uses: ./.github/workflows/cleanup_build_template.yml
needs: [build-template, js-sdk, python-sdk, performance-tests]
needs: [build-template, js-sdk, python-sdk, go-sdk, performance-tests]
if: always() && !contains(needs.build-template.result, 'failure') && !contains(needs.build-template.result, 'cancelled')
secrets:
E2B_TESTS_ACCESS_TOKEN: ${{ secrets.E2B_TESTS_ACCESS_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
python 3.10
poetry 1.8.5
golang 1.21.5
171 changes: 171 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# E2B Code Interpreter — Go SDK

Go SDK for the [E2B](https://e2b.dev) Code Interpreter. It lets you run
AI-generated code inside secure, isolated E2B sandboxes and get back rich,
structured results (text, HTML, images, chart data, …).

This package mirrors the features of the official
[Python](../python) and [JavaScript](../js) SDKs.

## Install

```bash
go get github.com/e2b-dev/codeinterpreter-go
```

> **Go version**: 1.21+

## Get your API key

1. Sign up at [e2b.dev](https://e2b.dev).
2. Grab an API key from the [dashboard](https://e2b.dev/dashboard?tab=keys).
3. Export it:

```bash
export E2B_API_KEY=e2b_***
```

## Quick start

```go
package main

import (
"context"
"fmt"
"log"
"time"

codeinterpreter "github.com/e2b-dev/codeinterpreter-go"
)

func main() {
ctx := context.Background()

sbx, err := codeinterpreter.Create(ctx, &codeinterpreter.SandboxOpts{
Timeout: 60 * time.Second,
})
if err != nil { log.Fatal(err) }
defer sbx.Kill(ctx)

_, _ = sbx.RunCode(ctx, "x = 1", nil)

exec, err := sbx.RunCode(ctx, "x += 1; x", nil)
if err != nil { log.Fatal(err) }

fmt.Println(exec.Text()) // "2"
}
```

## Features

The Go SDK exposes the same surface as the Python / JS SDKs:

### Sandbox lifecycle

| Function | Description |
|---|---|
| `Create(ctx, opts)` | Start a new sandbox. |
| `Connect(ctx, id, opts)` | Attach to a running sandbox. |
| `List(ctx, opts)` | List sandboxes for the API key. |
| `Sandbox.Kill(ctx)` | Terminate the sandbox. |
| `Sandbox.SetTimeout(ctx, d)` | Extend the sandbox lifetime. |
| `Sandbox.IsRunning(ctx)` | Health check. |
| `Sandbox.GetInfo(ctx)` | Metadata, start time, etc. |
| `Sandbox.GetHost(port)` | Hostname for a port exposed by the sandbox. |

### Code execution

| Function | Description |
|---|---|
| `Sandbox.RunCode(ctx, code, opts)` | Execute code (any supported language). |

`RunCodeOpts` lets you pass:

* `Language` — `"python"` / `"javascript"` / `"typescript"` / `"r"` / `"java"` / `"bash"` or any custom kernel id.
* `Context` — a pre-created `*Context` (mutually exclusive with `Language`).
* `Envs` — extra environment variables.
* `Timeout` / `RequestTimeout` — execution / request timeouts.
* `OnStdout`, `OnStderr`, `OnResult`, `OnError` — streaming callbacks.

### Code contexts (Jupyter kernels)

| Function | Description |
|---|---|
| `Sandbox.CreateCodeContext(ctx, opts)` | Create a fresh kernel. |
| `Sandbox.ListCodeContexts(ctx)` | List known kernels. |
| `Sandbox.RestartCodeContext(ctx, c)` | Restart a kernel (clears state). |
| `Sandbox.RemoveCodeContext(ctx, c)` | Terminate a kernel. |

### Result / Execution model

Every `RunCode` call returns an `*Execution`:

```go
type Execution struct {
Results []*Result
Logs Logs // stdout / stderr lines
Error *ExecutionError // nil on success
ExecutionCount int
}
```

Each `Result` may carry multiple representations of the same value: `Text`,
`HTML`, `Markdown`, `SVG`, `PNG`, `JPEG`, `PDF`, `LaTeX`, `JSON`, `JavaScript`,
`Data`, `Chart`, plus arbitrary `Extra` MIME types.

### Charts

`Result.Chart` is a `Chart` interface — type-assert it to inspect the
structured data:

```go
switch c := result.Chart.(type) {
case *codeinterpreter.LineChart:
for _, series := range c.Points { ... }
case *codeinterpreter.BarChart:
for _, bar := range c.Bars { ... }
case *codeinterpreter.PieChart:
for _, slice := range c.Slices { ... }
case *codeinterpreter.BoxAndWhiskerChart:
...
case *codeinterpreter.ScatterChart:
...
case *codeinterpreter.SuperChart:
for _, sub := range c.Charts { ... }
}
```

## Streaming output

```go
exec, err := sbx.RunCode(ctx, "for i in range(5): print(i)", &codeinterpreter.RunCodeOpts{
OnStdout: func(msg codeinterpreter.OutputMessage) {
fmt.Println(">", msg.Line)
},
OnResult: func(r *codeinterpreter.Result) {
fmt.Println("got result with formats:", r.Formats())
},
OnError: func(e *codeinterpreter.ExecutionError) {
fmt.Println("error:", e.Name, e.Value)
},
})
```

## Error types

| Error | When |
|---|---|
| `*AuthenticationError` | Invalid / missing API key. |
| `*NotFoundError` | Resource (sandbox, context) not found. |
| `*TimeoutError` | Request or execution timed out. |
| `*RateLimitError` | Hit E2B rate limit. |
| `*InvalidArgumentError` | Bad arguments (e.g. both `Language` + `Context`). |
| `*SandboxError` | Generic error from the backend. |

Use Go's type assertion / `errors.As` to discriminate between them.

## Check docs

Visit the [E2B documentation](https://e2b.dev/docs) for more details.

Loading