OpenCode Configuration
1. What OpenCode is and what you need
Section titled “1. What OpenCode is and what you need”OpenCode is an open source AI coding agent. It can read repositories, edit code, run commands, and work with desktop, web, and IDE interfaces.
Before connecting OpenCode through RouteAPI, prepare:
- A working terminal environment
- Your RouteAPI key
- A model available in the RouteAPI console and suitable for coding tasks
- Prefer a model that supports the OpenAI-compatible protocol and tool calls
2. Install OpenCode
Section titled “2. Install OpenCode”Install the CLI
Section titled “Install the CLI”For macOS / Linux / WSL, the official recommended installation is:
curl -fsSL https://opencode.ai/install | bashIf you prefer global Node.js installation:
npm install -g opencode-aiFor native Windows PowerShell:
npm install -g opencode-aiVerify installation
Section titled “Verify installation”opencode --versionA version number means OpenCode CLI is installed.
3. Recommended path: save the key, then write project config
Section titled “3. Recommended path: save the key, then write project config”OpenCode documentation recommends saving credentials with /connect first, then declaring provider and model in a config file. For RouteAPI, choose Other.
Save the RouteAPI key in OpenCode
Section titled “Save the RouteAPI key in OpenCode”Start OpenCode:
opencodeThen run in the session:
/connectComplete these steps:
- Select
Other - Set the provider ID to
routeapi - Paste your RouteAPI key
OpenCode writes credentials to local ~/.local/share/opencode/auth.json, so manual editing is usually not needed.
Create opencode.json in the project root
Section titled “Create opencode.json in the project root”OpenCode supports both global ~/.config/opencode/opencode.json and project-level opencode.json. For team collaboration, keep non-sensitive configuration in the project root.
{ "$schema": "https://opencode.ai/config.json", "provider": { "routeapi": { "npm": "@ai-sdk/openai-compatible", "name": "RouteAPI", "options": { "baseURL": "https://www.routeapi.ai/v1" }, "models": { "your-routeapi-model-id": { "name": "RouteAPI Model" } } } }, "model": "routeapi/your-routeapi-model-id"}In this configuration:
routeapi: the custom provider ID, which must match the provider ID used in/connectnpm: use@ai-sdk/openai-compatiblefor/v1/chat/completions; use@ai-sdk/openaifor/v1/responsesoptions.baseURL:https://www.routeapi.ai/v1models: use the actual model ID available in the RouteAPI console, not a display namemodel: the default model format isprovider_id/model_id, hererouteapi/your-routeapi-model-id
4. Alternative: use environment variables instead of /connect
Section titled “4. Alternative: use environment variables instead of /connect”If you prefer loading the key from an environment variable, set options.apiKey in opencode.json. This variable syntax is supported by OpenCode documentation.
4.1 Set the environment variable
Section titled “4.1 Set the environment variable”If you use bash, zsh, or sh on macOS / Linux / WSL:
export ROUTEAPI_KEY="your RouteAPI key"If you use Windows PowerShell:
$env:ROUTEAPI_KEY="your RouteAPI key"4.2 Reference the environment variable in opencode.json
Section titled “4.2 Reference the environment variable in opencode.json”{ "$schema": "https://opencode.ai/config.json", "provider": { "routeapi": { "npm": "@ai-sdk/openai-compatible", "name": "RouteAPI", "options": { "baseURL": "https://www.routeapi.ai/v1", "apiKey": "{env:ROUTEAPI_KEY}" }, "models": { "your-routeapi-model-id": { "name": "RouteAPI Model" } } } }, "model": "routeapi/your-routeapi-model-id"}5. Select a model and start OpenCode
Section titled “5. Select a model and start OpenCode”After configuration, select a model and start:
- Start OpenCode:
opencode- Run in the session:
/models- Select a model under
routeapi. Ifmodelis already set inopencode.json, OpenCode uses it by default.
To temporarily switch the model for the current session, use:
opencode -m routeapi/your-routeapi-model-idFor lightweight tasks such as title generation, you can add a cheaper model:
{ "small_model": "routeapi/your-small-model-id"}6. Common troubleshooting
Section titled “6. Common troubleshooting”- Run
opencode auth listto confirm thatrouteapicredentials are saved locally - If
/modelsdoes not show your custom provider, check that the provider ID in/connectexactly matchesrouteapiinopencode.json - If models appear but requests fail with a protocol error, check the
npmpackage: use@ai-sdk/openai-compatiblefor/v1/chat/completionsand@ai-sdk/openaifor/v1/responses - On Windows, if file access is slow, terminal behavior is abnormal, or paths differ, prefer running OpenCode in WSL
- To compare other RouteAPI clients, continue with Codex configuration, Cursor configuration, and Claude Code configuration