Documentation Index

Fetch the complete documentation index at: /llms.txt

Use this file to discover all available pages before exploring further.

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

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

Connect with the OpenCode SDK

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 server
sandbox.commands.run('opencode serve --hostname 0.0.0.0 --port 4096', {
  background: true,
})

// Wait for the server to be ready
const 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 server
const client = createOpencodeClient({
  baseUrl,
})

// Create a session and send a prompt
const { 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)

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 server on sandbox start
  .setStartCmd(
    'opencode serve --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

Sandbox persistence

Auto-pause, resume, and manage sandbox lifecycle

Git integration

Clone repos, manage branches, and push changes

SSH access

Connect to the sandbox via SSH for interactive sessions
Assistant
Responses are generated using AI and may contain mistakes.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.