This guide shows you how to set and retrieve environment variables for your Azure AI services credentials when you test applications.
Important
If you use an API key, store it securely somewhere else, such as in Azure Key Vault. Don't include the API key directly in your code, and never post it publicly.
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%
Use the following command to create and assign a persisted environment variable, given the input value.
# Assigns the env var to the value
[System.Environment]::SetEnvironmentVariable('ENVIRONMENT_VARIABLE_KEY', 'value', 'User')
In a new instance of the Windows PowerShell, use the following command to read the environment variable.
# Prints the env var value
[System.Environment]::GetEnvironmentVariable('ENVIRONMENT_VARIABLE_KEY')
Use the following command to create and assign a persisted environment variable, given the input value.
# Assigns the env var to the value
echo export ENVIRONMENT_VARIABLE_KEY="value" >> /etc/environment && source /etc/environment
In a new instance of the Bash, use the following command to read the environment variable.
# Prints the env var value
echo "${ENVIRONMENT_VARIABLE_KEY}"
# Or use printenv:
# printenv 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.
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");
}
}
#include <iostream>
#include <stdlib.h>
std::string GetEnvironmentVariable(const char* name);
int main()
{
// Get the named env var, and assign it to the value variable
auto value = GetEnvironmentVariable("ENVIRONMENT_VARIABLE_KEY");
}
std::string GetEnvironmentVariable(const char* name)
{
#if defined(_MSC_VER)
size_t requiredSize = 0;
(void)getenv_s(&requiredSize, nullptr, 0, name);
if (requiredSize == 0)
{
return "";
}
auto buffer = std::make_unique<char[]>(requiredSize);
(void)getenv_s(&requiredSize, buffer.get(), requiredSize, name);
return buffer.get();
#else
auto value = getenv(name);
return value ? value : "";
#endif
}
import java.lang.*;
public class Program {
public static void main(String[] args) throws Exception {
// Get the named env var, and assign it to the value variable
String value =
System.getenv(
"ENVIRONMENT_VARIABLE_KEY")
}
}
// Get the named env var, and assign it to the value variable
NSString* value =
[[[NSProcessInfo processInfo]environment]objectForKey:@"ENVIRONMENT_VARIABLE_KEY"];