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:

1sf env var set GITHUB_USERNAME=stevesmith -e MyComputeEnv

Set multiple environment variables in a single command:

1sf 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:

1sf env var get GITHUB_USERNAME -e MyComputeEnv
2stevesmith
3
4$ sf env var list -e MyComputeEnv
5Key             Value
6GITHUB_USERNAME stevesmith
7ANOTHER_VAR     anothervalue

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

1sf 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.

1sf 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:

1let userName = process.env.GITHUB_USERNAME;

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

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

Product Retirement Announcement

Salesforce Functions is no longer available for purchase or renewal. To preserve the capabilities that Salesforce Functions provided to your org, deploy an alternative solution before your existing order term ends. See Salesforce Functions Retirement for more information on migrating your functions. Contact your Salesforce Account Executive for more information on Heroku.