Skip to main content

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 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

1

Launch Qwen Code

Start Qwen Code from your terminal:
2

Run auth command

Type the auth command:
3

Select Qwen OAuth

Choose Qwen OAuth from the authentication options menu.
4

Complete browser flow

  1. A browser window opens automatically
  2. Sign in with your qwen.ai account (or create a new account)
  3. Authorize Qwen Code to access your account
  4. You’ll be redirected to a success page
  5. Return to your terminal—authentication is complete!

Usage in Production

Qwen OAuth requires interactive browser access. In non-interactive environments (CI/CD, Docker containers, SSH sessions without X11), use API key authentication instead.
Environments where OAuth won’t work:
  • Continuous Integration (CI/CD) pipelines
  • Docker containers without display
  • SSH sessions without X11 forwarding
  • Headless servers
  • Automated scripts
For these scenarios, use API Key authentication.

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:
Output:

Signing Out

To sign out and clear credentials:
Or manually remove 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:
Providers:
  • Alibaba Cloud Dashscope (Qwen models)
  • Alibaba Cloud Bailian Coding Plan
  • OpenAI (GPT models)
  • OpenRouter (multiple models)
  • Any OpenAI-compatible API
Required:
  • 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:
1

Create settings file

Create or edit ~/.qwen/settings.json in your home directory:
2

Add configuration

Example for Qwen models via Dashscope:
3

Get your API key

4

Start Qwen Code

Your configuration takes effect automatically.

Configuration Fields

Example Configurations

Higher quotas with a fixed monthly subscription:
Get your API key from Alibaba Cloud Bailian.
Configure multiple providers and switch between them:
Switch between models during a session:
For supported models with extended reasoning:

Environment Variables

For better security, set API keys via environment variables instead of storing them in settings.json:
Never commit API keys to version control. Use environment variables or .env files (added to .gitignore).

Priority Order

Qwen Code checks for API keys in this order (highest to lowest priority):
  1. Environment variables (from your shell or .env file)
  2. Custom envKey (defined in modelProviders[].envKey)
  3. settings.json env field (fallback, lowest priority)
Example:

Switching Authentication Methods

You can change authentication methods at any time:
1

Run /auth command

2

Select new method

Choose from:
  • Qwen OAuth
  • OpenAI-compatible API
  • Anthropic API
  • Google GenAI
  • Vertex AI
3

Complete setup

Follow the prompts to configure the new authentication method.

Project-Specific Configuration

Override global settings for specific projects by creating .qwen/settings.json in your project root:
Project settings override global settings:
Now this project always uses Claude, while other projects use your global defaults.
Use project-specific settings to configure different models for different types of projects (e.g., Claude for research, Qwen for coding).

Validation

Qwen Code validates your authentication configuration on startup:
If validation fails, you’ll see helpful error messages:

Troubleshooting

Solutions:
  1. Manually open the URL shown in the terminal
  2. Check if your BROWSER environment variable is set correctly
  3. Try running qwen from a different terminal emulator
  4. Use API key authentication in headless environments
Solutions:
  1. Verify the environment variable name matches envKey in settings
  2. Check if the variable is exported: echo $DASHSCOPE_API_KEY
  3. Restart your terminal after adding to .bashrc/.zshrc
  4. Use absolute paths for .env files
Solutions:
  1. Verify the API key is correct (no extra spaces)
  2. Check if the key has expired
  3. Ensure you have sufficient quota/credits
  4. Verify the key has permissions for the model you’re using
Solutions:
  1. Check security.auth.selectedType in settings.json
  2. Ensure the model ID matches the provider protocol
  3. Use /model to switch models during a session
  4. Verify baseUrl matches your provider’s API endpoint
Solutions:
  1. For Qwen OAuth: Wait until your daily quota resets (midnight UTC)
  2. For API keys: Check your provider’s rate limits
  3. Use /compress to reduce token usage
  4. 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.
The ~/.qwen/settings.json file in your home directory should be kept private. Never commit it to version control.

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