Use environment variables with Azure AI services

This guide shows you how to set and retrieve environment variables to handle your Azure AI services subscription credentials in a more secure way when you test out applications.

Set an environment variable

To set environment variables, use one the following commands, where the ENVIRONMENT_VARIABLE_KEY is the named key and value is the value stored in the environment variable.

Use the following command to create and assign a persisted environment variable, given the input value.

:: Assigns the env var to the value
setx ENVIRONMENT_VARIABLE_KEY "value"

In a new instance of the Command Prompt, use the following command to read the environment variable.

:: Prints the env var value
echo %ENVIRONMENT_VARIABLE_KEY%

Tip

After you set an environment variable, restart your integrated development environment (IDE) to ensure that the newly added environment variables are available.

Retrieve an environment variable

To use an environment variable in your code, it must be read into memory. Use one of the following code snippets, depending on which language you're using. These code snippets demonstrate how to get an environment variable given the ENVIRONMENT_VARIABLE_KEY and assign the value to a program variable named value.

For more information, see Environment.GetEnvironmentVariable .

using static System.Environment;

class Program
{
    static void Main()
    {
        // Get the named env var, and assign it to the value variable
        var value =
            GetEnvironmentVariable(
                "ENVIRONMENT_VARIABLE_KEY");
    }
}

Next steps