Skip to main content
OpenCode is an open-source coding agent that supports multiple LLM providers. E2B provides a pre-built opencode template with OpenCode already installed.

CLI

Spin up a sandbox with the E2B CLI.
e2b sbx create opencode
Once inside the sandbox, start OpenCode.
opencode

Run Headless

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()

Example: work on a cloned repository

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('opencode', {
  envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
  timeoutMs: 600_000,
})

await sandbox.git.clone('https://github.com/your-org/your-repo.git', {
  path: '/home/user/repo',
  username: 'x-access-token',
  password: process.env.GITHUB_TOKEN,
  depth: 1,
})

const result = await sandbox.commands.run(
  `cd /home/user/repo && opencode run "Add error handling to all API endpoints"`,
  { onStdout: (data) => process.stdout.write(data) }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)

await sandbox.kill()

Launch the Web UI

OpenCode has a built-in web interface. Start it inside a sandbox and connect from your browser.
This sandbox is created with a 10-minute timeout and auto-pause enabled — after 10 minutes of inactivity it pauses and can be resumed later. See Sandbox Persistence and Sandbox Lifecycle for more details.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.betaCreate('opencode', {
  envs: {
    ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY,
    OPENCODE_SERVER_PASSWORD: 'your-password',
  },
  autoPause: true,
  timeoutMs: 10 * 60 * 1000,
})

// Start the web server
sandbox.commands.run('opencode web --hostname 0.0.0.0 --port 4096')

const host = sandbox.getHost(4096)
const url = `https://${host}`
console.log(`OpenCode Web UI: ${url}`)
console.log(`Username: opencode`)
console.log(`Password: your-password`)
console.log(`Sandbox ID: ${sandbox.sandboxId}`)

Build a Custom Template

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.ts
import { Template, waitForPort } from 'e2b'

export const template = Template()
  .fromTemplate('opencode')
  .setEnvs({
    OPENCODE_SERVER_PASSWORD: 'your-password',
  })
  // Optional - start the OpenCode web server on sandbox start
  .setStartCmd(
    'opencode web --hostname 0.0.0.0 --port 4096',
    waitForPort(4096)
  )
// build.ts
import { Template, defaultBuildLogger } from 'e2b'
import { template as openCodeTemplate } from './template'

await Template.build(openCodeTemplate, 'my-opencode', {
  cpuCount: 2,
  memoryMB: 2048,
  onBuildLogs: defaultBuildLogger(),
})
Run the build script to create the template.
npx tsx build.ts