GuidesHosting choices

OpenCode API Key: Get One, Add Provider Keys, Where It's Stored

Create an OpenCode API key in the Console, sign OpenCode in, add Anthropic or OpenRouter keys, see where OpenCode 2 stores them, and rotate or remove one.

December 1, 2026The Everpod team
The short answer

An OpenCode API key is a key for OpenCode’s own models (the Console, formerly Zen, and Go). You create it at opencode.ai/console under Account, Personal API keys, Add API key; it starts oc_sk_ and is shown only once. OpenCode itself doesn’t need one: in version 2 you run /connect, choose OpenCode and sign in with your account. A key for Anthropic, OpenAI, OpenRouter or another provider goes in with /connect or opencode auth login, and OpenCode 2 keeps it in its SQLite database, ~/.local/share/opencode/opencode.db, readable only by you. Version 1 kept keys in auth.json in the same folder. As of OpenCode 2.4.3, December 1, 2026.

Two kinds of key

OpenCode’s own key pays for the models OpenCode sells: the pay-as-you-go catalogue, billed from a credit balance (OpenCode Zen, now the Console), and the OpenCode Go subscription. It works in OpenCode and in other agents such as Claude Code and Codex.

A provider’s key is one you already have from Anthropic, OpenAI, OpenRouter, DeepSeek or any other provider OpenCode supports. OpenCode stores it and bills nothing; the provider charges you directly. Getting one is the provider’s business (for OpenRouter, the OpenRouter key guide); adding it to OpenCode is below.

OpenCode’s free models need neither.

Creating an OpenCode key in the Console

  1. Sign in at opencode.ai/console and add credit, or subscribe to Go, in the workspace the key is for.
  2. Open Account from the account menu. Under Personal API keys, click Add API key.
  3. Choose the workspace, name the key, and set an expiration if you want one: never, 1, 7 or 30 days, or a custom 1 to 365 days.
  4. Copy it straight away. In the Console API docs “The full key is shown only once, right after you create it”; afterwards only its prefix shows.

A personal key “acts as you”: your role, budget and model rules apply to it, and it works in the one workspace chosen when it was made. Ordinary members can create keys that list and call models; owners and admins can also set a key to All, Restricted or Read only, and a restricted key needs Models set to Request to call a model. For a shared server or CI, the docs suggest a service account key instead, “so usage isn’t tied to one person”: owners and admins make one under Settings, Service accounts. Removing a member from the workspace also revokes the keys they created there.

Connecting OpenCode to your OpenCode account

In OpenCode 2, run /connect in the TUI, or opencode auth login in a shell, and choose OpenCode. It offers two methods:

In version 1 you chose OpenCode in /connect, signed in at opencode.ai/auth and pasted the key it gave you. If a key OpenCode has stored is refused with “Invalid credential” or “This API key has expired”, the invalid-credential fixes cover it.

Adding a provider’s API key

In the TUI, /connect lists the providers; pick one and paste the key, then choose a model with /models. From a shell, the providers CLI docs give these. The first asks for a provider and a method, the second goes straight to API-key entry for Anthropic, and the third shows what is connected, and how:

opencode auth login
opencode auth login anthropic --method key
opencode auth list
Copied

The key is typed at a prompt, so it stays out of your shell history. That also means it can’t be scripted: “API-key entry and provider forms require an interactive terminal.” Method IDs differ by provider, and opencode auth login <provider> without --method shows the ones a provider has.

Where OpenCode keeps the key

WhatUsual place (Linux and macOS)Permissions
Keys and sign-ins, version 2
~/.local/share/opencode/opencode.db (SQLite)600, set by OpenCode, with its -wal and -shm files
Variables given to the background service
~/.config/opencode/service.json (plain JSON)600, set by OpenCode
Keys, version 1
~/.local/share/opencode/auth.json (plain JSON)600, set by OpenCode

The database path is the usual one; opencode debug paths db prints the real one, and OPENCODE_DB can move it. On Windows, version 1 used %USERPROFILE%\.local\share\opencode\auth.json; for version 2 the docs give no Windows path, so ask opencode debug paths db, and the file is protected by your user profile’s permissions rather than a file mode.

The docs say plainly: “Do not edit the database to manage credentials; use /connect or the auth commands.” The same database holds your sessions, so a backup or copy of opencode.db carries your keys with it, and so does a heap snapshot, which the troubleshooting page warns “can contain prompts, file contents, and credentials.”

Keys from environment variables

OpenCode also picks a key up from the provider’s usual variable, such as ANTHROPIC_API_KEY or OPENROUTER_API_KEY. In version 2 it has to be set “on the server process that runs model requests”, which is OpenCode’s background service, not your shell. An export in your shell reaches it only if the service starts after it. The lasting way is the service’s own environment, read here from a prompt so the key never lands in shell history. After the first line, paste the key and press Enter; nothing is shown. The last line should list the provider with type environment:

read -rs KEY
opencode service set env ANTHROPIC_API_KEY "$KEY"
unset KEY
opencode auth list
Copied

Setting it stops the running service, and the next OpenCode command starts it with the new value. The value is written in plain text to service.json (mode 600, above). Two rules from the docs matter when a key seems to be ignored: an environment connection is not an account, so opencode auth logout can’t remove it, and “A saved account takes precedence over an environment connection for the same integration.” If both exist, the saved key wins.

For one run without the service, the docs show the variable on the command line with opencode --standalone. Typed that way, the key is saved in your shell history; set it from a prompt as above, or from a file only you can read.

A key in opencode.json

A provider entry in your config can carry its own key under settings.apiKey. Write a reference there, never the key itself, so the file can be committed or shared. {env:NAME} reads a variable and {file:path} reads a file:

{
  "$schema": "https://opencode.ai/config.json",
  "providers": {
    "anthropic": {
      "settings": {
        "apiKey": "{env:ANTHROPIC_API_KEY}"
      }
    },
    "openai": {
      "settings": {
        "apiKey": "{file:~/.secrets/openai-key}"
      }
    }
  }
}
Copied

Both fail quietly: an unset variable or a missing file “will be replaced with an empty string”, so the request goes out with an empty key and no warning. In version 2 the variable is read from the background service’s environment; one report of an empty key turned out to be a service started before the variable was exported. A key file should be readable by you alone. One way to write it without the key reaching your history or the screen:

mkdir -p ~/.secrets && chmod 700 ~/.secrets
(umask 077; read -rs KEY; printf '%s' "$KEY" > ~/.secrets/openai-key)
Copied

A relative {file:} path is read from the config file’s folder. Which config file wins when several set a provider is in the OpenCode config guide.

Several keys for one provider

“Each successful API-key, OAuth, or command login creates a saved account,” and the newest becomes active. Running /connect on a provider you already use opens its list of accounts, where you can activate, rename or delete one. From a shell, by the account’s label:

opencode auth list
opencode auth switch anthropic work
Copied

Rotating or removing a key

To replace a key, create the new one, connect it with /connect or opencode auth login (it becomes the active account), check it works, then remove the old account. For an OpenCode key there is no renewal: “An expired key can’t be renewed, so create a new one.”

This deletes the saved account labelled work from OpenCode; the key itself stays valid at the provider:

opencode auth logout anthropic work
Copied

Without a provider and label it opens a picker. A key from the environment is removed by unsetting it:

opencode service unset env ANTHROPIC_API_KEY
Copied

Removing a key from OpenCode does not stop it working. If it may have leaked, revoke it where it was issued: an OpenCode key in the Console, where revoked and expired keys “stop working immediately and return HTTP 401”, and a provider’s key in that provider’s dashboard.

One copy is easy to miss after an upgrade. Version 2 imports keys from version 1’s auth.json once and never writes back to it, so the old file keeps every key it held. If you no longer run version 1, delete it; version 1 would need its keys added again:

rm ~/.local/share/opencode/auth.json
Copied

Moving from OpenCode 1 to 2 covers what else the upgrade carries over.

Using the OpenCode key in other tools

Outside OpenCode the key is a bearer token, sent as Authorization: Bearer oc_sk_... to the endpoints under https://opencode.ai/inference, and the examples in OpenCode’s model docs hold it in OPENCODE_API_KEY. A personal key bills your own budget in the workspace it was made in; a Go workspace’s key uses Go, a workspace with credit pays as it goes. The settings for Claude Code and Codex, and which base URL each model uses, are in the OpenCode Zen guide, and the free models’ refusal outside OpenCode in “only within OpenCode”.

On version 1

opencode --version tells you which version you have. Version 1’s keys live in ~/.local/share/opencode/auth.json, written with mode 600, which opencode auth logout edits for you. Its CLI reference says it also loads keys “defined in your environments or a .env file in your project”, so a provider key in a project’s .env is picked up when you start OpenCode there. In its source, version 1 reads the whole credential store from an OPENCODE_AUTH_CONTENT variable, as JSON, when that is set, instead of the file.

Run OpenCode on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.