Skip to main content
Preparation

Configure your API key

Avoid hardcoding secrets

Prerequisites

Create an API key first.

Steps

  • Linux
  • macOS
  • Windows

Permanent environment variable

Set a permanent environment variable for the current user:
1

Add the environment variable

Add the variable to ~/.bashrc:
# Replace YOUR_DASHSCOPE_API_KEY with your API key.
echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bashrc
Open ~/.bashrc:
nano ~/.bashrc
Add the following content to the file:
# Replace YOUR_DASHSCOPE_API_KEY with your API key.
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
In the nano editor, press Ctrl+X and then Y. Press Enter to save and close the file.
2

Apply the changes

Apply changes:
source ~/.bashrc
3

Verify

Verify in a new session:
echo $DASHSCOPE_API_KEY

Temporary environment variable

Set a temporary variable (current session only):
1

Set the variable

# Replace YOUR_DASHSCOPE_API_KEY with your API key.
export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
2

Verify

echo $DASHSCOPE_API_KEY

FAQ

echo works but code reports "no API key found"

Common causes:
  • Non-permanent variable: Temporary variables work in the current session only. Set a permanent variable instead.
  • Need restart: Restart IDE, terminal, or app. Service-managed apps may need service restart.
  • Service manager config: For service-managed apps (systemd, supervisord), add the variable to the service config file.
  • Using sudo: sudo doesn't inherit environment variables. Use sudo -E python xx.py (the -E flag passes variables) or run without sudo if permissions allow.
  • Base URL required: Set Qwen Cloud's base URL:
    • In code:
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
  • As environment variable:
export DASHSCOPE_HTTP_BASE_URL='https://dashscope-intl.aliyuncs.com/api/v1'