All Products
Search
Document Center

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

Last Updated:Oct 22, 2024

You can 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.

Environment variables are used to store information about the system environment within an operating system. You can set your API key as an environment variable to protect the API key from being leaked even if your code repository is made public. The method for setting an API key as an environment variable varies based on your operating system and use scenario.

Linux

When you use the command line in a Linux operating system such as Ubuntu or CentOS to configure your API key as an environment variable, you can choose to configure the API key as a temporary environment variable for the current session or as a permanent environment variable for the current user.

  • Configure a temporary environment variable

    To configure and use your API key as a temporary environment variable for the current session, run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

    Run the following command in the current session to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY
  • Configure a permanent environment variable for the current user

    To configure your API key as a permanent environment variable for the current user to use in new sessions, copy and paste the following command to the ~/.bashrc file:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

    Alternatively, directly run the following command to add the preceding command to the ~/.bashrc file:

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

    Run the following command to make the environment variable take effect:

    source ~/.bashrc

    Create a session and run the following command to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY

macOS

When you use the command line in macOS to configure your API key as an environment variable, you can choose to configure the API key as a temporary environment variable for the current session or as a permanent environment variable for the current user.

  • Configure a temporary environment variable

    To configure and use your API key as a temporary environment variable for the current session, run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

    Run the following command in the current session to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY
  • Configure a permanent environment variable for the current user

    To configure your API key as a permanent environment variable for the current user to use in new sessions, copy and paste the following command to the ~/.zshrc or ~/.bash_profile file based on your shell type:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

    Alternatively, directly run either of the following commands to add the preceding command to the ~/.zshrc or ~/.bash_profile file:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    ## Run the following command if you use Z shell:
    echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.zshrc
    
    ## Run the following command if you use Bash:
    echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bash_profile

    Run either of the following commands based on your shell type to make the environment variable take effect:

    ## Run the following command if you use Z shell:
    source ~/.zshrc
    
    ## Run the following command if you use Bash:
    source ~/.bash_profile

    Create a session and 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

When you use Command Line to configure your API key as an environment variable, you can choose to configure the API key as a temporary environment variable for the current session or as a permanent environment variable for the current user.

  • Configure a temporary environment variable

    To configure and use your API key as a temporary environment variable for the current session, run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    set DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

    Run the following command in the current session to check whether the environment variable takes effect:

    echo %DASHSCOPE_API_KEY%
  • Configure a permanent environment variable for the current user

    To configure your API key as a permanent environment variable for the current user by using Command Line, run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"

    Create a new session and run the following command to check whether the environment variable takes effect:

    echo %DASHSCOPE_API_KEY%

PowerShell

When you use PowerShell to configure your API key as an environment variable, you can choose to configure the API key as a temporary environment variable for the current session or as a permanent environment variable for the current user.

  • Configure a temporary environment variable

    To configure and use your API key as a temporary environment variable for the current session, run the following command:

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

    Run the following command in the current session to check whether the environment variable takes effect:

    echo $env:DASHSCOPE_API_KEY
  • Configure a permanent environment variable for the current user

    To configure your API key as a permanent environment variable for the current user by using PowerShell, run the following command:

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

    Create a session and run the following command to check whether the environment variable takes effect:

    echo $env:DASHSCOPE_API_KEY