Environment variables

Storing and using environment variables in an application allows for certain variables to be maintained outside of the code of the application. Here is how to use environment variables from within Slack functions, triggers, and manifests.

Using environment variables with a custom function

When accessing environment variables from within a custom function, where you store them differs when the app is local versus deployed.

Storing local environment variables

Local environment variables are stored in a .env file at the root of the project and made available for use in custom functions via the env context property.

A local .env file might look like this:


MY_ENV_VAR=asdf1234

Note that changes to your .env file will be reflected when you restart your local development server.

While the .env file should never be committed to source control for security reasons, you can see a sample .env file we've included in the Timesheet approval sample app and the Incident management sample app.

Storing deployed environment variables

When your app is deployed, it will no longer use the .env file. Instead, you will have to add the environment variables using the env add command. Environment variables added with env add will be made available to your deployed app's custom functions just as they are locally; see examples in the next section.

For the above example, we could run the following before deploying our app:


slack env add MY_ENV_VAR asdf1234

If your token contains non-alphanumeric characters, wrap it in quotes like this:


slack env add MY_ENV_VAR "asdf-1234"

Your environment variables are always encrypted before being stored on our servers and will be automatically decrypted when you use them—including when listing environment variables with slack env list.

Access variables from within function

We can retrieve the MY_ENV_VAR environment variable from within a custom Slack function via the env context property like this:


// functions/my_function.ts

import { DefineFunction, SlackFunction } from "deno-slack-sdk/mod.ts";
import { MyFunctionDefinition } from "functions/myfunction.ts"


export default SlackFunction(MyFunctionDefinition, ({ env }) => {
  const myEnvVar = env["MY_ENV_VAR"];
  // ...
  return { outputs: {} };
});

Environment variables also play an important part in making calls to a third-party API. Learn more about how to do that in the FAQ.

Using environment variables with a trigger or manifest

Accessing environment variables from within a trigger or manifest file differs slightly.

Storing environment variables

Regardless of the deployed status of the app, environment variables that are to be accessed from a trigger or manifest file should be saved in the local .env file, as shown above. Read on to learn how to access those stored variables.

Access variables from a trigger or manifest

From within a trigger or manifest, access stored environment variables with Deno.env.get("MY_VAR") like this:

...

export const envMap: EnvMap = {
  beagoodhost: {
    channel_id: Deno.env.get("BEAGOODHOST_CHANNEL_ID"),
  },
  mysandbox: {
    channel_id: Deno.env.get("MYSANDBOX_CHANNEL_ID"),
  },
  default: {
    channel_id: "{{data.channel_id}}",
  },
};

Included variables

Slack provides two environment variables baked into the environment: SLACK_WORKSPACE and SLACK_ENV. These variables provide distinction between a local (local) and deployed (prod) app. Use these values if you need different values based on the workspace or environment that the app is installed into.

Using the EnvMap as shown here is an alternative to storing the variables in the local env file, however, this does not account for SLACK_ENV and is not secure. Storing values in your local .env file is more secure, but these values are not remotely hosted or encrypted.

export const envMap: EnvMap = {
  prod: {
    beagoodhost: {
      channel_id: "C013BUS924T",
    },
    sandbox: {
      channel_id: "C03T9V0BRBP",
    },
  },
  local: {
    beagoodhost: {
      channel_id: "C013BUS132T",
    },
    sandbox: {
      channel_id: "C03T9V0CDAP",
    },
  },
  default: {
    channel_id: "{{data.channel_id}}",
  },
};

const deployState = Deno.env.get("SLACK_URL");
const workspace = Deno.env.get("SLACK_WORKSPACE");
const { channel_id } = envMap?.[deployState]?.[workspace] ?? envMap.default;