Skip to content

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.

  1. 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'" >> ~/.bashrc

    Alternatively, edit ~/.bashrc manually. Edit manually Open ~/.bashrc.

    HELPCODEESCAPE-zsh
    nano ~/.bashrc

    Add 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.

  2. Apply the changes.

    HELPCODEESCAPE-bash
    source ~/.bashrc
  3. Open 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:

  1. 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"
  2. 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.

  1. Check your default shell.

    HELPCODEESCAPE-bash
    echo $SHELL
  2. Follow the steps for your default shell.

    Zsh

    1. 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'" >> ~/.zshrc

      Alternatively, edit ~/.zshrc manually. Edit manually Open the shell configuration file.

      HELPCODEESCAPE-zsh
      nano ~/.zshrc

      Add 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.

    2. Apply the changes.

      HELPCODEESCAPE-zsh
      source ~/.zshrc
    3. Open a new terminal window and verify the environment variable is set.

      HELPCODEESCAPE-zsh
      echo $DASHSCOPE_API_KEY

    Bash

    1. 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_profile

      Alternatively, edit ~/.bash_profile manually. Edit manually Open the shell configuration file.

      HELPCODEESCAPE-bash
      nano ~/.bash_profile

      Add 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.

    2. Apply the changes.

      HELPCODEESCAPE-zsh
      source ~/.bash_profile
    3. Open 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.

  1. 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"
  2. 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.

  1. Press Win+Q, search for Edit the system environment variables , and click the result to open the System Properties window.

  2. 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.

  3. Click OK on all three windows to save the changes and close them.

  4. 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.

  1. 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"
  2. Open a new Command Prompt window.

  3. 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_KEY

Verify 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.

  1. 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)
  2. Open a new PowerShell window.

  3. 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_KEY

FAQ

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 . sudo does not inherit the current user's environment variables by default. Use sudo -E python xx.py to pass environment variables, or run python xx.py directly if you have the required permissions.

Mirror of Alibaba Cloud Model Studio docs for reference and RAG. Not affiliated with Alibaba Cloud.