Skip to main content
Getting Started

Configure your API key

Avoid hardcoding secrets

Prerequisites

Create an API key first, in the region you will call. The environment variable stores only the key; your code must also use the Base URL of the same region. See Regions and endpoints.

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 QwenCloud's base URL for the region where the API key was created:
    • In code:
# Singapore (ap-southeast-1). For another region, replace ap-southeast-1 in the URL.
# API keys are region-specific. See https://docs.qwencloud.com/developer-guides/getting-started/regions
dashscope.base_http_api_url = 'https://ap-southeast-1.maas.qwencloudapi.com/api/v1'
  • As environment variable:
# Singapore (ap-southeast-1). For another region, replace ap-southeast-1 in the URL.
# API keys are region-specific. See https://docs.qwencloud.com/developer-guides/getting-started/regions
export DASHSCOPE_HTTP_BASE_URL='https://ap-southeast-1.maas.qwencloudapi.com/api/v1'