Skip to content

Configure Environment Variables

⏱ 15 minutes intermediate
📜Corecms

Why environment-specific configuration matters

Section titled “Why environment-specific configuration matters”

Your application needs different settings in each environment. Integration might use a test payment gateway, Preproduction might connect to a staging search index, and Production uses live credentials. Hardcoding any of these values into your repository is both a security risk and a deployment headache. Environment variables solve this by letting each environment inject its own values at runtime.


Set variables through the management portal

Section titled “Set variables through the management portal”
  1. Log into the Optimizely Cloud management portal
  2. Select your project
  3. Navigate to Settings > Environment Variables
  4. Select the target environment (Integration, Preproduction, or Production)
  5. Click Add Variable
  6. Enter the variable name and value
  7. Click Save

Changes take effect on the next deployment or application restart.


Connection strings are automatically provisioned for your CMS database. You can add custom connection strings for additional data sources.

ConnectionStrings__CustomDb = Server=myserver.database.windows.net;Database=mydb;...

Override any appsettings.json value using the double-underscore notation that .NET uses for nested configuration:

Optimizely__Search__ServiceUrl = https://search-integration.example.com
Logging__LogLevel__Default = Debug
MyApp__Features__NewCheckout = true

Store API keys as environment variables rather than in your codebase:

SendGrid__ApiKey = SG.xxxxxxxxxxxx
GoogleMaps__ApiKey = AIzaxxxxxxxxxxxxxxxx

Optimizely Cloud follows .NET configuration conventions:

PatternExampleMaps to
Flat keyMY_SETTINGConfiguration["MY_SETTING"]
Nested (double underscore)Section__KeyConfiguration["Section:Key"]
Connection stringConnectionStrings__NameConfiguration.GetConnectionString("Name")

Important: Use double underscores (__) to represent the colon (:) separator in hierarchical configuration keys. This is a .NET convention that the platform respects.


Environment variables are automatically available through the standard configuration system:

public class MyService
{
private readonly string _apiKey;
public MyService(IConfiguration configuration)
{
_apiKey = configuration["SendGrid:ApiKey"];
}
}

For strongly-typed configuration, bind environment variables to options classes:

services.Configure<SendGridOptions>(configuration.GetSection("SendGrid"));

Environment variables are stored encrypted at rest in the Optimizely Cloud platform. However, follow these security practices:

  • Never commit secrets to your Git repository
  • Use different values per environment — do not share Production API keys with Integration
  • Rotate regularly — Update keys periodically and redeploy
  • Limit access — Only grant portal access to team members who need it

When the same setting is defined in multiple places, the following precedence applies (highest wins):

  1. Environment variables (set in portal)
  2. appsettings.{Environment}.json (in your repository)
  3. appsettings.json (in your repository)
  4. Default values (in code)

This means portal-set variables always override file-based configuration, which is the intended behavior for environment-specific overrides.


After setting variables, verify they are applied:

  1. Deploy or restart the application
  2. Check application startup logs for configuration warnings
  3. Use a health check endpoint that reports configuration status
  4. Test the specific feature that depends on the variable

For debugging, temporarily set Logging__LogLevel__Default to Debug in the Integration environment and check the logs for configuration binding output.