# cubesandbox Go SDK Go SDK for [CubeSandbox](https://github.com/TencentCloud/CubeSandbox). It matches the current Python SDK surface: sandbox lifecycle, code execution, commands, PTY (interactive terminal), filesystem operations (read, write, list, stat, exists, remove, rename, mkdir, watch), snapshots, clone, rollback, and L7 egress policy. ## Install ```bash go get github.com/tencentcloud/CubeSandbox/sdk/go ``` ## Configuration ```bash export CUBE_API_URL=http://127.0.0.1:3000 export CUBE_TEMPLATE_ID= # Optional remote data-plane access. export CUBE_PROXY_NODE_IP= export CUBE_PROXY_PORT_HTTP=80 export CUBE_PROXY_SCHEME=http export CUBE_SANDBOX_DOMAIN=cube.app # Template tried first when CreateOptions.MCP is set and TemplateID is empty; # CUBE_TEMPLATE_ID is used if it does not exist. export CUBE_MCP_TEMPLATE_ID=mcp-gateway ``` `NewConfigFromEnv` also accepts `E2B_API_URL` and `E2B_API_KEY`; `CUBE_API_URL` and `CUBE_API_KEY` take precedence. `CUBE_PROXY_SCHEME` supports `http` and `https`; when omitted, port `443` defaults to `https` and other ports default to `http`. ## Create And Run Code ```go package main import ( "context" "fmt" cubesandbox "github.com/tencentcloud/CubeSandbox/sdk/go" ) func main() { ctx := context.Background() client := cubesandbox.NewClient(cubesandbox.NewConfigFromEnv()) defer client.Close() sb, err := client.Create(ctx, cubesandbox.CreateOptions{}) if err != nil { panic(err) } defer sb.Kill(ctx) exec, err := sb.RunCode(ctx, "x = 41\nx + 1", cubesandbox.RunCodeOptions{}) if err != nil { panic(err) } fmt.Println(exec.Text) } ``` `Client.Close` releases local idle HTTP connections held by the SDK client. It does not pause or destroy remote sandboxes. Use `Sandbox.Kill` to destroy a sandbox, or `Sandbox.Pause` when you want to keep the sandbox for a later `Client.Connect`. When `WithHTTPClient` is used with a shared `*http.Client`, `Client.Close` also closes that shared client's idle connections. ## Commands ```go result, err := sb.Commands().Run(ctx, "echo hello", cubesandbox.CommandOptions{}) if err != nil { panic(err) } fmt.Println(result.Stdout, result.Stderr, result.ExitCode) ``` `Commands.Run` starts `/bin/bash -l -c ` through envd's `process.Process/Start` API and returns stdout, stderr, and the `EndEvent` exit code. Callers are still responsible for treating untrusted shell input carefully. ## PTY (interactive terminal) `sb.Pty()` opens a real pseudo-terminal for interactive programs that need a TTY — shells/REPLs, full-screen tools (`vim`, `top`), or agent-driven terminals. It mirrors the Python/Node `sandbox.pty` surface. ```go pty := sb.Pty() // Start an interactive login shell (80x24). Optionally set opts.User / opts.Cwd. handle, err := pty.Create(ctx, cubesandbox.PtySize{Rows: 24, Cols: 80}, cubesandbox.PtyCreateOptions{}) if err != nil { panic(err) } // Drive it: send input, resize the window. _ = handle.SendStdin(ctx, []byte("echo hi && stty size\n")) _ = handle.Resize(ctx, cubesandbox.PtySize{Rows: 40, Cols: 120}) // Consume output with Wait's callback OR by ranging handle.Output() — not both, // they share one stream. Wait blocks until the shell exits (or you call // handle.Kill / handle.Disconnect) and returns the exit code. code, err := handle.Wait(func(chunk []byte) { os.Stdout.Write(chunk) }) fmt.Println("pty exited with", code, err) ``` Reattach to a still-running PTY from elsewhere with `pty.Connect(ctx, pid, ...)`, or control one by PID without a handle: ```go handle, _ := pty.Create(ctx, cubesandbox.PtySize{Rows: 24, Cols: 80}, cubesandbox.PtyCreateOptions{}) handle.Disconnect() // detach without killing; the shell keeps running again, _ := pty.Connect(ctx, handle.PID(), cubesandbox.PtyConnectOptions{}) _ = pty.SendStdin(ctx, again.PID(), []byte("ls\n")) killed, _ := pty.Kill(ctx, again.PID()) // false if the PID already exited _ = killed ``` | Method | Description | |---|---| | `Pty.Create(ctx, size, opts)` | Start `/bin/bash -i -l` with a PTY; seeds `TERM`/`LANG`/`LC_ALL` (overridable via `opts.Envs`). Streaming `process.Process/Start`. | | `Pty.Connect(ctx, pid, opts)` | Reattach to a running PTY. Streaming `process.Process/Connect`. | | `Pty.Kill(ctx, pid)` | `SIGKILL` a PTY; returns `false` (not an error) if the PID was not found. | | `Pty.SendStdin(ctx, pid, data)` | Write bytes to the PTY master (`SendInput`). | | `Pty.Resize(ctx, pid, size)` | Resize the window (`Update`). | | `handle.Output()` | Channel of raw output chunks; closed when the stream ends. | | `handle.Wait(onData)` | Block until exit, return the exit code; surfaces envd errors (e.g. `signal: killed`). | | `handle.Disconnect()` | Stop receiving output without killing the PTY. | | `handle.PID()` / `ExitCode()` / `ErrorMessage()` | PTY process ID; exit code (returns `0, false` until known); and envd end error. | | `handle.Kill` / `SendStdin` / `Resize` | Per-handle shortcuts that target this PTY's PID. | Consume output via **either** `Output()` **or** `Wait(onData)`, not both — they share one stream. `PtyCreateOptions.Timeout` / `PtyConnectOptions.Timeout` (default 60s, `<= 0` uses the default) is both sent to envd as `Connect-Timeout-Ms` and enforced client-side as an idle abort that resets on every received frame; on expiry `Wait` returns an "idle" timeout error. ## Files ```go // Read & write content, err := sb.Files().Read(ctx, "/etc/hosts") png, err := sb.Files().ReadBytes(ctx, "/tmp/chart.png") err = sb.Files().Write(ctx, "/tmp/hello.txt", []byte("hi")) // Execute all filesystem operations as a specific sandbox user. rootFiles := sb.Files().ForUser("root") content, err = rootFiles.Read(ctx, "/root/hello.txt") err = rootFiles.Write(ctx, "/root/hello.txt", []byte("hi")) // Batch write n, err := sb.Files().WriteFiles(ctx, []cubesandbox.WriteEntry{ {Path: "/tmp/a.txt", Data: []byte("aaa")}, {Path: "/tmp/b.txt", Data: []byte("bbb")}, }) // Directory operations entries, err := sb.Files().List(ctx, "/tmp") entry, err := sb.Files().Stat(ctx, "/tmp/hello.txt") exists, err := sb.Files().Exists(ctx, "/tmp/hello.txt") entry, err = sb.Files().MakeDir(ctx, "/tmp/mydir") entry, err = sb.Files().Rename(ctx, "/tmp/old.txt", "/tmp/new.txt") err = sb.Files().Remove(ctx, "/tmp/hello.txt") // Watch for changes watcher, err := sb.Files().WatchDir(ctx, "/tmp") if err != nil { panic(err) } defer watcher.Close() for ev := range watcher.Events { fmt.Println(ev.Name, ev.Type) // e.g. "hello.txt" "EVENT_TYPE_CREATE" } ``` | Method | Description | |---|---| | `ForUser(user)` | Return an immutable view that runs all filesystem operations as `user` | | `Read(ctx, path)` | Download file content as text via `GET /files` (e2b `format=text`) | | `ReadBytes(ctx, path)` | Download raw bytes (e2b `format=bytes`) | | `ReadStream(ctx, path)` | Stream download; caller must `Close` (e2b `format=stream`) | | `Write(ctx, path, data)` | Upload via `POST /files` (octet-stream, multipart fallback) | | `WriteFiles(ctx, entries)` | Batch write, stops on first error, returns count | | `List(ctx, path)` | List directory entries via `ListDir` RPC | | `Stat(ctx, path)` | File/directory metadata via `Stat` RPC | | `Exists(ctx, path)` | `true` if path exists (Stat + 404 check) | | `MakeDir(ctx, path)` | Create directory via `MakeDir` RPC | | `Rename(ctx, old, new)` | Move/rename via `Move` RPC | | `Remove(ctx, path)` | Delete file or directory via `Remove` RPC | | `WatchDir(ctx, path)` | Stream filesystem events (Connect streaming) | ## Snapshots, Clone, Rollback ```go snap, err := sb.CreateSnapshot(ctx, "") // POST /sandboxes/:id/snapshots snaps, nextToken, err := client.ListSnapshots(ctx, cubesandbox.ListSnapshotsOptions{ SandboxID: sb.SandboxID, Limit: 100, }) deleted, err := client.DeleteSnapshot(ctx, snap.SnapshotID) // DELETE /templates/:id?snapshot_only=true // `deleted` is false when the snapshot no longer exists; a name may be passed // instead of an ID. Only snapshots are matched, never templates; several // same-named snapshots return an error (HTTP 409), so use the `snap-` ID. _, err = sb.Rollback(ctx, snap.SnapshotID) // POST /sandboxes/:id/rollback clones, err := sb.Clone(ctx, cubesandbox.CloneOptions{N: 3, Concurrency: 3}) // One server-side call: the backend snapshots sb once and derives N copies. forks, err := sb.Fork(ctx, cubesandbox.ForkOptions{Count: cubesandbox.IntPtr(3)}) ``` `DeleteSnapshot` accepts a snapshot ID or name and returns `(deleted bool, err error)`; `deleted` is `false` when the snapshot does not exist. `Clone` snapshots the sandbox, creates `N` sandboxes from it (capped by `Concurrency`), then deletes the ephemeral snapshot. If any create fails, all successful siblings are killed and the first error is returned. `Rollback` restarts the sandbox process and drops pooled data-plane connections so the next call reconnects. `Fork` returns exactly `Count` entries — each a `ForkResult` with the derived `Sandbox` or that fork's error — so partial success keeps copies without failing the call (unlike `Clone`). `Count` is 1..100, default 1 (`IntPtr` for a value, nil = default); `Timeout` is a `time.Duration` idle TTL per fork. The temporary snapshot is server-managed. ## Volumes Persistent volumes survive sandbox lifecycles and are mounted at creation via `VolumeMounts`. ```go // Driver is optional: when empty the backend uses its first configured // volume plugin (e2b compatible). Name is optional too — when empty the // server generates a UUID used as both name and volume ID. volume, err := client.CreateVolume(ctx, cubesandbox.CreateVolumeOptions{ Name: "my-data", Driver: "cos", }) // POST /volumes volumes, err := client.ListVolumes(ctx) // GET /volumes (no tokens) volume, err = client.GetVolume(ctx, "my-data") // GET /volumes/:id (includes token) sb, err := client.Create(ctx, cubesandbox.CreateOptions{ TemplateID: "base", VolumeMounts: []cubesandbox.VolumeMount{ {Name: "my-data", Path: "/workspace"}, {Name: "shared-cache", Path: "/cache", ReadOnly: true}, }, }) err = client.DeleteVolume(ctx, "my-data") // DELETE /volumes/:id ``` For opaque plugin input, use the separate creation method (requires support in CubeAPI, every serving CubeMaster, and the plugin; existing `CreateVolume` calls are unchanged): ```go volume, err := client.CreateVolumeWithPrivateData(ctx, cubesandbox.CreateVolumeWithPrivateDataOptions{ Name: "my-data", Driver: "my-storage", PrivateData: `{"bucket":"my-bucket","prefix":"work/","cred_ref":"account-7"}`, }) // POST /v2/volumes ``` The `private_data` create API requires CubeAPI, every serving CubeMaster, and Cubelet at **0.8.0 or newer**, a plugin that supports this protocol, and the Go SDK at **0.8.0 or newer**. `PrivateData` must be valid UTF-8, limited to 1024 bytes, contain no NUL, and contain no characters outside the BMP. Invalid input is rejected before any HTTP request. Valid input is passed unchanged. Use credential references, never plaintext credentials. Empty strings are forwarded; API versions without `/v2/volumes` return an error without a legacy fallback. The returned `VolumeInfo` works with the existing query, delete and mount APIs. Upgrade order: check or upgrade the Controller/Node plugins for Create input A and Attach/Destroy state B, upgrade **all CubeMasters serving volume operations**, then deploy the new CubeAPI and begin using this method. A new API connected to an old Master can return 201 while silently dropping A; the old Master also omits B on Destroy. The SDK does not detect this mixed-version deployment. Keep compatible Masters serving existing A/B-dependent volumes during any rollback. See the [plugin upgrade guide](../../docs/guide/volume-plugin.md#private_data-upgrade-order). Deleting a volume does not auto-detach it: destroy the sandboxes using it first, otherwise the server refuses with an error matching `ErrVolumeInUse`. A delete of a missing volume matches `ErrVolumeNotFound`, so idempotent cleanup can ignore that case: ```go if err := client.DeleteVolume(ctx, "my-data"); err != nil { switch { case errors.Is(err, cubesandbox.ErrVolumeInUse): // still mounted — destroy the sandboxes using it first case errors.Is(err, cubesandbox.ErrVolumeNotFound): // already gone default: return err } } ``` ## L7 Egress Policy ```go sb, err := client.Create(ctx, cubesandbox.CreateOptions{ Network: cubesandbox.NetworkOptions{ Rules: []cubesandbox.Rule{{ Name: "github-api", Match: cubesandbox.Match{Host: "api.github.com", Scheme: "https"}, Action: cubesandbox.Action{ Allow: true, Audit: "metadata", Inject: []cubesandbox.Inject{{Header: "Authorization", Secret: "token", Format: "Bearer ${SECRET}"}}, }, }}, }, }) ``` ## Is Running `Sandbox.IsRunning` checks the control-plane `state` first: `paused`/`pausing` return `false` without touching the data plane (so a probe never auto-resumes the sandbox). For `running`/`unknown` it probes envd `GET /health` on port 49983, returning `false` on 404/502/503/410 and on transport errors, raising only on server errors such as 403/500. ```go ok, err := sb.IsRunning(ctx) if err != nil { panic(err) } ``` ## Pause And Connect ```go wait := true if err := sb.Pause(ctx, cubesandbox.PauseOptions{Wait: &wait}); err != nil { panic(err) } resumed, err := client.Connect(ctx, sb.SandboxID) if err != nil { panic(err) } _ = resumed ``` `Sandbox.Resume` is available for compatibility but deprecated; prefer `Client.Connect`. ## MCP Gateway (Preview) Set `MCP` to start an E2B-compatible MCP gateway inside the sandbox. It needs a template that ships `mcp-gateway`; when `TemplateID` is empty, the alias `mcp-gateway` is tried first (override with `CUBE_MCP_TEMPLATE_ID` or `Config.MCPTemplateID`), then `CUBE_TEMPLATE_ID` if that template does not exist. An error matching `ErrTemplateNotFound` is returned when neither exists. A failed start returns an error wrapping `ErrMCPGateway` and kills the sandbox. See [MCP Gateway](../../docs/guide/mcp-gateway.md). ```go sb, err := client.Create(ctx, cubesandbox.CreateOptions{ MCP: cubesandbox.MCPServers{"duckduckgo": map[string]any{}}, }) if err != nil { return err } url := sb.GetMCPURL() // streamable HTTP endpoint token, err := sb.GetMCPToken(ctx) // send as "Authorization: Bearer " ``` ## Network Policy ```go denyInternet := false sb, err := client.Create(ctx, cubesandbox.CreateOptions{ AllowInternetAccess: &denyInternet, Network: cubesandbox.NetworkOptions{ AllowOut: []string{"151.101.0.0/16"}, DenyOut: []string{"0.0.0.0/0"}, }, }) ``` Customize the Host forwarded to user services with a per-sandbox template: ```go maskRequestHost := "localhost:${PORT}" sb, err := client.Create(ctx, cubesandbox.CreateOptions{ Network: cubesandbox.NetworkOptions{ MaskRequestHost: &maskRequestHost, }, }) ``` ## Host Directory Mount ```go sb, err := client.Create(ctx, cubesandbox.CreateOptions{ Metadata: map[string]string{ "hostdir-mount": `[{"hostPath":"/data/shared","mountPath":"/mnt/data"}]`, }, }) ``` ## Remote Proxy When `CUBE_PROXY_NODE_IP` is set, data-plane requests connect directly to that IP and port while preserving the virtual sandbox host: ```text URL: ://49983-./ TCP: : Host: 49983-. ``` The host prefix is the sandbox's internal service port: `49983` (envd) for commands/filesystem/files/PTY, and `49999` (Jupyter) for the code interpreter (`RunCode` / `/execute`). You can also set it directly: ```go cfg := cubesandbox.Config{ APIURL: "http://10.0.0.1:3000", TemplateID: "tpl-xxxxxxxx", ProxyNodeIP: "10.0.0.1", ProxyPortHTTP: 80, ProxyScheme: "http", SandboxDomain: "cube.app", } client := cubesandbox.NewClient(cfg) ``` ## Integration Tests Unit tests do not require a live service: ```bash go test ./... ``` Live integration tests are behind the `integration` build tag. They require `CUBE_API_URL`, auto-discover a READY template from `/templates` when `CUBE_TEMPLATE_ID` is unset, and use `CUBE_PROXY_NODE_IP` for remote data-plane proxying when needed. ```bash export CUBE_API_URL=http://:3000 export CUBE_TEMPLATE_ID= export CUBE_PROXY_NODE_IP= export CUBE_PROXY_PORT_HTTP=80 export CUBE_PROXY_SCHEME=http export CUBE_SANDBOX_DOMAIN=cube.app go test -tags=integration -run Integration -count=1 ./... ```