All Products
Search
Document Center

Alibaba Cloud Model Studio:Set API key as an environment variable

Last Updated:Dec 03, 2024

We recommend that you set your API key as an environment variable to avoid explicitly specifying it when you use Alibaba Cloud Model Studio SDK and reduce the risk of an API leak. This topic describes how to set an API key as an environment variable.

Procedure

Linux

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command to add the environment variable to the ~/.bashrc file:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bashrc

    You can also edit the ~/.bashrc file manually.

    Edit manually

    Run the following command to open the ~/.bashrc file.

    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. Run the following command to make the change take effect:

    source ~/.bashrc
  3. Create a session and run the following command to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY

Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY

macOS

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command to check the default shell type.

    echo $SHELL
  2. Perform the following operation based on your shell type.

    Zsh

    1. Run the following command to add the environment variable to the ~/.zshrc file.

      # Replace YOUR_DASHSCOPE_API_KEY with your API key.
      echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.zshrc

      You can also edit the ~/.zshrc file manually.

      Edit manually

      Run the following command to open the configuration file.

      nano ~/.zshrc

      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. Run the following command to make the change take effect:

      source ~/.zshrc
    3. Create a session and run the following command to check whether the environment variable takes effect:

      echo $DASHSCOPE_API_KEY

    Bash

    1. Run the following command to add the environment variable to the ~/.bash_profile file.

      # Replace YOUR_DASHSCOPE_API_KEY with your API key.
      echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bash_profile

      You can also edit the ~/.bash_profile file manually.

      Edit manually

      Run the following command to open the file.

      nano ~/.bash_profile

      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. Run the following command to make the change take effect:

      source ~/.bash_profile
    3. Create a session and run the following command to check whether the environment variable takes effect:

      echo $DASHSCOPE_API_KEY

Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY

Windows

In Windows, you can run commands by using Command Line or PowerShell.

Command Line

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"
  2. Create a new session.

  3. Run the following command to check whether the environment variable takes effect:

    echo %DASHSCOPE_API_KEY%

Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    set DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command in the current session to check whether the environment variable takes effect:

    echo %DASHSCOPE_API_KEY%

PowerShell

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    [Environment]::SetEnvironmentVariable("DASHSCOPE_API_KEY", "YOUR_DASHSCOPE_API_KEY", [EnvironmentVariableTarget]::User)
  2. Create a session.

  3. Run the following command to check whether the environment variable takes effect:

    echo $env:DASHSCOPE_API_KEY

Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    $env:DASHSCOPE_API_KEY = "YOUR_DASHSCOPE_API_KEY"
  2. Run the following command in the current session to check whether the environment variable takes effect:

    echo $env:DASHSCOPE_API_KEY

FAQ

Q: I have run the echo command to confirm the environment variable. But when I run my code, it still says no API key found or invalid API key?

  • Scenario 1: Usually you need to restart the IDE (such as VS Code) or command-line tools to load the latest environment variables.

  • Scenario 2: If environment variables are set after you deploy your application, you may need to restart the application service for it to reload the environment variables.

  • Scenario 3: If your application is started through a service manager (such as systemd or supervisord), you may need to add the environment variables to the configuration file of the service manager.

  • Scenario 4: If you run the script using sudo python xx.py, you may miss the environment variables of the current user because sudo does not inherit all environment variables by default. You can use the command sudo -E python xx.py, where the -E option ensures that environment variables are passed through. If you have permission to execute the script, you can run it directly using python xx.py.

  • Scenario 5: You may need to configure the base URL of Model Studio. Use one of the following methods:

    • Add the base URL to your code:

      dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    • Set the base URL as an environment variable:

      DASHSCOPE_HTTP_BASE_URL='https://dashscope-intl.aliyuncs.com/api/v1'