Skip to main content
Qwen Code uses environment variables for API keys, configuration, and runtime behavior.

Loading Priority

Environment variables are loaded in the following order (highest to lowest priority):
  1. CLI flags (highest priority)
  2. System/Shell environment variables (export or inline)
  3. .env files (searched from current directory upward)
  4. settings.jsonenv field (lowest priority)

.env Files

Qwen Code automatically searches for .env files in the following order:
  1. .qwen/.env in current directory (searched upward to home)
  2. .env in current directory (searched upward to home)
  3. ~/.qwen/.env (user-level fallback)
  4. ~/.env (home directory fallback)

Example .env File

.env
# API Keys
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxx
OPENAI_API_KEY=sk-xxxxxxxxxxxxx
ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxx
GEMINI_API_KEY=AIzaxxxxxxxxxxxxx

# Optional: Custom base URLs
OPENAI_BASE_URL=https://api.openai.com/v1
ANTHROPIC_BASE_URL=https://api.anthropic.com

# Optional: Web search
TAVILY_API_KEY=tvly-xxxxxxxxxxxxx
GOOGLE_API_KEY=AIzaxxxxxxxxxxxxx
GOOGLE_SEARCH_ENGINE_ID=xxxxxxxxxxxxx

# Optional: Proxy configuration
HTTPS_PROXY=http://proxy.example.com:8080
NO_PROXY=localhost,127.0.0.1
Security: Never commit .env files to version control. Add .env to your .gitignore.

API Key Variables

Model Providers

DASHSCOPE_API_KEY
string
API key for Alibaba Cloud DashScope (Qwen models).Get your key at: Alibaba Cloud ModelStudio
BAILIAN_CODING_PLAN_API_KEY
string
API key for Alibaba Cloud Bailian Coding Plan subscription.Get your key at: Bailian Coding Plan
OPENAI_API_KEY
string
API key for OpenAI models.Get your key at: OpenAI API Keys
ANTHROPIC_API_KEY
string
API key for Anthropic Claude models.Get your key at: Anthropic Console
GEMINI_API_KEY
string
API key for Google Gemini models (via Google AI Studio).Get your key at: Google AI Studio
GOOGLE_API_KEY
string
API key for Google services (Vertex AI, Custom Search).

Custom Environment Variables

You can define custom environment variable names in modelProviders:
{
  "modelProviders": {
    "openai": [
      {
        "id": "qwen3-coder-plus",
        "name": "Qwen3 Coder Plus",
        "baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1",
        "envKey": "MY_CUSTOM_API_KEY"  // Custom variable name
      }
    ]
  }
}
Then set it in your environment:
export MY_CUSTOM_API_KEY=sk-xxxxxxxxxxxxx

Base URL Variables

OPENAI_BASE_URL
string
Custom base URL for OpenAI-compatible APIs.Example: https://api.openai.com/v1
ANTHROPIC_BASE_URL
string
Custom base URL for Anthropic API.Example: https://api.anthropic.com

Web Search Variables

TAVILY_API_KEY
string
API key for Tavily web search.Get your key at: Tavily
GOOGLE_SEARCH_ENGINE_ID
string
Google Custom Search Engine ID.Set up at: Google Custom Search

Proxy Variables

HTTPS_PROXY
string
HTTPS proxy server URL.Example: http://proxy.example.com:8080
HTTP_PROXY
string
HTTP proxy server URL.Example: http://proxy.example.com:8080
NO_PROXY
string
Comma-separated list of hosts to bypass proxy.Example: localhost,127.0.0.1,.local

Google Cloud Variables

GOOGLE_APPLICATION_CREDENTIALS
string
Path to Google Cloud service account key file (for Vertex AI).Example: /path/to/service-account-key.json
GOOGLE_CLOUD_PROJECT
string
Google Cloud project ID (for Vertex AI).
GOOGLE_GENAI_USE_VERTEXAI
string
Set to true to use Vertex AI instead of Google AI.

System Variables

DEBUG
string
Enable debug mode. Set to true or 1.
DEBUG=true qwen
DEBUG_MODE
string
Alternative to DEBUG. Set to true or 1.
NO_BROWSER
string
Disable browser opening for OAuth flows. Set to true or 1.
QWEN_OAUTH
string
Force Qwen OAuth authentication.
CLOUD_SHELL
string
Indicates running in Google Cloud Shell (set automatically).

Path Overrides

QWEN_CODE_SYSTEM_SETTINGS_PATH
string
Override system settings file path.
QWEN_CODE_SYSTEM_DEFAULTS_PATH
string
Override system defaults file path.
QWEN_CODE_TRUSTED_FOLDERS_PATH
string
Override trusted folders file path.

Sandbox Variables

SANDBOX
string
Sandbox execution environment identifier.
SANDBOX_SET_UID_GID
string
Set UID/GID in sandbox. Set to true or false.
SANDBOX_PORTS
string
Comma-separated list of ports to expose in sandbox.Example: 8080,3000,5000
SANDBOX_MOUNTS
string
Comma-separated list of volume mounts for sandbox.Example: /host/path:/container/path,/another:/path
BUILD_SANDBOX
string
Enable sandbox build mode.
QWEN_SANDBOX_PROXY_COMMAND
string
Custom proxy command for sandbox.

Testing Variables

QWEN_CODE_INTEGRATION_TEST
string
Enable integration test mode. Set to true.
NODE_ENV
string
Node.js environment. Set to development, production, or test.

Usage Examples

Setting Variables via Export

# Set for current session
export DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxx
export OPENAI_API_KEY=sk-xxxxxxxxxxxxx

# Run Qwen Code
qwen

Setting Variables Inline

# Set for single command
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxx qwen

Using .env File

Create ~/.qwen/.env:
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxx
OPENAI_API_KEY=sk-xxxxxxxxxxxxx
Variables are loaded automatically:
qwen  # API keys loaded from .env

Project-Specific .env

Create .qwen/.env in your project:
# Project-specific API key
DASHSCOPE_API_KEY=sk-project-specific-key

# Proxy for this project
HTTPS_PROXY=http://corporate-proxy:8080

Fallback in settings.json

For non-sensitive variables or defaults:
{
  "env": {
    "DASHSCOPE_API_KEY": "sk-xxxxxxxxxxxxx"
  }
}
This is not recommended for sensitive credentials. Use .env files instead.

Excluded Variables

Some environment variables are excluded from project context by default:
  • DEBUG
  • DEBUG_MODE
You can customize this list in settings:
{
  "advanced": {
    "excludedEnvVars": ["DEBUG", "DEBUG_MODE", "MY_SECRET_VAR"]
  }
}

Workspace Trust

When folder trust is enabled, project-level .env files are only loaded from trusted workspaces. User-level .env files (~/.qwen/.env, ~/.env) are always loaded. See Trusted Folders for more information.

Security Best Practices

  1. Never commit .env files - Add to .gitignore:
    .env
    .qwen/.env
    
  2. Use user-level .env for personal keys - Store in ~/.qwen/.env
  3. Use project .env for team settings - Document required variables without including actual keys
  4. Rotate keys regularly - Update API keys periodically
  5. Use environment-specific files - Consider .env.development, .env.production patterns
  6. Set file permissions - Restrict access to .env files:
    chmod 600 ~/.qwen/.env
    

Troubleshooting

Variable Not Loading

  1. Check the file location:
    ls -la .qwen/.env
    ls -la ~/.qwen/.env
    
  2. Verify file syntax (no quotes around values unless needed):
    # Correct
    API_KEY=sk-123456
    
    # Usually not needed
    API_KEY="sk-123456"
    
  3. Check for typos in variable names (case-sensitive)
  4. Ensure no trailing whitespace

Priority Issues

If a variable isn’t using the expected value:
  1. Check system environment: echo $VARIABLE_NAME
  2. Check .env file in current directory
  3. Check ~/.qwen/.env
  4. Check settings.jsonenv

Workspace Trust Issues

If project .env isn’t loading:
  1. Check if folder trust is enabled:
    {
      "security": {
        "folderTrust": {
          "enabled": true
        }
      }
    }
    
  2. Trust the workspace or move variables to ~/.qwen/.env

See Also