Skip to main content

Overview

The auth command manages authentication credentials for AI model providers. OpenCode uses models.dev provider list, allowing you to use API keys from any supported provider. Credentials are stored in ~/.local/share/opencode/auth.json and loaded automatically when OpenCode starts.

Usage

Subcommands

login

Add credentials for a provider

list

Show all authenticated providers

logout

Remove credentials for a provider

login

Authenticate with a provider by storing API credentials.

Usage

Interactive Flow

When run without arguments, the command guides you through authentication:
  1. Select provider from the list
  2. Enter API key when prompted
  3. Credentials saved to auth file
Example interaction:

Provider-Specific Instructions

OpenCode

Recommended for best experience:

Anthropic

For Claude models:

OpenAI

For GPT models:

GitHub Copilot

Use your Copilot subscription:

Amazon Bedrock

Bedrock uses AWS credential chain:
Authentication priority:
  1. Bearer token (AWS_BEARER_TOKEN_BEDROCK or /connect)
  2. AWS credential chain (profile, access keys, IAM roles)
Configure in opencode.json:

Vercel AI Gateway

Custom Providers

For providers not in the default list:
Then configure the provider in opencode.json. See provider documentation for details.

URL-Based Authentication

For custom authentication servers:
The provider must expose /.well-known/opencode with authentication information.

list

Display all authenticated providers.

Usage

Alias:

Example Output

If environment variables are set, they’re also shown:

Authentication Types

The command shows the authentication method:
  • api: API key authentication
  • oauth: OAuth-based authentication
  • wellknown: Custom authentication endpoint

logout

Remove stored credentials for a provider.

Usage

Interactive Flow

  1. Shows list of authenticated providers
  2. Select provider to remove
  3. Credentials deleted from auth file
Example:

Credential Storage

Credentials are stored in:
Format:
Keep your auth.json file secure. It contains sensitive API keys.

Environment Variables

You can also provide credentials via environment variables:
Or in a .env file in your project:
Environment variables take precedence over stored credentials.

Authentication Priority

OpenCode loads credentials in this order:
  1. Environment variables (PROVIDER_API_KEY)
  2. Project .env file
  3. Stored credentials (~/.local/share/opencode/auth.json)
Higher priority sources override lower ones.

Provider Discovery

When you run opencode auth login, the list of providers comes from:
  1. models.dev - Central provider registry
  2. Plugins - Providers registered by installed plugins
  3. Custom config - Providers defined in opencode.json
Disable providers in configuration:
Or enable only specific providers:

OAuth Authentication

Some providers use OAuth instead of API keys:

Automatic Flow

For providers with automatic OAuth:
  1. Select provider
  2. Browser opens to authorization page
  3. Grant permissions
  4. Tokens saved automatically

Manual Code Flow

For providers requiring manual code entry:
  1. Select provider
  2. Open authorization URL
  3. Copy authorization code
  4. Paste code when prompted
  5. Tokens saved

Token Refresh

OpenCode automatically refreshes OAuth tokens before they expire. If refresh fails:

Plugin Authentication

Plugins can register custom authentication methods. When you select a plugin-registered provider:
  1. Plugin’s authentication flow executes
  2. May open browser, prompt for input, or use other methods
  3. Credentials saved to auth file
See plugin documentation for creating custom authentication.

Security Best Practices

Use environment variables

For CI/CD and automated environments

Rotate keys regularly

Update API keys periodically

Limit key permissions

Use least-privilege API keys when possible

Don't commit credentials

Add .env and auth.json to .gitignore

Troubleshooting

Authentication Failed

Problem: Invalid API key error Solutions:
  • Verify key is correct (copy/paste carefully)
  • Check key has proper permissions
  • Ensure key hasn’t expired
  • Try generating a new key

Provider Not Listed

Problem: Can’t find provider in list Solutions:
  • Run opencode models --refresh to update provider list
  • Select “Other” and enter provider ID manually
  • Check provider is supported on models.dev
  • Configure custom provider in opencode.json

OAuth Failed

Problem: OAuth authorization fails Solutions:
  • Try again (tokens may have expired)
  • Clear browser cookies for the provider
  • Check internet connectivity
  • Verify the provider supports OAuth

Environment Variables Not Working

Problem: Environment variables not recognized Solutions:
  • Check variable name matches provider convention
  • Verify variables are exported: export VARIABLE=value
  • Confirm .env file is in project root
  • Run opencode auth list to see detected variables

models

List available models from providers

Providers

Learn about provider configuration

Configuration

Configure provider settings

Plugins

Custom authentication via plugins