Maintenance May 12, 8–9 PM PDT (May 13, 03:00–04:00 UTC). ~1 min disruption to sandbox management may occur. Already running sandboxes will not be affected. Questions? Contact us
Maintenance May 12, 8–9 PM PDT (May 13, 03:00–04:00 UTC). ~1 min disruption to sandbox management may occur. Already running sandboxes will not be affected. Questions? Contact us
Use this file to discover all available pages before exploring further.
OpenCode is an open-source coding agent that supports multiple LLM providers. E2B provides a pre-built opencode template with OpenCode already installed.
Use opencode run for non-interactive mode. Pass your LLM provider’s API key as an environment variable — OpenCode supports ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, and others.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create('opencode', { envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },})const result = await sandbox.commands.run( `opencode run "Create a hello world HTTP server in Go"`)console.log(result.stdout)await sandbox.kill()
OpenCode includes a headless HTTP server that you can control programmatically using the @opencode-ai/sdk client. Start the server inside a sandbox, get the public URL with sandbox.getHost(), and connect from your application.
import { Sandbox } from 'e2b'import { createOpencodeClient } from '@opencode-ai/sdk'const sandbox = await Sandbox.create('opencode', { envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY }, lifecycle: { onTimeout: 'pause', // "pause" | "kill" }, timeoutMs: 10 * 60 * 1000,})// Start the OpenCode serversandbox.commands.run('opencode serve --hostname 0.0.0.0 --port 4096', { background: true,})// Wait for the server to be readyconst host = sandbox.getHost(4096)const baseUrl = `https://${host}`while (true) { try { await fetch(`${baseUrl}/global/health`) break } catch { await new Promise((r) => setTimeout(r, 500)) }}// Connect to the serverconst client = createOpencodeClient({ baseUrl,})// Create a session and send a promptconst { data: session } = await client.session.create({ body: { title: 'E2B Session' },})const { data: result } = await client.session.prompt({ path: { id: session.id }, body: { parts: [{ type: 'text', text: 'Create a hello world HTTP server in Go' }], },})console.log(result)
If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own template on top of the pre-built opencode template.
// template.tsimport { Template, waitForPort } from 'e2b'export const template = Template() .fromTemplate('opencode') .setEnvs({ OPENCODE_SERVER_PASSWORD: 'your-password', }) // Optional - start the OpenCode server on sandbox start .setStartCmd( 'opencode serve --hostname 0.0.0.0 --port 4096', waitForPort(4096) )
// build.tsimport { Template, defaultBuildLogger } from 'e2b'import { template as openCodeTemplate } from './template'await Template.build(openCodeTemplate, 'my-opencode', { cpuCount: 2, memoryMB: 2048, onBuildLogs: defaultBuildLogger(),})