Skip to main content
The opencode template supports ANTHROPIC_API_KEY, OPENAI_API_KEY, or OPENCODE_API_KEY.

Run OpenCode

Create a sandbox from the opencode template, run OpenCode headlessly, and clean up. The SDK and CLI paths are equivalent — use the one that matches your workflow.
import json
import os
from nullspace import Sandbox

model = os.environ.get("OPENCODE_OPENAI_MODEL", "openai/gpt-5.2")
config = json.dumps({
    "$schema": "https://opencode.ai/config.json",
    "model": model,
    "small_model": model,
    "provider": {
        "openai": {
            "options": {"apiKey": "{env:OPENAI_API_KEY}"}
        }
    },
})

with Sandbox.create(
    template="opencode",
    envs={
        "OPENAI_API_KEY": os.environ["OPENAI_API_KEY"],
        "OPENCODE_CONFIG_CONTENT": config,
        "OPENCODE_DISABLE_AUTOUPDATE": "true",
    },
    timeout=600,
) as sandbox:
    result = sandbox.commands.run(
        'opencode run --model openai/gpt-5.2 --dangerously-skip-permissions "Create a hello world HTTP server in Go"',
        shell=True,
        cwd="/workspace",
        timeout=600,
    )
    print(result.stdout)

Interactive terminal

Open a PTY instead of a headless run to drive OpenCode interactively:
nullspace sandbox pty create sb_123 --cols 120 --rows 30
nullspace sandbox pty connect sb_123 --session-id ses_123
# then, inside the PTY:
opencode
Use the session_id from sandbox pty create with --session-id.

Provider models

ProviderAPI key envDefault model env
AnthropicANTHROPIC_API_KEYOPENCODE_ANTHROPIC_MODEL
OpenAIOPENAI_API_KEYOPENCODE_OPENAI_MODEL
OpenCodeOPENCODE_API_KEYOPENCODE_ZEN_MODEL

Work on a cloned repository

sandbox.git.clone("https://github.com/your-org/your-repo.git", path="/workspace/repo", depth=1)
sandbox.commands.run(
    'opencode run --dangerously-skip-permissions "Add tests for the auth module"',
    shell=True,
    cwd="/workspace/repo",
    timeout=600,
    on_stdout=lambda data: print(data, end=""),
)
print(sandbox.git.diff(path="/workspace/repo"))

Serve OpenCode

OpenCode can run a headless HTTP server inside the sandbox. Start it on 0.0.0.0, then use sandbox.get_url(port) to connect through Nullspace’s preview URL routing.
server = sandbox.commands.run(
    "opencode serve --hostname 0.0.0.0 --port 4096",
    shell=True,
    background=True,
)
print(sandbox.get_url(4096))