Configure Environment Variables
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”- Log into the Optimizely Cloud management portal
- Select your project
- Navigate to Settings > Environment Variables
- Select the target environment (Integration, Preproduction, or Production)
- Click Add Variable
- Enter the variable name and value
- Click Save
Changes take effect on the next deployment or application restart.
Common variables
Section titled “Common variables”Connection strings
Section titled “Connection strings”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;...Application settings
Section titled “Application settings”Override any appsettings.json value using the double-underscore notation that .NET uses for nested configuration:
Optimizely__Search__ServiceUrl = https://search-integration.example.comLogging__LogLevel__Default = DebugMyApp__Features__NewCheckout = trueThird-party API keys
Section titled “Third-party API keys”Store API keys as environment variables rather than in your codebase:
SendGrid__ApiKey = SG.xxxxxxxxxxxxGoogleMaps__ApiKey = AIzaxxxxxxxxxxxxxxxxVariable naming conventions
Section titled “Variable naming conventions”Optimizely Cloud follows .NET configuration conventions:
| Pattern | Example | Maps to |
|---|---|---|
| Flat key | MY_SETTING | Configuration["MY_SETTING"] |
| Nested (double underscore) | Section__Key | Configuration["Section:Key"] |
| Connection string | ConnectionStrings__Name | Configuration.GetConnectionString("Name") |
Important: Use double underscores (__) to represent the colon (:) separator in hierarchical configuration keys. This is a .NET convention that the platform respects.
Read variables in your application
Section titled “Read variables in your application”ASP.NET Core
Section titled “ASP.NET Core”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"]; }}Options pattern
Section titled “Options pattern”For strongly-typed configuration, bind environment variables to options classes:
services.Configure<SendGridOptions>(configuration.GetSection("SendGrid"));Sensitive values and secrets
Section titled “Sensitive values and secrets”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
Configuration precedence
Section titled “Configuration precedence”When the same setting is defined in multiple places, the following precedence applies (highest wins):
- Environment variables (set in portal)
- appsettings.{Environment}.json (in your repository)
- appsettings.json (in your repository)
- Default values (in code)
This means portal-set variables always override file-based configuration, which is the intended behavior for environment-specific overrides.
Verify your configuration
Section titled “Verify your configuration”After setting variables, verify they are applied:
- Deploy or restart the application
- Check application startup logs for configuration warnings
- Use a health check endpoint that reports configuration status
- 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.