Appearance
Export an API key as an environment variable
Set your API key as an environment variable to avoid hard-coding it in source code and reduce the risk of accidental exposure.
Prerequisites
You have activated Alibaba Cloud Model Studio and have an API key. See Create an API key.
Procedure
Linux
Permanent
A permanent environment variable persists across all new sessions for the current user.
Append the API key to
~/.bashrc.HELPCODEESCAPE-bash # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bashrcAlternatively, edit
~/.bashrcmanually. Edit manually Open~/.bashrc.HELPCODEESCAPE-zsh nano ~/.bashrcAdd the following line to the configuration file.
HELPCODEESCAPE-bash # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"In the nano editor, press Ctrl+X, then Y, and then Enter to save and close the file.
Apply the changes.
HELPCODEESCAPE-bash source ~/.bashrcOpen a new terminal window and verify the environment variable is set.
HELPCODEESCAPE-bash echo $DASHSCOPE_API_KEY
Temporary
To set the environment variable for the current session only:
Export the API key.
HELPCODEESCAPE-bash # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"Verify the environment variable is set.
HELPCODEESCAPE-bash echo $DASHSCOPE_API_KEY
macOS
Permanent
A permanent environment variable persists across all new sessions for the current user.
Check your default shell.
HELPCODEESCAPE-bash echo $SHELLFollow the steps for your default shell.
Zsh
Append the API key to
~/.zshrc.HELPCODEESCAPE-zsh # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.zshrcAlternatively, edit
~/.zshrcmanually. Edit manually Open the shell configuration file.HELPCODEESCAPE-zsh nano ~/.zshrcAdd the following line to the configuration file.
HELPCODEESCAPE-bash # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"In the nano editor, press Ctrl+X, then Y, and then Enter to save and close the file.
Apply the changes.
HELPCODEESCAPE-zsh source ~/.zshrcOpen a new terminal window and verify the environment variable is set.
HELPCODEESCAPE-zsh echo $DASHSCOPE_API_KEY
Bash
Append the API key to
~/.bash_profile.HELPCODEESCAPE-zsh # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bash_profileAlternatively, edit
~/.bash_profilemanually. Edit manually Open the shell configuration file.HELPCODEESCAPE-bash nano ~/.bash_profileAdd the following line to the configuration file.
HELPCODEESCAPE-bash # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"In the nano editor, press Ctrl+X, then Y, and then Enter to save and close the file.
Apply the changes.
HELPCODEESCAPE-zsh source ~/.bash_profileOpen a new terminal window and verify the environment variable is set.
HELPCODEESCAPE-zsh echo $DASHSCOPE_API_KEY
Temporary
To set the environment variable for the current session only:
The following command applies to both Zsh and Bash.
Export the API key.
HELPCODEESCAPE-bash # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"Verify the environment variable is set.
HELPCODEESCAPE-bash echo $DASHSCOPE_API_KEY
Windows
On Windows, configure an environment variable with System Properties, Command Prompt, or PowerShell.
System Properties
Note
An environment variable configured with this method is permanent.
You must have administrator permissions to modify system environment variables.
Changes do not apply to running applications. Restart them or open a new terminal window for the changes to take effect.
Press
Win+Q, search for Edit the system environment variables , and click the result to open the System Properties window.In the System Properties window, click Environment Variable . In the System Variables section, click Create . For Variable Name , enter
DASHSCOPE_API_KEY, and for Variable value, enter your DashScope API key.Click OK on all three windows to save the changes and close them.
Open a new Command Prompt or PowerShell window and verify the environment variable is set.
Command Prompt command:
HELPCODEESCAPE-powershell echo %DASHSCOPE_API_KEY%PowerShell command:
HELPCODEESCAPE-powershell echo $env:DASHSCOPE_API_KEY
Command Prompt
Permanent
To persist the environment variable across sessions, follow these steps.
Set the variable in Command Prompt.
HELPCODEESCAPE-bash REM Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"Open a new Command Prompt window.
Verify the environment variable in the new window.
HELPCODEESCAPE-bash echo %DASHSCOPE_API_KEY%
Temporary
To set a temporary environment variable for the current session:
HELPCODEESCAPE-cmd
REM Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key.
set DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEYVerify the environment variable in the same session.
HELPCODEESCAPE-bash
echo %DASHSCOPE_API_KEY%PowerShell
Permanent
To persist the environment variable across sessions, follow these steps.
Set the variable in PowerShell.
HELPCODEESCAPE-powershell # Replace YOUR_DASHSCOPE_API_KEY with your Alibaba Cloud Model Studio API key. [Environment]::SetEnvironmentVariable("DASHSCOPE_API_KEY", "YOUR_DASHSCOPE_API_KEY", [EnvironmentVariableTarget]::User)Open a new PowerShell window.
Verify the environment variable in the new window.
HELPCODEESCAPE-powershell echo $env:DASHSCOPE_API_KEY
Temporary
To set a temporary environment variable for the current session:
HELPCODEESCAPE-powershell
$env:DASHSCOPE_API_KEY = "YOUR_DASHSCOPE_API_KEY"Verify the environment variable in the same session.
HELPCODEESCAPE-powershell
echo $env:DASHSCOPE_API_KEYFAQ
Q: I used the echo command and confirmed the environment variable was set correctly. Why does my code still fail with an "API key not found" error?
A: This can happen for several reasons:
Reason 1: You set a temporary environment variable. Temporary variables are valid only for the current terminal session. Set a permanent environment variable instead.
Reason 2: You did not restart your IDE, command line interface, or application.
Restart your IDE (such as VS Code) or open a new terminal to load the new environment variable.
If you set the environment variable after deploying an application, restart the service to load the new variables.
Reason 3: The application is started by a service manager . If
systemd,supervisord, or another service manager starts your application, add the environment variable to the service's configuration file.Reason 4: Using sudo .
sudodoes not inherit the current user's environment variables by default. Usesudo -E python xx.pyto pass environment variables, or runpython xx.pydirectly if you have the required permissions.