> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fibre.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenCode in a sandbox

> Install OpenCode in a Fibre sandbox, run a task on a repository with opencode run and the model of your choice, and continue the session later.

OpenCode is an open-source coding agent that works with many different models. Its `opencode run` command gives
OpenCode a single task without opening its interface.

If you run it in a Fibre sandbox, OpenCode gets a Linux machine of its own for the repository, well away from your
computer. Between tasks the sandbox sleeps with its memory and files, so the next task can continue the same
session.

**Before you start**, set your Fibre API key in `FIBRE_API_KEY` and `FIBRE_API_URL=https://api.fibre.dev`. For the
steps where OpenCode talks to a model, you also need that model provider's API key. The example uses Anthropic
(`ANTHROPIC_API_KEY`). For OpenAI it would be `OPENAI_API_KEY`: each provider reads its own variable.

## Start a sandbox

```bash title="CLI" run theme={null}
SBX=$(fibre create --lifetime 1h --label example=opencode --json | jq -r .id)
```

```typescript title="TypeScript" run theme={null}
import { Sandbox } from "@fibre-dev/sdk";

const sbx = await Sandbox.create({ lifetime: 3600, labels: { example: "opencode" } });
```

```python title="Python" run theme={null}
from fibre import Sandbox

sbx = Sandbox.create(lifetime=3600, labels={"example": "opencode"})
```

```bash title="curl" run capture=SBX=.id theme={null}
curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"lifetime_sec": 3600, "labels": {"example": "opencode"}}'
```

## Install OpenCode

OpenCode is installed from npm, and the sandbox's default image already has Node. The install takes under a
minute, which is longer than an exec allows, so it runs as a [background command](https://docs.fibre.dev/sandboxes/running-commands.md).

```bash title="CLI" run theme={null}
fibre exec "$SBX" --timeout 10m -- 'npm install -g opencode-ai && opencode --version'   # 1.19.6
```

```typescript title="TypeScript" run theme={null}
const install = await sbx.run("npm install -g opencode-ai && opencode --version", { timeout: 600 });
if (install.exitCode !== 0) throw new Error(install.stderr);
console.log(install.stdout.trim().split("\n").pop()); // 1.19.6
```

```python title="Python" run theme={null}
install = sbx.run("npm install -g opencode-ai && opencode --version", timeout=600)
if install.exit_code != 0:
    raise RuntimeError(install.stderr)
print(install.stdout.strip().split("\n")[-1])  # 1.19.6
```

```bash title="curl" run theme={null}
CMD=$(curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes/$SBX/commands" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"command": ["sh", "-c", "npm install -g opencode-ai && opencode --version"]}' | jq -r .command_id)
curl -sSN "$FIBRE_API_URL/v1/sandboxes/$SBX/commands/$CMD/logs?follow=1" -H "Authorization: Bearer $FIBRE_API_KEY"
```

> [accordion] Output
>
> ```text title="output" expected theme={null}
> "exit_code":0
> ```

## Give it a repository

```bash title="CLI" run theme={null}
fibre exec "$SBX" -- git clone --depth 1 https://github.com/octocat/Hello-World /work/repo
```

```typescript title="TypeScript" run theme={null}
const clone = await sbx.run(["git", "clone", "--depth", "1", "https://github.com/octocat/Hello-World", "/work/repo"]);
if (clone.exitCode !== 0) throw new Error(clone.stderr);
```

```python title="Python" run theme={null}
clone = sbx.run(["git", "clone", "--depth", "1", "https://github.com/octocat/Hello-World", "/work/repo"])
if clone.exit_code != 0:
    raise RuntimeError(clone.stderr)
```

```bash title="curl" run theme={null}
curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes/$SBX/exec" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"command": ["git", "clone", "--depth", "1", "https://github.com/octocat/Hello-World", "/work/repo"]}'
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "exit_code": 0
> }
> ```

## Run a task

`-m` chooses the model, written as `provider/model`. Once a provider's key is set, `opencode models anthropic`
lists the models that provider offers. `--auto` approves the actions OpenCode would otherwise stop and ask about,
since nobody is there to answer. That is safe here, because the Fibre sandbox keeps whatever OpenCode does inside
it. The key is given to this one command only; there is more about it [below](#the-api-key).

```bash title="CLI" norun reason="needs your Anthropic API key" theme={null}
fibre exec "$SBX" --cwd /work/repo --timeout 30m \
  --env ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" --env TASK="Rewrite the README as a short project description with a usage section." \
  -- 'opencode run --auto -m anthropic/claude-sonnet-5-5 "$TASK"'
```

```typescript title="TypeScript" norun reason="needs your Anthropic API key" theme={null}
const task = await sbx.run(
  ["sh", "-c", 'cd /work/repo && opencode run --auto -m anthropic/claude-sonnet-5-5 "$TASK"'],
  {
    env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY!, TASK: "Rewrite the README as a short project description with a usage section." },
    timeout: 1800,
  },
);
console.log(task.stdout);
```

```python title="Python" norun reason="needs your Anthropic API key" theme={null}
import os

task = sbx.run(
    ["sh", "-c", 'cd /work/repo && opencode run --auto -m anthropic/claude-sonnet-5-5 "$TASK"'],
    env={"ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], "TASK": "Rewrite the README as a short project description with a usage section."},
    timeout=1800,
)
print(task.stdout)
```

```bash title="curl" norun reason="needs your Anthropic API key" theme={null}
jq -n --arg key "$ANTHROPIC_API_KEY" '{
  command: ["sh", "-c", "cd /work/repo && opencode run --auto -m anthropic/claude-sonnet-5-5 \"$TASK\""],
  env: {ANTHROPIC_API_KEY: $key, TASK: "Rewrite the README as a short project description with a usage section."}
}' | curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes/$SBX/commands" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" -d @-
```

Add `--format json` to get one JSON event per line instead of plain text. With curl, follow the command's output in
the same way as in the install step.

## See what it changed

```bash title="CLI" run theme={null}
fibre exec "$SBX" -- git -C /work/repo diff --stat
```

```typescript title="TypeScript" run theme={null}
const diff = await sbx.run(["git", "-C", "/work/repo", "diff", "--stat"]);
console.log(diff.stdout);
```

```python title="Python" run theme={null}
diff = sbx.run(["git", "-C", "/work/repo", "diff", "--stat"])
print(diff.stdout)
```

```bash title="curl" run theme={null}
curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes/$SBX/exec" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" \
  -d '{"command": ["git", "-C", "/work/repo", "diff", "--stat"]}'
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "exit_code": 0
> }
> ```

## The next task, later

When OpenCode has finished, the sandbox goes to sleep after its idle timeout, with its memory and files saved, and
the next command wakes it. OpenCode keeps its sessions on the sandbox's disk, so `--continue` carries on the last
one.

```bash title="CLI" norun reason="needs your Anthropic API key" theme={null}
fibre exec "$SBX" --cwd /work/repo --timeout 30m --env ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" --env TASK="Now add a contributing section." \
  -- 'opencode run --continue --auto -m anthropic/claude-sonnet-5-5 "$TASK"'
```

```typescript title="TypeScript" norun reason="needs your Anthropic API key" theme={null}
const again = await Sandbox.get(sbx.id);
const next = await again.run(
  ["sh", "-c", 'cd /work/repo && opencode run --continue --auto -m anthropic/claude-sonnet-5-5 "$TASK"'],
  { env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY!, TASK: "Now add a contributing section." }, timeout: 1800 },
);
```

```python title="Python" norun reason="needs your Anthropic API key" theme={null}
again = Sandbox.get(sbx.id)
nxt = again.run(
    ["sh", "-c", 'cd /work/repo && opencode run --continue --auto -m anthropic/claude-sonnet-5-5 "$TASK"'],
    env={"ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], "TASK": "Now add a contributing section."},
    timeout=1800,
)
```

```bash title="curl" norun reason="needs your Anthropic API key" theme={null}
jq -n --arg key "$ANTHROPIC_API_KEY" '{
  command: ["sh", "-c", "cd /work/repo && opencode run --continue --auto -m anthropic/claude-sonnet-5-5 \"$TASK\""],
  env: {ANTHROPIC_API_KEY: $key, TASK: "Now add a contributing section."}
}' | curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes/$SBX/commands" \
  -H "Authorization: Bearer $FIBRE_API_KEY" -H "Content-Type: application/json" -d @-
```

To continue a particular session instead, use `--session <id>`.

## The API key

The agent needs your model provider's API key, so it is worth understanding exactly where that key goes:

* **Only the command you give it to can see it**, along with the programs that command starts. Other commands
  in the sandbox do not get it, Fibre does not write it into the sandbox's files, and Fibre's API never returns
  it.
* **Everything OpenCode runs while it works can read it**, including tests, install scripts and code in the
  repository. Treat it as
  visible to that repository's code. Use a key you can revoke, with a spending limit.
* **Do not put the key in the sandbox's own environment** (`env` in the SDK, or `env_vars` in the API, when you
  create the sandbox). Every command in the sandbox would then get it.

## Delete it

```bash title="CLI" run theme={null}
fibre delete "$SBX"
```

```typescript title="TypeScript" run theme={null}
await sbx.delete();
```

```python title="Python" run theme={null}
sbx.delete()
```

```bash title="curl" run theme={null}
curl -sS -X DELETE "$FIBRE_API_URL/v1/sandboxes/$SBX" -H "Authorization: Bearer $FIBRE_API_KEY"
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "deleted": true
> }
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.