查看中文版 →

Deutsche Version ansehen →

Открыть русскую версию →

Ver versión en español →

Log in
Blog · Setup guides

OpenCode Base URL, API Key & Zen Setup

Custom endpoints in OpenCode depend on which major version you run. This guide covers opencode.json baseURL syntax, API key storage, and how OpenCode Zen compares to a fixed-allowance provider.

OpenCode separates the coding agent from the model vendor, so pointing it at a custom gateway, local server, or multi-model API platform only takes a provider block in opencode.json (or opencode.jsonc) and an API key. The main catch is that OpenCode 1.x and OpenCode 2.x use different field names for custom providers and store environment keys differently.

Quick reference: On OpenCode 1.x, set provider.<id>.options.baseURL and options.apiKey: "{env:VAR}" (or authenticate with opencode auth login). On OpenCode 2.x, use providers.<id>.settings.baseURL with env: ["VAR"] and store the secret via opencode service set env. Always include /v1 without a trailing slash when the gateway expects it. If you are deciding between an OpenCode Zen API key and a custom provider, Zen bills pay-as-you-go credits, while CodingPass plugs in as a custom Base URL starting at $10 a month ($60 monthly allowance, metered at official API prices).

Setting a custom Base URL in OpenCode

OpenCode loads global configuration from ~/.config/opencode/opencode.json (or opencode.jsonc) and merges project-level overrides from opencode.json in your repository root. Before editing the file, run opencode --version to confirm whether you are on 1.x or 2.x.

OpenCode 1.x configuration (provider + options.baseURL)

OpenCode 1.x uses a singular provider object, an npm adapter package, and nested options for baseURL and apiKey. Below is a minimal working config that connects CodingPass over the Anthropic-compatible adapter for Claude and open-weight models:

~/.config/opencode/opencode.json
{  "$schema": "https://opencode.ai/config.json",  "provider": {    "codingpass": {      "npm": "@ai-sdk/anthropic",      "name": "CodingPass",      "options": {        "baseURL": "https://codingpass.ai/v1",        "apiKey": "{env:CODINGPASS_API_KEY}"      },      "models": {        "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" },        "claude-opus-5-5": { "name": "Claude Opus 5.5" },        "deepseek-v4.1-flash": { "name": "DeepSeek V4.1 Flash" }      }    }  }}

For an OpenAI-compatible gateway or local server (such as Ollama or LM Studio), swap npm to "@ai-sdk/openai-compatible" and point baseURL at that server's /v1 root (for example, http://localhost:11434/v1).

OpenCode 2.x configuration (providers + settings.baseURL)

OpenCode 2.x renamed the top-level key to providers (plural), replaced npm with package, moved baseURL under settings, and declared environment variable names in an env array:

~/.config/opencode/opencode.jsonc
{  "$schema": "https://opencode.ai/config.json",  "model": "codingpass/claude-sonnet-5-5",  "providers": {    "codingpass": {      "name": "CodingPass",      "env": ["CODINGPASS_API_KEY"],      "package": "@opencode/ai/providers/anthropic",      "settings": { "baseURL": "https://codingpass.ai/v1" },      "models": {        "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" },        "claude-opus-5-5": { "name": "Claude Opus 5.5" },        "deepseek-v4.1-flash": { "name": "DeepSeek V4.1 Flash" }      }    },    "codingpass-openai": {      "name": "CodingPass (OpenAI)",      "env": ["CODINGPASS_API_KEY"],      "package": "@opencode/ai/providers/openai-compatible/responses",      "settings": { "baseURL": "https://codingpass.ai/v1" },      "models": {        "gpt-6.2-sol": { "name": "GPT-6.2 Sol" },        "gpt-6.1-terra": { "name": "GPT-6.1 Terra" }      }    }  }}

Common baseURL pitfalls

Most connection errors after adding a custom provider come down to three URL and protocol details:

  • Omit action paths: Never append /chat/completions, /responses, or /messages to baseURL. The underlying AI SDK appends those paths automatically based on the selected adapter
  • Include /v1 when required, without a trailing slash: Use https://codingpass.ai/v1 rather than https://codingpass.ai (which returns 404) or https://codingpass.ai/v1/ (a trailing slash can trigger double-slash path mismatches on strict proxies)
  • Match the protocol adapter to the endpoint: @ai-sdk/anthropic (v1) and @opencode/ai/providers/anthropic (v2) speak the Anthropic Messages API, preserving prompt caching and extended thinking for Claude models. @ai-sdk/openai-compatible (v1) and @opencode/ai/providers/openai-compatible/responses (v2) target OpenAI-style endpoints. On certain older OpenCode 1.x builds, custom @ai-sdk/anthropic blocks can drop credentials unless options.apiKey is explicitly set; if a third-party gateway still fails authentication on v1, verify options.apiKey or test the OpenAI-compatible adapter

Configuring and verifying your OpenCode API key

OpenCode supports three ways to store API keys. Choose the method that matches your OpenCode version and whether you share configs across machines.

1. Interactive login (opencode auth login and /connect)

For built-in providers (Anthropic, OpenAI, OpenCode Zen, and others), run opencode auth login in your terminal or /connect inside the TUI. When a vendor supports both OAuth and API keys, pass --method key to enter an API key directly:

Terminal
opencode auth login anthropic --method key

OpenCode writes the credentials to ~/.local/share/opencode/auth.json on macOS and Linux, or C:\Users\<username>\.local\share\opencode\auth.json on Windows.

2. Environment variable reference ({env:VAR_NAME})

For custom providers in opencode.json, referencing an environment variable keeps secrets out of dotfiles and Git repositories. Export the variable in your shell profile (~/.zshrc or ~/.bashrc) before launching OpenCode 1.x:

~/.zshrc
export CODINGPASS_API_KEY="cp-live-…"

3. OpenCode 2 shared background service (opencode service set env)

OpenCode 2 runs a single local background service shared by the terminal UI, desktop app, and web interface. Setting the key directly on that service ensures every client reads the same credential without extra shell configuration:

Terminal
opencode service set env CODINGPASS_API_KEY cp-live-…

Verifying the connection

Restart OpenCode (or reload config), run /models, pick a model under your custom provider, and send a short prompt:

Terminal
opencode/models

If the model does not appear in /models, check that you used providers on v2 (or provider on v1) and that the JSON has no syntax errors. If requests return 401, confirm the environment variable is exported in the current shell or registered with opencode service set env. If requests return 404, check that baseURL includes /v1 and that the model ID matches the provider catalog—for CodingPass, check the Models page.

OpenCode Zen API key vs BYOK vs CodingPass

When setting up OpenCode for the first time, developers often encounter OpenCode Zen alongside Bring Your Own Key (BYOK) and custom providers. They solve different billing problems inside the same OpenCode client.

OpenCode Zen is OpenCode's hosted pay-as-you-go gateway, managed through the OpenCode Console and routed via https://opencode.ai/zen/v1/. You sign in, load prepaid credits (with optional auto-reload when your balance drops below a threshold), generate a Zen API key, and connect it via /connect → OpenCode Zen (or OPENCODE_API_KEY). Models appear under the opencode/<model-id> namespace and bill per token from your credit balance, alongside a rotating set of free models.

Direct vendor BYOK connects your own Anthropic, OpenAI, or Gemini API keys directly to each vendor. OpenCode charges nothing; you pay each vendor's standard per-token API rates with separate dashboards and billing alerts.

CodingPass connects as a custom provider over a single baseURL (https://codingpass.ai/v1) and one API key, backed by a fixed monthly coding allowance. The Go plan starts at $10 a month for a $60 monthly pool metered at official API prices, with transparent caps ($30 weekly, $10 per 5-hour window) and hard 429 stops so an agent loop cannot run up unexpected charges. It covers Claude Sonnet 5.5, Opus 5.5, GPT, and open-weight models (up to $40 each on Go) under one key. See Usage limits and Pricing for full tier rules.

Comparison of OpenCode Zen, direct vendor BYOK, and CodingPass in OpenCode
DimensionOpenCode ZenDirect Vendor BYOKCodingPass
Billing modelPay-as-you-go prepaid credits (optional auto-reload)Pay-as-you-go per vendorFixed monthly plan from $10 ($60 pool on Go)
Setup in OpenCode/connect → OpenCode Zen or OPENCODE_API_KEYopencode auth login or vendor env varsCustom baseURL in opencode.json + one key
Endpointhttps://opencode.ai/zen/v1/Each vendor's official API hosthttps://codingpass.ai/v1
Multi-model accessCurated frontier and free models under one balanceOne key and bill per vendorClaude, GPT, Gemini, Grok, and open-weight models on one key
Cost protectionPrepaid balance; watch auto-reload settingsManual vendor spend limitsHard 5-hour, weekly, and monthly caps; no overage charges

Which setup fits your workflow

Scenario decision table for choosing an OpenCode API key and provider setup
Workload or scenarioRecommended setupReason and trade-off
Occasional coding or testing models with zero monthly commitmentOpenCode Zen API keyPrepaid credits with no recurring subscription; heavy agent sessions can deplete credits quickly if auto-reload is on
Daily coding with Claude Sonnet 5.5, Opus 5.5, and open-weight models on a fixed budgetCodingPass custom providerOne Base URL and key, monthly allowance metered at official API prices, no surprise overage bills
Company compliance requiring direct vendor contractsDirect vendor BYOK (opencode auth login)Requests go straight to your existing Anthropic or OpenAI account; requires managing separate keys and budgets
Offline work, strict local privacy, or internal proxyCustom @ai-sdk/openai-compatible providerPoint baseURL at Ollama, LM Studio, or an internal gateway; limited by local GPU speed and model quality

Frequently asked questions

Why did my custom provider stop showing up after upgrading to OpenCode 2?
OpenCode 2 changed the custom provider schema: provider became providers, npm became package, options became settings, and keys are passed via env plus opencode service set env. Updating those keys in opencode.jsonc restores the provider in /models.
Can I use an OpenCode Zen API key and a CodingPass provider at the same time?
Yes. OpenCode supports multiple providers side by side. You can keep OpenCode Zen connected for free or pay-as-you-go models while routing daily Claude and open-weight sessions through CodingPass, switching anytime in /models.
Should baseURL end with /v1 or /v1/?
End with /v1 and omit the trailing slash (for example, https://codingpass.ai/v1). Never append endpoint paths like /chat/completions or /messages, because the SDK adds those automatically.
Boarding

Keep OpenCode. Fix the monthly bill.

One custom Base URL and key puts Claude, GPT and open-weight models in /models. No surprise charges, cancel anytime.

  • Request ID on every response
  • Full refund if you've used 5% or less
  • No surprise charges
Choose GoOpenCode guide

$10/mo until you cancel.