---
title: "OpenCode integration"
description: "Run OpenCode in an E2B sandbox with any LLM provider. Use opencode run headless, or start its HTTP server and drive it from the OpenCode SDK."
url: "https://e2b.dev/integrations/opencode"
---

# 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.

Coding agents · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** E2B
- **How it works:** Prebuilt opencode sandbox template
- **Package:** e2b
- **Language:** TypeScript, Python
- **Upstream:** [anomalyco/opencode · ★ 228k](https://github.com/anomalyco/opencode)
- **Docs:** [docs.e2b.dev/agents/opencode](https://docs.e2b.dev/agents/opencode)

## Get started

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

### Paste this into your coding agent

```text
Run OpenCode in an E2B sandbox from this project.

1. Install the e2b SDK using the project's package manager.
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. 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. Run one task and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Install the E2B SDK

```bash
npm init -y && npm pkg set type=module
npm install e2b @opencode-ai/sdk
npm install -D tsx
```

### 2. Set your API keys

```bash
export E2B_API_KEY="e2b_..."
export ANTHROPIC_API_KEY="..."
```

### 3. Run OpenCode headless

```typescript
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. Or serve it over HTTP

```typescript
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)}`,
})
```

## What runs in the sandbox

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

- **Agent:** OpenCode with any provider it supports. Pass ANTHROPIC_API_KEY, OPENAI_API_KEY or GEMINI_API_KEY in envs.
- **Files:** The repo you clone and every change OpenCode makes. Read the result back with git diff.
- **Server:** opencode serve exposes its HTTP API on a sandbox port you reach through getHost().
- **Lifetime:** Set onTimeout to pause and the sandbox pauses at its timeout instead of being killed, so you can resume it.

## More coding agents

Other coding agents that run in E2B sandboxes.

- [Claude Code](https://e2b.dev/integrations/claude-code.md): Run Claude Code in its own sandbox with a real filesystem, terminal, and git, so it can work unattended.
- [OpenAI Codex](https://e2b.dev/integrations/codex.md): Run OpenAI's Codex CLI headless in a sandbox and collect the diff when it's done.
- [Pi](https://e2b.dev/integrations/pi.md): Run the Pi coding agent in a sandbox, or keep Pi local and send its tool calls to E2B with @e2b/pi.

## Links

- [Get API key](https://console.e2b.dev/?tab=keys)
- [Read the docs](https://docs.e2b.dev/agents/opencode)
- [E2B documentation](https://docs.e2b.dev)
