Use environment variables in configuration files

At a glance
Goal: Keep secrets and environment-specific values out of Dev Proxy configuration files
Time: 5 minutes
Prerequisites: Set up Dev Proxy

Instead of hardcoding values like subscription IDs, tenant names, or API keys in your Dev Proxy configuration, you can reference environment variables. That way you can commit your configuration to source control and supply environment-specific values on each machine or in your CI/CD pipeline.

Reference an environment variable

To reference an environment variable, use the ${VAR_NAME} placeholder in a string value. Dev Proxy replaces the placeholder with the value of the environment variable when it loads the file.

File: devproxyrc.json
Purpose: Read Microsoft Entra app settings from environment variables

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "AuthPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "auth"
    }
  ],
  "urlsToWatch": [
    "https://api.contoso.com/*"
  ],
  "auth": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/authplugin.schema.json",
    "type": "oauth2",
    "oauth2": {
      "metadataUrl": "https://login.microsoftonline.com/organizations/v2.0/.well-known/openid-configuration",
      "allowedAudiences": [
        "${ENTRA_APP_CLIENT_ID}"
      ],
      "issuer": "https://login.microsoftonline.com/${ENTRA_APP_TENANT_ID}/v2.0"
    }
  }
}

A placeholder can make up the whole value or a part of it, and a value can contain multiple placeholders.

Dev Proxy also supports the ${env:VAR_NAME} syntax. Use it for variables whose names contain characters other than letters, digits, and underscores, for example ${env:ProgramFiles(x86)}. The ${VAR_NAME} syntax only supports names that start with a letter or an underscore, followed by letters, digits, or underscores.

Where you can use placeholders

You can use environment variable placeholders in:

Dev Proxy resolves placeholders only in string values. It doesn't change property names. For settings that expect a number or a boolean, like port, you can use a string placeholder in the Dev Proxy configuration file, for example "port": "${DEV_PROXY_PORT}". In plugin data files, values must have the type that the plugin expects, so use placeholders only in values that are strings.

Dev Proxy doesn't resolve placeholders in .http mocks files, files referenced from mock response bodies using @filename, and prompt files.

How Dev Proxy resolves variables

In the Dev Proxy configuration file, Dev Proxy looks up variables in the following order:

  1. Values passed using the --env option.
  2. Values defined in the env section of the configuration file.
  3. Environment variables of the operating system.

For example, you can define default values in the configuration file and override them when starting Dev Proxy:

{
  "env": {
    "API_HOST": "api.contoso.com"
  },
  "urlsToWatch": [
    "https://${API_HOST}/*"
  ]
}
devproxy --env API_HOST=staging.api.contoso.com

Values in the env section can reference operating system environment variables and values passed using --env, but not other values from the env section.

In plugin data files, Dev Proxy resolves placeholders using the environment variables of the Dev Proxy process. Because Dev Proxy sets the variables from the env section and the --env option on its process before loading plugins, plugin data files can use these values too.

Dev Proxy resolves placeholders when it loads a file, including when it reloads a file after you change it. Placeholders are different from tokens like @dynamic or @request.body.* in mock responses, which Dev Proxy resolves for each request.

Variables that aren't set

If a placeholder references an environment variable that isn't set, Dev Proxy leaves the placeholder as-is and logs a warning, for example:

Environment variable ENTRA_APP_TENANT_ID referenced in /Users/user/devproxyrc.json isn't set. Leaving the placeholder as-is

Escape a placeholder

To use the literal text ${VAR_NAME} in a value, prefix it with an extra $. For example, Dev Proxy turns $${VAR_NAME} into ${VAR_NAME} without resolving the variable.

Rewrite rules

RewritePlugin uses ${name} to reference named capture groups in rewrite rules, so Dev Proxy doesn't resolve placeholders in rewrites files and in rewrite rules defined in the plugin's configuration section when loading them. To use environment variables in rewrite rules, use the ${env:VAR_NAME} syntax, which RewritePlugin resolves for each request.

Deprecated syntax

Earlier versions of Dev Proxy used plugin-specific syntax to reference environment variables. Dev Proxy still supports this syntax, but logs a deprecation warning when you use it:

Next step