Overview
Theauth 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:- Select provider from the list
- Enter API key when prompted
- Credentials saved to auth file
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:- Bearer token (
AWS_BEARER_TOKEN_BEDROCKor/connect) - AWS credential chain (profile, access keys, IAM roles)
opencode.json:
Vercel AI Gateway
Custom Providers
For providers not in the default list:opencode.json. See provider documentation for details.
URL-Based Authentication
For custom authentication servers:/.well-known/opencode with authentication information.
list
Display all authenticated providers.Usage
Example Output
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
- Shows list of authenticated providers
- Select provider to remove
- Credentials deleted from auth file
Credential Storage
Credentials are stored in:Environment Variables
You can also provide credentials via environment variables:.env file in your project:
Authentication Priority
OpenCode loads credentials in this order:- Environment variables (
PROVIDER_API_KEY) - Project
.envfile - Stored credentials (
~/.local/share/opencode/auth.json)
Provider Discovery
When you runopencode auth login, the list of providers comes from:
- models.dev - Central provider registry
- Plugins - Providers registered by installed plugins
- Custom config - Providers defined in
opencode.json
OAuth Authentication
Some providers use OAuth instead of API keys:Automatic Flow
For providers with automatic OAuth:- Select provider
- Browser opens to authorization page
- Grant permissions
- Tokens saved automatically
Manual Code Flow
For providers requiring manual code entry:- Select provider
- Open authorization URL
- Copy authorization code
- Paste code when prompted
- 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:- Plugin’s authentication flow executes
- May open browser, prompt for input, or use other methods
- Credentials saved to auth file
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 .gitignoreTroubleshooting
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 --refreshto 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
.envfile is in project root - Run
opencode auth listto see detected variables
Related Commands
models
List available models from providers
Providers
Learn about provider configuration
Configuration
Configure provider settings
Plugins
Custom authentication via plugins