Overview
Qwen Code supports two authentication methods, each suited for different use cases:Qwen OAuth
Recommended for most users
- Sign in with your qwen.ai account
- 1,000 free requests per day
- No API key management required
- Browser-based authentication flow
API Key
For advanced users and production
- Use your own API keys
- Support for multiple providers
- Full control over models and quotas
- Works in headless environments
You can switch between authentication methods at any time using the
/auth command.Qwen OAuth (Recommended)
Qwen OAuth is the easiest way to get started with Qwen Code. Sign in once and get immediate access to AI-powered coding assistance.Features
- Free tier: 1,000 requests per day at no cost
- No API keys: No need to manage API keys or billing
- Auto-refresh: Credentials refresh automatically
- Secure: OAuth 2.0 standard with token encryption
Setup
Usage in Production
Environments where OAuth won’t work:- Continuous Integration (CI/CD) pipelines
- Docker containers without display
- SSH sessions without X11 forwarding
- Headless servers
- Automated scripts
Credential Storage
OAuth credentials are stored securely:- Location:
~/.qwen/auth/oauth-tokens.json - Encryption: Tokens are encrypted at rest
- Auto-refresh: Access tokens refresh automatically before expiration
- Privacy: Never shared or sent to third parties
Checking Your Usage
Check remaining requests at any time:Signing Out
To sign out and clear credentials:API Key Authentication
API key authentication gives you full control over which AI provider and model to use. It’s ideal for:- Production deployments
- CI/CD pipelines
- Custom model configurations
- Team environments with shared API keys
- Users who want to use their own API quotas
Supported Providers
Qwen Code supports multiple AI providers through API key authentication:- OpenAI-Compatible
- Anthropic
- Google GenAI
Providers:
- Alibaba Cloud Dashscope (Qwen models)
- Alibaba Cloud Bailian Coding Plan
- OpenAI (GPT models)
- OpenRouter (multiple models)
- Any OpenAI-compatible API
- API key
- Base URL (if not using OpenAI directly)
- Model ID
Quick Setup with settings.json
The recommended way to configure API key authentication is using~/.qwen/settings.json:
Get your API key
- Dashscope: Get key from Alibaba Cloud Dashscope
- OpenAI: Get key from OpenAI Platform
- Anthropic: Get key from Anthropic Console
- Google: Get key from Google AI Studio
Configuration Fields
| Field | Description |
|---|---|
modelProviders | Define available models per protocol (openai, anthropic, gemini) |
modelProviders[].id | Model ID sent to the API (e.g., qwen3-coder-plus, gpt-4o) |
modelProviders[].name | Display name shown in model selection |
modelProviders[].baseUrl | API endpoint URL (required for non-default endpoints) |
modelProviders[].envKey | Environment variable name that holds the API key |
modelProviders[].description | Optional description shown in model selection |
env | Fallback place to store API keys (prefer environment variables for security) |
security.auth.selectedType | Default auth protocol: openai, anthropic, gemini, vertex-ai, qwen-oauth |
model.name | Default model to use on startup |
Example Configurations
Alibaba Cloud Bailian Coding Plan
Alibaba Cloud Bailian Coding Plan
Multiple Providers (OpenAI + Anthropic + Gemini)
Multiple Providers (OpenAI + Anthropic + Gemini)
Configure multiple providers and switch between them:Switch between models during a session:
Enable Thinking Mode (qwen3.5-plus)
Enable Thinking Mode (qwen3.5-plus)
For supported models with extended reasoning:
Environment Variables
For better security, set API keys via environment variables instead of storing them insettings.json:
Priority Order
Qwen Code checks for API keys in this order (highest to lowest priority):- Environment variables (from your shell or
.envfile) - Custom
envKey(defined inmodelProviders[].envKey) - settings.json
envfield (fallback, lowest priority)
Switching Authentication Methods
You can change authentication methods at any time:Select new method
Choose from:
- Qwen OAuth
- OpenAI-compatible API
- Anthropic API
- Google GenAI
- Vertex AI
Project-Specific Configuration
Override global settings for specific projects by creating.qwen/settings.json in your project root:
Validation
Qwen Code validates your authentication configuration on startup:Troubleshooting
OAuth browser doesn't open
OAuth browser doesn't open
Solutions:
- Manually open the URL shown in the terminal
- Check if your
BROWSERenvironment variable is set correctly - Try running
qwenfrom a different terminal emulator - Use API key authentication in headless environments
API key not found
API key not found
Solutions:
- Verify the environment variable name matches
envKeyin settings - Check if the variable is exported:
echo $DASHSCOPE_API_KEY - Restart your terminal after adding to
.bashrc/.zshrc - Use absolute paths for
.envfiles
Invalid API key error
Invalid API key error
Solutions:
- Verify the API key is correct (no extra spaces)
- Check if the key has expired
- Ensure you have sufficient quota/credits
- Verify the key has permissions for the model you’re using
Wrong model provider
Wrong model provider
Solutions:
- Check
security.auth.selectedTypein settings.json - Ensure the model ID matches the provider protocol
- Use
/modelto switch models during a session - Verify
baseUrlmatches your provider’s API endpoint
Rate limit exceeded
Rate limit exceeded
Solutions:
- For Qwen OAuth: Wait until your daily quota resets (midnight UTC)
- For API keys: Check your provider’s rate limits
- Use
/compressto reduce token usage - Switch to a different model or provider with
/model
Security Best Practices
Use Environment Variables
Store API keys in environment variables or
.env files, never in settings.json committed to git.Rotate Keys Regularly
Rotate API keys periodically, especially after team member changes.
Limit Key Scope
Use API keys with minimal required permissions. Avoid admin or billing access.
Use .gitignore
Add
.env and settings.json files containing secrets to .gitignore.Next Steps
Quick Start
Complete your first AI-assisted coding session
Model Providers
Learn more about configuring multiple providers
Environment Variables
Full reference for environment variable configuration
Settings Reference
Complete settings.json documentation
