Appearance
OpenCode
opencode is a pre-built AI Agents runtime environment that includes the OpenCode CLI. It allows you to run open-source coding agents from multiple providers (Anthropic / OpenAI / Gemini, etc.) headlessly in an isolated sandbox with full file system, terminal, and git access.
The template comes pre-installed with Node.js, git, ripgrep, vim, GitHub CLI, and other common tools, as well as frontend scaffolding tools like pnpm/tsx/vite. No API keys are embedded in the image; authentication is injected via envs when creating the sandbox or through secret injection rules.
Template Contents
| Component | Description |
|---|---|
opencode | OpenCode CLI |
| Base Tools | Node.js 24.x, git, ripgrep, vim, GitHub CLI, pnpm/tsx/vite, etc. |
Creating a Sandbox
OpenCode supports multiple providers. Inject the corresponding key based on the selected model:
| Provider | Environment Variable |
|---|---|
| Anthropic | ANTHROPIC_API_KEY |
| OpenAI | OPENAI_API_KEY |
| Google Gemini | GEMINI_API_KEY |
javascript
import { Sandbox } from "e2b";
const sandbox = await Sandbox.create("opencode", {
envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY },
});Headless Execution
Use the opencode run "<prompt>" subcommand for non-interactive mode:
javascript
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();Git Repository Integration
javascript
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();Service Mode
OpenCode provides an HTTP service interface (default port 4096) that can be controlled programmatically using the official SDK. Start opencode serve as a background process, then use the JS SDK's getHost(4096) to obtain the external access URL:
javascript
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 },
timeoutMs: 10 * 60 * 1000,
});
await sandbox.commands.run("opencode serve --hostname 0.0.0.0 --port 4096", {
background: true,
});
const baseUrl = `https://${sandbox.getHost(4096)}`;
while (true) {
try {
await fetch(`${baseUrl}/global/health`);
break;
} catch {
await new Promise((r) => setTimeout(r, 500));
}
}
const client = createOpencodeClient({ baseUrl });
const { data: session } = await client.session.create({
body: { title: "sandbox" },
});
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);Using Secret Injection
To keep real keys on the platform side, create injection rules of the corresponding type (e.g., anthropic, openai, gemini — see Secret Injection) and pass only placeholders in the sandbox:
javascript
const sandbox = await Sandbox.create("opencode", {
envs: { ANTHROPIC_API_KEY: "placeholder" },
injections: [{ id: "<rule-id>" }],
});References
- OpenCode website: https://opencode.ai