# Run OpenCode in a sandbox
URL: https://docs.munito.ai/sandboxes/cloud-agents/opencode

## Sign in [#sign-in]

Use one of these three methods.

### With the API key of a provider [#with-the-api-key-of-a-provider]

Put the key in the `env` of the sandbox, in the usual variable of the provider:

```json
{ "ANTHROPIC_API_KEY": "sk-ant-..." }
```

- `opencode models` lists the models that your keys can use.
- `-m provider/model` selects the model for one run.

### With a subscription [#with-a-subscription]

**GitHub Copilot** (Pro, Pro+, Business or Enterprise):

1. Connect to the sandbox over [SSH](https://docs.munito.ai/sandboxes/ports-and-ssh#ssh).
2. Run `opencode auth login`, and select GitHub Copilot.
3. Open github.com/login/device, and type the code that OpenCode shows.

**OpenCode Zen or OpenCode Go:**

1. Create a key at [opencode.ai/auth](https://opencode.ai/auth).
2. Put the key in the `env` of the sandbox as `OPENCODE_API_KEY`.

- OpenCode keeps a sign-in in `~/.local/share/opencode/auth.json`.
- Anthropic does not allow a Claude subscription in OpenCode. For Claude, use [Claude Code](https://docs.munito.ai/sandboxes/cloud-agents/claude-code).
- For a ChatGPT plan, use [Codex](https://docs.munito.ai/sandboxes/cloud-agents/codex).
- See [providers](https://opencode.ai/docs/providers) in the OpenCode docs.

### With the open models of Munito AI [#with-the-open-models-of-munito-ai]

1. Put your Munito AI key in the `env` of the sandbox as `MUNITO_API_KEY`.
2. Write this configuration to `~/.config/opencode/opencode.json`, with the files API or a command:

```json
{
  "$schema": "https://opencode.ai/config.json",
  "model": "munito/zai-org/glm-5.3-flash",
  "provider": {
    "munito": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Munito",
      "options": {
        "baseURL": "https://api.munito.ai/inference/v1",
        "apiKey": "{env:MUNITO_API_KEY}"
      },
      "models": {
        "zai-org/glm-5.3-flash": {
          "name": "GLM-5.3-Flash",
          "limit": { "context": 1048576, "output": 943718 }
        }
      }
    }
  }
}
```

- `{env:MUNITO_API_KEY}` reads the key from the environment, so the file holds no key.
- [OpenCode with open models](https://docs.munito.ai/agents/opencode) explains the provider, the reasoning effort and the regional hosts.

## Run a task [#run-a-task]

```bash
curl -sS --fail-with-body "https://api.munito.ai/sandboxes/v1/sandboxes/$SANDBOX/exec" \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"command": "cd /workspace && opencode run --auto \"Write a Python script that prints the first 10 primes, and run it\"", "timeout": 1800}'
```

- `opencode run` runs one task with the `build` agent, and then it stops. It writes the answer to standard output.
- `--auto` approves each permission that no rule denies. Then OpenCode runs commands and changes files with no question.
- Without `--auto`, `opencode run` rejects each permission that asks.
- `-m provider/model` selects the model for one run. Without it, OpenCode uses `model` from its configuration.
- For a task of more than 30 minutes, start `opencode run` in the background with `nohup`, and read its log with later commands.

## Use the terminal UI [#use-the-terminal-ui]

1. Add your public key with [SSH](https://docs.munito.ai/sandboxes/ports-and-ssh#ssh).
2. Connect with the command that `GET /sandboxes/{sandbox_id}/ssh` gives.
3. Run `opencode` in `/workspace`.

To keep the session after you disconnect, install `tmux` (`apt-get install -y tmux`), and start OpenCode in it.
