Configure an API key as an environment variable

更新时间:
复制 MD 格式

We recommend setting your API key as an environment variable. This helps you avoid hard-coding the key in your source code, reducing the risk of accidental exposure.

Prerequisites

You have activated Alibaba Cloud Model Studio and obtained an API key. For more information, see Create an API key.

Procedure

Linux

Permanent

To make the API key available in all new sessions for the current user, set it as a permanent environment variable.

  1. Run the following command to append the environment variable setting to the ~/.bashrc file.

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

    Alternatively, you can manually edit the ~/.bashrc file.

    Manual modification

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

    nano ~/.bashrc

    Add the following content to the configuration file.

    # Replace YOUR_DASHSCOPE_API_KEY with your DashScope 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. Run the following command to apply the changes.

    source ~/.bashrc
  3. Open a new terminal window and run the following command to verify that the environment variable is set.

    echo $DASHSCOPE_API_KEY

Temporary

To use the 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 DashScope API key
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command to verify that the environment variable is set.

    echo $DASHSCOPE_API_KEY

macOS

Permanent

To make the API key available in all new sessions for the current user, set it as a permanent environment variable.

  1. Run the following command in your terminal to check your default shell type.

    echo $SHELL
  2. Proceed based on your default shell type.

    Zsh

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

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

      Alternatively, you can manually edit the ~/.zshrc file.

      Manual modification

      Run the following command to open the shell configuration file.

      nano ~/.zshrc

      Add the following content to the configuration file.

      # Replace YOUR_DASHSCOPE_API_KEY with your DashScope 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. Run the following command to apply the changes.

      source ~/.zshrc
    3. Open a new terminal window and run the following command to verify that the environment variable is set.

      echo $DASHSCOPE_API_KEY

    Bash

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

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

      Alternatively, you can manually edit the ~/.bash_profile file.

      Manual modification

      Run the following command to open the shell configuration file.

      nano ~/.bash_profile

      Add the following content to the configuration file.

      # Replace YOUR_DASHSCOPE_API_KEY with your DashScope 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. Run the following command to apply the changes.

      source ~/.bash_profile
    3. Open a new terminal window and run the following command to verify that the environment variable is set.

      echo $DASHSCOPE_API_KEY

Temporary

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

The following command applies to both Zsh and Bash.
  1. Run the following command.

    # Replace YOUR_DASHSCOPE_API_KEY with your DashScope API key
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command to verify that the environment variable is set.

    echo $DASHSCOPE_API_KEY

Windows

In Windows, you can configure an environment variable by using System Properties, Command Prompt, or Windows PowerShell.

System Properties

Note
  • Environment variables configured this way are permanent.

  • You need administrator permissions to modify system environment variables.

  • Changes to environment variables do not affect running applications, including Command Prompt windows and IDEs. You must restart these applications or open a new command-line session to apply the changes.

  1. On the Windows desktop, press Win+Q, search for Edit the system environment variables in the search box, and click the search result to open the System Properties window.

  2. In the System Properties window, click Environment Variable, and then 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 open windows to save the changes and close them.

  4. Open a new Command Prompt or Windows PowerShell window and run the appropriate command to verify that the environment variable is set.

    • In Command Prompt:

      echo %DASHSCOPE_API_KEY%
      Microsoft Windows [Version 10.0.19045.5371]
      (c) Microsoft Corporation. All rights reserved.
      C:\Windows\system32>echo %DASHSCOPE_API_KEY%
      sk-ee16697?fe4
      C:\Windows\system32>
    • In Windows PowerShell:

      echo $env:DASHSCOPE_API_KEY
      Windows PowerShell
      Copyright (C) Microsoft Corporation. All rights reserved.
      Try the new cross-platform PowerShell https://aka.ms/pscore6
      PS C:\Windows\system32> echo $env:DASHSCOPE_API_KEY
      sk-ee166797fe40xxx
      PS C:\Windows\system32>

Command Prompt

Permanent

To make the API key environment variable available in all new sessions for the current user, follow these steps.

  1. Run the following command in Command Prompt.

    REM Replace YOUR_DASHSCOPE_API_KEY with your DashScope API key
    setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"
  2. Open a new Command Prompt window for the change to take effect.

  3. In the new Command Prompt window, run the following command to verify that the environment variable is set.

    echo %DASHSCOPE_API_KEY%

Temporary

To use the environment variable only in the current session, run the following command in Command Prompt.

REM Replace YOUR_DASHSCOPE_API_KEY with your DashScope API key
set DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

You can run the following command in the same session to verify that the environment variable is set.

echo %DASHSCOPE_API_KEY%

Windows PowerShell

Permanent

To make the API key environment variable available in all new sessions for the current user, follow these steps.

  1. Run the following command in Windows PowerShell.

    # Replace YOUR_DASHSCOPE_API_KEY with your DashScope API key
    [Environment]::SetEnvironmentVariable("DASHSCOPE_API_KEY", "YOUR_DASHSCOPE_API_KEY", [EnvironmentVariableTarget]::User)
  2. Open a new Windows PowerShell window for the change to take effect.

  3. In the new Windows PowerShell window, run the following command to verify that the environment variable is set.

    echo $env:DASHSCOPE_API_KEY

Temporary

If you want to use the environment variable only in the current session, you can run the following command in PowerShell.

# Replace YOUR_DASHSCOPE_API_KEY with your DashScope API key
$env:DASHSCOPE_API_KEY = "YOUR_DASHSCOPE_API_KEY"

You can run the following command in the same session to verify that the environment variable is set.

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 report that the API key cannot be found?

A: This can occur for the following reasons:

  • Scenario 1: A temporary environment variable was set. A temporary variable is valid only within the current terminal session and does not affect running IDEs or other applications. Refer to the instructions in this topic to set a permanent environment variable.

  • Scenario 2: The IDE, command-line tool, or application was not restarted.

    • You must restart your IDE (such as VS Code) or terminal to load the new environment variables.

    • If you set the environment variable after deploying an application, you must restart the application service to reload the environment variables.

  • Scenario 3: The variable is missing from a service configuration file. If your application is started by a service manager, such as systemd or supervisord, you may need to add the environment variable to the service manager's configuration file.

  • Scenario 4: Using the sudo command. If you use sudo python xx.py to run the script, the environment variables of the current user may not be inherited. This is because sudo does not inherit all environment variables by default. You can use the sudo -E python xx.py command, where the -E parameter ensures that the environment variables are passed. If you have permission to run the script, you can run python xx.py directly.