Function Environment Variables

Use Function environment variables to represent values your Function needs at runtime that are environment specific. For example, your Function might need to access an external data resource. Authentication information for this resource might need to be configured based on if the Function is invoked from a sandbox org versus a production org. Instead of writing Function code that has to determine what environment the Function is running in, you can configure an environment variable at deploy-time that stores the authentication inforation, and access this variable in your Function code.

Environment variables are securely stored in the Salesforce Functions Compute Cloud, and can be accessed by Functions code as system variables.

Set Environment Variables in Compute Environments

To manage environment variables, use the 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 env:var:set:

Note that you can set multiple environment vars in a single command:

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

If you need to remove an environment variable from a Compute Environment, use the env:var:unset command:

Set Environment Variables for Local Invocation

If you're testing your Function locally, use the --env / -e argument to run:function:start to set environment variables that your locally running Function can access. For example:

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:

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