← Back to blog
Technical guide · OpenCode

Add an API key and custom provider to OpenCode 2

This guide covers where OpenCode 2 reads API keys, adding a custom provider, why environment variables get ignored, image input and exact error messages. Every step was tested on OpenCode 2.4.4 on 3 December 2026. The examples use Vora Compute's API for Qwen4 35B-A3B (requests using qwen3.8-27b route automatically to qwen4-35b-a3b) and work for any OpenAI-compatible API by changing three values.

15 free messages in Chat (no card) · €9.90/month (or €99/year) first-year launch offer through 31 December (excl. VAT).

Compare your token bill

Where OpenCode 2 takes an API key from

OpenCode 2 reads a provider's API key from three places, in this order when more than one is set: a saved key first, then an environment variable, then the config file. We tested every combination.

Where OpenCode 2 takes an API key from
Saved keyEnvironment variableKey in the config file
Set it with/connect in the app, or opencode auth loginA variable named in the provider's env listsettings.apiKey: the key itself, {env:NAME} or {file:path}
Where it livesOpenCode's local database, as plain textThe environment of the process that serves the requestThe config file, or the file it points to
Works from any terminalYesOnly if that process has the variableYes for the key itself and {file:path}; {env:NAME} behaves like a variable
Order when several are setFirstSecondThird
Remove it withopencode auth logoutUnset the variableDelete the line

Add an API key for a provider in OpenCode's list

Inside OpenCode, /connect opens a searchable list of providers (/login and /auth open the same list); choose one and paste your key. From an interactive terminal, run the command below to open the same list.

Terminal
opencode auth login

Run opencode auth list to see what is saved. OpenCode's own models are an exception: choose OpenCode in that list and sign in, with no key to paste.

Add a custom OpenAI-compatible provider

  1. Save this (a minimal config that worked against Vora's live API) as opencode.json in your project folder, or ~/.config/opencode/opencode.json for every project (opencode.jsonc works too). For another provider, change three values: the provider id (vora), baseURL and the model id (qwen4-35b-a3b); name and env are yours to choose.
opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "providers": {
    "vora": {
      "name": "Vora Compute",
      "package": "@opencode/ai/providers/openai-compatible",
      "env": ["VORA_API_KEY"],
      "settings": { "baseURL": "https://api.voracompute.com/v1" },
      "models": { "qwen4-35b-a3b": {} }
    }
  },
  "model": "vora/qwen4-35b-a3b"
}
  1. Create a Vora API key in the Vora console, then save it. One prompt appears, "Manually enter API Key", and input is masked. In the app, /connect lists the provider by its name for the same key.
Terminal
opencode auth login vora
  1. Check the setup. The first command should print Vora Compute Vora Compute stored, and the second should list vora/qwen4-35b-a3b.
Terminal
opencode auth list
opencode models
  1. Run a request. The first output line names the answering model: > build · qwen4-35b-a3b.
Terminal
opencode run -m "vora/qwen4-35b-a3b" "Say OK."

A provider must be defined in the config before its key can be saved: OpenCode 2.4.4 has no free-form entry for unknown providers. The model line sets the default, so opencode alone starts on it. See OpenCode's provider docs for reference.

What a minimal config leaves to defaults

Declaring a model as {} works, but OpenCode 2.4.4 fills in the defaults below.

What a minimal config leaves to defaults
If you leave outOpenCode 2.4.4 assumesSet it when
limit200,000 tokens of context and 32,000 of outputThe model's real limits differ
capabilities.reasoning.effortLevels low, medium and high, with no noneYou want none, or the model takes other levels
capabilities.inputText and imagesThe endpoint takes text only
compatibility.maxTokensFieldThe output limit is sent as max_completion_tokensThe API only reads max_tokens
nameThe ids are shown (vora, qwen4-35b-a3b)You want readable names in lists

For our complete Qwen4 35B-A3B config on Vora (1M context limit, reasoning levels and timeouts), see the OpenCode pricing and setup guide.

When OpenCode ignores an environment variable

The process serving the request reads the variable. That is OpenCode 2's shared background server when one is running. It keeps the environment it started with. When no server is running, opencode run serves the request itself, with your shell's environment.

So the same export can work in one terminal and fail in another. If the server started before your export, no key is sent: OpenCode says nothing, you see the provider's error (Missing API key on Vora), and opencode auth list prints No authenticated integrations. An unset {env:NAME} becomes an empty string with no warning.

Three tested options:

  1. Save the key as shown above, which works through the background server.
  2. Point settings.apiKey at a file (absolute, ~/, or relative to the config). OpenCode reads the file when needed, so this also works through a background server that never saw the variable.
opencode.json
"settings": {
  "baseURL": "https://api.voracompute.com/v1",
  "apiKey": "{file:~/.secrets/vora-key}"
}
  1. Run --standalone for a private server reading your shell, useful in scripts and CI.
Terminal
export VORA_API_KEY=vora_sk_...
opencode --standalone

For the background server only, opencode service set env VORA_API_KEY vora_sk_... stores plain text in ~/.config/opencode/service.json (opencode service unset env VORA_API_KEY removes it).

Image input with a custom model

Guides for OpenCode 1 tell you to declare image support. OpenCode 2 enables it by default for custom models, so you declare the opposite case instead. With "input": ["text", "image"] or nothing declared, a PNG reached Vora and Qwen3.8 27B answered a question about it. If an endpoint accepts text only, set it as below.

opencode.json
"models": {
  "my-text-model": {
    "capabilities": { "input": ["text"] }
  }
}

In our test, OpenCode then sent the model a short text note in place of the image, and the model answered that it could not see it, with no error.

A broken config falls back to a free model

In our test, removing one comma made OpenCode 2.4.4 skip the file without a message. With the file skipped, no model line was left, and opencode run chose one of OpenCode's own free models for the request (big-pickle in that test). The same happened with a valid file whose model line was missing, or named a provider or model that the config does not define (seaglass-alpha-free). No account was connected and outbound traffic was blocked, so nothing was sent. Trailing commas and comments are accepted; unknown config fields are dropped silently.

Two checks: the first output line names the model, and opencode debug config lists loaded files (keys as ***); an unlisted file did not parse. Passing -m fails loudly instead of falling back.

Coming from OpenCode 1

OpenCode 2.4.4 still reads the OpenCode 1 format: it converts it in memory, with no warning, and leaves the file untouched. An unchanged OpenCode 1 config worked in our test with its key. Write new providers in the OpenCode 2 format, and never mix both inside one provider: OpenCode 2 fields such as compatibility are dropped there.

Coming from OpenCode 1
OpenCode 1OpenCode 2
providerproviders
npm: "@ai-sdk/openai-compatible"package: "@opencode/ai/providers/openai-compatible"
options.baseURLsettings.baseURL
options.apiKeysettings.apiKey
tool_call, modalitiescapabilities
Keys in auth.jsonKeys in the local database, imported once

Errors and what they mean

The two messages about the API key and the one about a model that does not exist are worded by Vora's API (another provider words its own); the rest come from OpenCode.

API key errors

API key errors
What you seeCauseWhat to do
Missing API key. Send the header Authorization: Bearer vora_sk_…The request left with no key: the process that served it does not have your variableSave the key with opencode auth login vora
Invalid API key. Create one at https://inference.voracompute.com/console/keys.The key is wrong, or an older saved key wins over your variableCheck opencode auth list, then log out and save the right key
Integration not found: voraopencode auth login vora ran where no config defines voraRun it in the project folder, or put the provider in the global config
API key input requires an interactive terminalopencode auth login ran without a terminalUse --standalone with a variable, or a {file:path} key

Config file errors

Config file errors
What you seeCauseWhat to do
Model unavailable: vora/qwen4-35b-a3bThe provider or model is not in the config that loaded: a typo, or a file with a JSON errorRun opencode debug config and check that your file is listed
> build · big-pickleNo valid model line, so OpenCode chose one of its free modelsFix the model line, or pass -m
Cannot initialize vora/qwen4-35b-a3b: Cannot find package '@opencode/ai'…The package line is misspelledCopy the package line exactly
Unsupported package for vora/qwen4-35b-a3bThe package line is missingAdd the package line

Request errors

Request errors
What you seeCauseWhat to do
Variant unavailable for vora/qwen4-35b-a3b: noneThat reasoning level is not declared for the modelDeclare it in capabilities.reasoning.effort, or use low, medium or high
Provider request failed with HTTP 404 (POST …/chat/completions)baseURL does not end in /v1End baseURL with /v1
The model 'qwen4-35b-a3b-x' does not exist. See GET /v1/models.The model id is not one the API servesUse an id from the provider's model list

Frequently asked questions

Where does OpenCode store API keys?
In a local SQLite database as plain text. Run opencode debug paths db to print its path. OpenCode 1 stored keys in auth.json, which OpenCode 2 imports once.
How do I remove or replace a saved key?
Run opencode auth logout vora "Vora Compute" (the second argument is the label from opencode auth list), then save the new key with opencode auth login vora. Until removed, a saved key keeps overriding an environment variable.
Why does the desktop app have no form for a custom provider?
OpenCode removed the custom provider entry from the connect dialog in version 2.2.0, and on 3 December 2026 the request to restore it was still open. Define custom providers in opencode.json as shown above.
How do I set a default reasoning level?
A #level suffix on the root model line is ignored. Set it on an agent ("agents": { "build": { "model": "vora/qwen4-35b-a3b#medium" } }), per run with -m "vora/qwen4-35b-a3b#high", or inside the app with /variants.
Where do I get an API key for OpenCode's own models?
Inside OpenCode, sign in through /connect (choose OpenCode). For other tools, create an oc_sk_ key in the OpenCode Console under Account, Personal API keys. Use base URL https://opencode.ai/inference, or https://opencode.ai/inference/go for OpenCode Go.

Related guides

Get a Vora API key for OpenCode.

Flat rate on European GPUs with zero API retention: €9.90/month or €99/year (€8.25/month) for the first year through 31 December 2026 (then €29/month or €288/year, excl. VAT). Up to 4 parallel requests.

WhatsApp