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
Security: Never commit .env files to version control. Add .env to your .gitignore.

API Key Variables

Model Providers

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

Custom Environment Variables

You can define custom environment variable names in modelProviders:
Then set it in your environment:

Base URL Variables

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

Web Search Variables

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

Proxy Variables

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

Google Cloud Variables

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

System Variables

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

Path Overrides

string
Override system settings file path.
string
Override system defaults file path.
string
Override trusted folders file path.

Sandbox Variables

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

Testing Variables

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

Usage Examples

Setting Variables via Export

Setting Variables Inline

Using .env File

Create ~/.qwen/.env:
Variables are loaded automatically:

Project-Specific .env

Create .qwen/.env in your project:

Fallback in settings.json

For non-sensitive variables or defaults:
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:

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

Troubleshooting

Variable Not Loading

  1. Check the file location:
  2. Verify file syntax (no quotes around values unless needed):
  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:
  2. Trust the workspace or move variables to ~/.qwen/.env

See Also