Automate Environment Variable Management with the CLI or API

Instead of using Runtime Admin to manage environment variables from the UI, automate the environment variable administration with the B2C CLI or the Managed Runtime API.

Also, access environment variables in Node.js by using process.env.

Note

Manage Environment Variables with the B2C CLI 

Use the B2C CLI to list all environment variables for an environment, update an environment variable value, and create environment variables from an environment file.

The B2C CLI examples use placeholder values for the project and the environment formatted as <placeholder-value>. Replace placeholders with the actual values for your storefront. To get the project ID, see Find a Project.

Note

Set Up the B2C CLI 

For installation instructions, see B2C CLI, MCP and Tooling SDK to install the tool and authenticate.

List All Environment Variables for an Environment 

Use this command to list all environment variables for a specific project and environment.

1b2c mrt env var list --project <project-id> --environment <environment-id>

Alternative command:

1b2c mrt env var list --p <project-id> --e <environment-id>

Set or Update an Environment Variable 

1b2c mrt env var set "MY_VAR=hello world" -p <project-id> --e <environment-id>

To unset an existing environment variable, set its value to null.

Modifying environment variables redeploys the environment. The updated values are available when the deploy completes.

Create Environment Variables from an .env File 

The --yes argument causes the suppression of the confirmation prompt.

1b2c mrt env var push -p <project-id> -e <environment-id> --file <file-path>/.env --yes

When you add a new environment variable, wait for the redeploy of the bundle to complete, and then deploy a new bundle that uses the new variable.

Note

For a complete list of MRT commands, see MRT Commands in the B2C Developer Toolkit.

Manage Environment Variables with the Managed Runtime API 

Use the Managed Runtime API to list all environment variables for an environment and update an environment variable value.

Set up the Managed Runtime API 

To make API requests, you must include an API key in the HTTP request Authorization header with the value, Bearer $API_KEY. To find your API key, log in to the Runtime Admin tool and go to the Account Settings page.

Treat your API key like a password because it allows scripts to perform operations on your behalf.

Note

This tutorial uses sample curl requests. Before running the commands, replace any placeholders with actual values. Placeholders are formatted as $PLACEHOLDER. Replace $PROJECT_ID with your actual project ID. To get the project ID, see Find a Project.

Get All Environment Variables 

Use projects_target_env_var_list:

1curl "https://cloud.mobify.com/api/projects/$PROJECT/target/$ENVIRONMENT/env-var/" \
2  --header "Authorization: Bearer $MOBIFY_API_KEY" \
3  --header 'Accept: application/json'

Set, Update, and Unset Environment Variables 

Use projects_target_env_var_partial_update:

1curl "https://cloud.mobify.com/api/projects/$PROJECT/target/$ENVIRONMENT/env-var/" \
2  --request 'PATCH' \
3  --header 'Content-Type: application/json' \
4  --header 'Accept: application/json' \
5  --header "Authorization: Bearer $MOBIFY_API_KEY" \
6  --data '{
7    "API_KEY": {"value": "b3Blbi1zZXNhbWU="},
8    "STOREFRONT_CONFIG": {"value": "{\"showBuyNowButton\": false}"},
9    "SLAS_CLIENT_ID": {"value": "da422690-7800-41d1-8ee4-3ce983961078"}
10}'

To unset an existing environment variable, set its value to null.

Modifying environment variables redeploys the environment. The updated values are available when the deploy completes.

See Also