> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/QwenLM/qwen-code/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment Variables

> Environment variable reference for Qwen Code

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.json` → `env` 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

```bash .env theme={null}
# 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
```

<Warning>
  **Security:** Never commit `.env` files to version control. Add `.env` to your `.gitignore`.
</Warning>

## API Key Variables

### Model Providers

<ParamField path="DASHSCOPE_API_KEY" type="string">
  API key for Alibaba Cloud DashScope (Qwen models).

  Get your key at: [Alibaba Cloud ModelStudio](https://modelstudio.console.alibabacloud.com)
</ParamField>

<ParamField path="BAILIAN_CODING_PLAN_API_KEY" type="string">
  API key for Alibaba Cloud Bailian Coding Plan subscription.

  Get your key at: [Bailian Coding Plan](https://modelstudio.console.aliyun.com/?tab=dashboard#/efm/coding_plan)
</ParamField>

<ParamField path="OPENAI_API_KEY" type="string">
  API key for OpenAI models.

  Get your key at: [OpenAI API Keys](https://platform.openai.com/api-keys)
</ParamField>

<ParamField path="ANTHROPIC_API_KEY" type="string">
  API key for Anthropic Claude models.

  Get your key at: [Anthropic Console](https://console.anthropic.com/)
</ParamField>

<ParamField path="GEMINI_API_KEY" type="string">
  API key for Google Gemini models (via Google AI Studio).

  Get your key at: [Google AI Studio](https://makersuite.google.com/app/apikey)
</ParamField>

<ParamField path="GOOGLE_API_KEY" type="string">
  API key for Google services (Vertex AI, Custom Search).
</ParamField>

### Custom Environment Variables

You can define custom environment variable names in `modelProviders`:

```json theme={null}
{
  "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:

```bash theme={null}
export MY_CUSTOM_API_KEY=sk-xxxxxxxxxxxxx
```

## Base URL Variables

<ParamField path="OPENAI_BASE_URL" type="string">
  Custom base URL for OpenAI-compatible APIs.

  Example: `https://api.openai.com/v1`
</ParamField>

<ParamField path="ANTHROPIC_BASE_URL" type="string">
  Custom base URL for Anthropic API.

  Example: `https://api.anthropic.com`
</ParamField>

## Web Search Variables

<ParamField path="TAVILY_API_KEY" type="string">
  API key for Tavily web search.

  Get your key at: [Tavily](https://tavily.com)
</ParamField>

<ParamField path="GOOGLE_SEARCH_ENGINE_ID" type="string">
  Google Custom Search Engine ID.

  Set up at: [Google Custom Search](https://programmablesearchengine.google.com/)
</ParamField>

## Proxy Variables

<ParamField path="HTTPS_PROXY" type="string">
  HTTPS proxy server URL.

  Example: `http://proxy.example.com:8080`
</ParamField>

<ParamField path="HTTP_PROXY" type="string">
  HTTP proxy server URL.

  Example: `http://proxy.example.com:8080`
</ParamField>

<ParamField path="NO_PROXY" type="string">
  Comma-separated list of hosts to bypass proxy.

  Example: `localhost,127.0.0.1,.local`
</ParamField>

## Google Cloud Variables

<ParamField path="GOOGLE_APPLICATION_CREDENTIALS" type="string">
  Path to Google Cloud service account key file (for Vertex AI).

  Example: `/path/to/service-account-key.json`
</ParamField>

<ParamField path="GOOGLE_CLOUD_PROJECT" type="string">
  Google Cloud project ID (for Vertex AI).
</ParamField>

<ParamField path="GOOGLE_GENAI_USE_VERTEXAI" type="string">
  Set to `true` to use Vertex AI instead of Google AI.
</ParamField>

## System Variables

<ParamField path="DEBUG" type="string">
  Enable debug mode. Set to `true` or `1`.

  ```bash theme={null}
  DEBUG=true qwen
  ```
</ParamField>

<ParamField path="DEBUG_MODE" type="string">
  Alternative to `DEBUG`. Set to `true` or `1`.
</ParamField>

<ParamField path="NO_BROWSER" type="string">
  Disable browser opening for OAuth flows. Set to `true` or `1`.
</ParamField>

<ParamField path="QWEN_OAUTH" type="string">
  Force Qwen OAuth authentication.
</ParamField>

<ParamField path="CLOUD_SHELL" type="string">
  Indicates running in Google Cloud Shell (set automatically).
</ParamField>

## Path Overrides

<ParamField path="QWEN_CODE_SYSTEM_SETTINGS_PATH" type="string">
  Override system settings file path.
</ParamField>

<ParamField path="QWEN_CODE_SYSTEM_DEFAULTS_PATH" type="string">
  Override system defaults file path.
</ParamField>

<ParamField path="QWEN_CODE_TRUSTED_FOLDERS_PATH" type="string">
  Override trusted folders file path.
</ParamField>

## Sandbox Variables

<ParamField path="SANDBOX" type="string">
  Sandbox execution environment identifier.
</ParamField>

<ParamField path="SANDBOX_SET_UID_GID" type="string">
  Set UID/GID in sandbox. Set to `true` or `false`.
</ParamField>

<ParamField path="SANDBOX_PORTS" type="string">
  Comma-separated list of ports to expose in sandbox.

  Example: `8080,3000,5000`
</ParamField>

<ParamField path="SANDBOX_MOUNTS" type="string">
  Comma-separated list of volume mounts for sandbox.

  Example: `/host/path:/container/path,/another:/path`
</ParamField>

<ParamField path="BUILD_SANDBOX" type="string">
  Enable sandbox build mode.
</ParamField>

<ParamField path="QWEN_SANDBOX_PROXY_COMMAND" type="string">
  Custom proxy command for sandbox.
</ParamField>

## Testing Variables

<ParamField path="QWEN_CODE_INTEGRATION_TEST" type="string">
  Enable integration test mode. Set to `true`.
</ParamField>

<ParamField path="NODE_ENV" type="string">
  Node.js environment. Set to `development`, `production`, or `test`.
</ParamField>

## Usage Examples

### Setting Variables via Export

```bash theme={null}
# Set for current session
export DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxx
export OPENAI_API_KEY=sk-xxxxxxxxxxxxx

# Run Qwen Code
qwen
```

### Setting Variables Inline

```bash theme={null}
# Set for single command
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxx qwen
```

### Using .env File

Create `~/.qwen/.env`:

```bash theme={null}
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxx
OPENAI_API_KEY=sk-xxxxxxxxxxxxx
```

Variables are loaded automatically:

```bash theme={null}
qwen  # API keys loaded from .env
```

### Project-Specific .env

Create `.qwen/.env` in your project:

```bash theme={null}
# 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:

```json theme={null}
{
  "env": {
    "DASHSCOPE_API_KEY": "sk-xxxxxxxxxxxxx"
  }
}
```

<Warning>
  This is **not recommended** for sensitive credentials. Use `.env` files instead.
</Warning>

## Excluded Variables

Some environment variables are excluded from project context by default:

* `DEBUG`
* `DEBUG_MODE`

You can customize this list in settings:

```json theme={null}
{
  "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](/configuration/trusted-folders) for more information.

## Security Best Practices

1. **Never commit `.env` files** - Add to `.gitignore`:
   ```gitignore theme={null}
   .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:
   ```bash theme={null}
   chmod 600 ~/.qwen/.env
   ```

## Troubleshooting

### Variable Not Loading

1. Check the file location:
   ```bash theme={null}
   ls -la .qwen/.env
   ls -la ~/.qwen/.env
   ```

2. Verify file syntax (no quotes around values unless needed):
   ```bash theme={null}
   # 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.json` → `env`

### Workspace Trust Issues

If project `.env` isn't loading:

1. Check if folder trust is enabled:
   ```json theme={null}
   {
     "security": {
       "folderTrust": {
         "enabled": true
       }
     }
   }
   ```

2. Trust the workspace or move variables to `~/.qwen/.env`

## See Also

* [Settings.json Reference](/configuration/settings)
* [Model Providers](/configuration/model-providers)
* [Trusted Folders](/configuration/trusted-folders)
