An environment variable is a named configuration value supplied outside an application’s source code. It lets a process receive secrets, service endpoints, database details, feature flags, and runtime settings without baking them into a script or container image. This separation matters when the same application must be rebuilt, scaled, or replaced while its deployment-specific configuration remains changeable. section 5.2.3
A common failure mode is placing an API key directly in code or client-side JavaScript. A committed key can reach version control or shared copies; a browser-visible database key can be read through developer tools and used to incur costs or damage data. Keep credentials on the server side, where environment variables can be combined with Key Vault and role-based access control. section 2.4.3 section 6.5
Use environment variables at the boundary between code and its operating environment:
- During local development, place an API key in a project .env file, load it with python-dotenv, and retrieve it by name rather than embedding it in Python. section 2.4.3
- In automated tests, provide the OpenAI key as a GitHub Actions secret mapped to OPENAI_API_KEY for the test command. section 2.4.3
- In an Azure web app, configure expected keys, endpoints, Cosmos DB details, and runtime flags in the Environment Variables settings so configuration remains outside the container. section 5.2.3
- In an Azure Function, configure the Cosmos DB account key, URI, database name, and container name before testing the browser-to-database path. section 6.9
Loading an API key locally
Create a .env file containing OPENAI_API_KEY, install python-dotenv, call load_dotenv(), then read the value with os.getenv("OPENAI_API_KEY") or os.environ.get("OPENAI_API_KEY"). The application can then configure its OpenAI client without exposing the key in the script. section 2.4.3
Listing 5.5 Your web app environment variables
[
{
"name": "AI_SEARCH_ENDPOINT",
"value": "<<your ai search endpoint here>>",
"slotSetting": false
},
{
"name": "AI_SEARCH_KEY",
"value": ""<<your ai search key here>>",
"slotSetting": false
},
{
"name": "AI_SEARCH_NAME",
"value": ""<<your ai search index name here>>",
"slotSetting": false
},
{
"name": "AUTOGEN_USE_DOCKER",
"value": "0",
"slotSetting": false
},
{
"name": "COSMOSDB_CONTAINER_NAME",
"value": "<<the name of your cosmosdb container>>",
"slotSetting": false
},
{
"name": "COSMOSDB_DATABASE_NAME",
"value": "<<the name of your cosmosdb database>>",
"slotSetting": false
},
{
"name": "COSMOSDB_KEY",
"value": "<<your cosmosdb database key>>",
"slotSetting": false
},
{
"name": "COSMOSDB_URI",
"value": "<<the URI for your CosmosDB>",
"slotSetting": false
},
{
"name": "OPENAI_API_KEY",
"value": "<<your openai api key>>",
"slotSetting": false
},
{
"name": "test_environment",
"value": "False",
"slotSetting": false
}
]
This Azure configuration template shows how named environment variables carry search, database, and runtime settings independently of the application image. section 5.2.3
Treat environment changes as deployment operations. Azure injects configured values when the container restarts after Apply and Save. For a long configuration list, Advanced edit accepts a JSON template and merges it with existing settings. Keep browser access separate from database credentials: a Function App can hold the Cosmos DB values server-side while CORS and an API key govern incoming requests. section 5.2.3 section 6.9























