Skip to main content

Coding agents

OpenCode

Run OpenCode in a sandbox from the prebuilt opencode template, with the LLM provider of your choice. Use it headless, or serve it over HTTP and drive it from the OpenCode SDK.

Maintained by
E2B
How it works
Prebuilt opencode sandbox template
Package
e2b
Language
TypeScript, Python

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Run OpenCode in an E2B sandbox from this project.

  1. 1.Install the e2b SDK using the project's package manager.
  2. 2.The SDK needs the E2B_API_KEY environment variable, and OpenCode needs an LLM provider key such as ANTHROPIC_API_KEY. If E2B_API_KEY isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode either key.
  3. 3.Follow the OpenCode guide at https://docs.e2b.dev/agents/opencode: create a sandbox from the opencode template, pass the provider key in envs, and run opencode run "<task>" headless. Kill the sandbox when it's done.
  4. 4.Run one task and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install the E2B SDK

    npm init -y && npm pkg set type=module
    npm install e2b @opencode-ai/sdk
    npm install -D tsx
  2. 2. Set your API keys

    export E2B_API_KEY="e2b_..."
    export ANTHROPIC_API_KEY="..."
  3. 3. Run OpenCode headless

    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()
  4. 4. Or serve it over HTTP

    import { createOpencodeClient } from '@opencode-ai/sdk'
    
    sandbox.commands.run('opencode serve --hostname 0.0.0.0 --port 4096', {
      background: true,
    })
    
    const client = createOpencodeClient({
      baseUrl: `https://${sandbox.getHost(4096)}`,
    })

Security

What runs in the sandbox

OpenCode and the commands it runs live in the sandbox, away from your machine.

  • AgentOpenCode with any provider it supports. Pass ANTHROPIC_API_KEY, OPENAI_API_KEY or GEMINI_API_KEY in envs.
  • FilesThe repo you clone and every change OpenCode makes. Read the result back with git diff.
  • Serveropencode serve exposes its HTTP API on a sandbox port you reach through getHost().
  • LifetimeSet onTimeout to pause and the sandbox pauses at its timeout instead of being killed, so you can resume it.

3B+ sandboxes started

Give every agent a machine