Manage Function Environment Variables

Use environment variables to represent values your function needs at runtime, such as securely storing credentials or configuration details. Environment variables are securely stored in the Salesforce Functions Compute Cloud, and can be accessed by your function code as system variables.

Set Environment Variables in Compute Environments

To manage environment variables, use the sf env var CLI commands. These commands require specifying a Compute Environment where the environment variables are set or unset.

To set an environment variable in a compute environment, use sf env var set:

sf env var set GITHUB_USERNAME=stevesmith -e MyComputeEnv

Set multiple environment variables in a single command:

sf env var set USER1=joe USER2=steve -e MyComputeEnv

Use sf env var get or sf env var list to inspect what environment variables are set to in a given compute environment:

sf env var get GITHUB_USERNAME -e MyComputeEnv
stevesmith

$ sf env var list -e MyComputeEnv
Key             Value
GITHUB_USERNAME stevesmith
ANOTHER_VAR     anothervalue

To remove an environment variable from a compute environment, use the sf env var unset command:

sf env var unset GITHUB_USERNAME -e MyComputeEnv

Set Environment Variables for Local Invocation

To test your function locally, use sf run function start with local environment variables your function can access.

sf run function start GITHUB_USERNAME

Access Environment Variables in Your Function Code

Environment variables appear as standard system variables when your function runs. Use standard system variable APIs provided by your programming language to access environment variables.

For example, to access the GITHUB_USERNAME environment variable in a JavaScript function, use the process.env:

let userName = process.env.GITHUB_USERNAME;

To access GITHUB_USERNAME in a Java Function, use System.getenv():

String userName = System.getenv("GITHUB_USERNAME");