# OpenCode

[OpenCode](https://opencode.ai) is an open-source AI coding agent for the terminal. This guide is based on the current OpenCode 2.x. The configuration for the older 1.x is at the end of this page.

## 1. Get an API Key

Open the [API Keys](https://beeroute.ai/keys) page, click **Create API Key**, set **Group** to `default`, and copy the key (it starts with `sk-`).

## 2. Install OpenCode

Choose any one of the following methods:

| Method | Command |
| :--- | :--- |
| Official install script (macOS / Linux) | `curl -fsSL https://opencode.ai/v2/install \| bash` |
| npm | `npm install -g @opencode/cli` |
| Homebrew | `brew install anomalyco/tap/opencode-v2` |

?> The npm package for the older OpenCode 1.x is `opencode-ai`, and for 2.x it is `@opencode/cli`. Both provide the `opencode` command. When upgrading from 1.x, first uninstall any 1.x installed through a package manager.

## 3. Set the API Key Environment Variable

OpenCode reads the key from an environment variable, so you do not have to store the key in plain text in the configuration file.

**macOS / Linux** (add it to `~/.zshrc` or `~/.bashrc`, then reopen the terminal):

```bash
export BEEROUTE_API_KEY="sk-xxx"
```

**Windows PowerShell** (saves it as a user environment variable; takes effect after you reopen the terminal):

```powershell
[Environment]::SetEnvironmentVariable("BEEROUTE_API_KEY", "sk-xxx", "User")
```

## 4. Edit the Configuration File

Edit the global configuration file (create it if it does not exist):

- macOS / Linux: `~/.config/opencode/opencode.json`
- Windows: `%USERPROFILE%\.config\opencode\opencode.json`

```json
{
  "$schema": "https://opencode.ai/config.json",
  "model": "beeroute/claude-sonnet-5-5",
  "providers": {
    "beeroute": {
      "name": "BeeRoute",
      "env": ["BEEROUTE_API_KEY"],
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": {
        "baseURL": "https://beeroute.ai/v1"
      },
      "models": {
        "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" },
        "claude-opus-5-5": { "name": "Claude Opus 5.5" },
        "gpt-6.2-sol": { "name": "GPT-6.2 Sol" },
        "gemini-4-neon": { "name": "Gemini 4 Neon" },
        "deepseek-flash": { "name": "DeepSeek Flash" },
        "kimi-k3.1-pro": { "name": "Kimi K3.1 Pro" },
        "glm-5.3": { "name": "GLM-5.3" }
      }
    }
  }
}
```

| Field | Description |
| :--- | :--- |
| `model` | Default model, in the format `provider-id/model-id` |
| `providers.beeroute` | Provider ID. You can choose any name; it is used when referencing models |
| `env` | Name of the environment variable that holds the key |
| `package` | `@opencode/ai/providers/openai-compatible` means the OpenAI Chat Completions format is used |
| `settings.baseURL` | Must include `/v1` |
| `models` | The models you want to use. Each key must be a BeeRoute model ID (case-sensitive); `name` is the display name |

Add or remove models under `models` as needed. See the [Pricing](https://beeroute.ai/modellist/) page for all model IDs. BeeRoute automatically converts Chat Completions requests for Claude, Gemini, and other models, so a single provider gives you access to all models.

?> You can also skip the environment variable: after starting OpenCode, enter `/connect`, choose **BeeRoute**, and enter the key manually; or run `opencode auth login beeroute --method key` in the terminal.

## 5. Launch and Verify

```bash
cd /path/to/your-project
opencode
```

Enter `/models`. You should see **BeeRoute** and the configured models in the list. Choose a model and send a message to test.

## 6. Advanced: Use Native API Formats (Optional)

If you want Claude models to use the native Anthropic Messages format and GPT models to use the Responses format, add two more providers (alongside the `beeroute` provider above):

```json
{
  "$schema": "https://opencode.ai/config.json",
  "providers": {
    "beeroute-claude": {
      "name": "BeeRoute Claude",
      "env": ["BEEROUTE_API_KEY"],
      "package": "@opencode/ai/providers/anthropic-compatible",
      "settings": {
        "baseURL": "https://beeroute.ai/v1"
      },
      "models": {
        "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" },
        "claude-opus-5-5": { "name": "Claude Opus 5.5" }
      }
    },
    "beeroute-responses": {
      "name": "BeeRoute Responses",
      "env": ["BEEROUTE_API_KEY"],
      "package": "@opencode/ai/providers/openai-compatible/responses",
      "settings": {
        "baseURL": "https://beeroute.ai/v1"
      },
      "models": {
        "gpt-6.2-sol": { "name": "GPT-6.2 Sol" }
      }
    }
  }
}
```

!> Here the `baseURL` for the Anthropic-compatible format also needs `/v1`: OpenCode appends `/messages` to the URL.

## 7. Configuration for the Older OpenCode 1.x

OpenCode 1.x uses the singular `provider` key along with the `npm` and `options` fields. 2.x can also read this format and migrates it automatically:

```json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "beeroute": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "BeeRoute",
      "options": {
        "baseURL": "https://beeroute.ai/v1",
        "apiKey": "{env:BEEROUTE_API_KEY}"
      },
      "models": {
        "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" },
        "gpt-6.2-sol": { "name": "GPT-6.2 Sol" }
      }
    }
  }
}
```

`apiKey` supports only two reference forms: `{env:VARIABLE_NAME}` and `{file:path/to/file}`. In 1.x, you can also choose **Other** in `/connect` and enter the provider ID `beeroute` and your key, in which case you can omit `apiKey`.

## 8. FAQ

**BeeRoute does not appear in `/models`?**

Check that the JSON is valid (you can use your editor's JSON validation). After saving, quit OpenCode completely and start it again.

**Getting a 401 error?**

The environment variable has not taken effect. Make sure you reopened the terminal after setting it, or use `/connect` to enter the key manually instead.

**Getting a 404 error?**

`baseURL` is missing `/v1`.

**Model not found?**

The keys under `models` must match the BeeRoute model IDs exactly.

For more settings, see the official OpenCode documentation: [Providers](https://opencode.ai/docs/providers).
