How To
Environment Variables

Environment Variables

You can store environment variables in the Reflect server with the npx reflect env command. These variables are then accessible at runtime in your server-side mutators and auth handler.

Setting Environment Variables

npx reflect env set foo=bar baz=hotdog

By default reflect env set modifies the variables for the production environment that runs on reflect-server.net.

To set variables for the local "development" environment add the --dev flag:

npx reflect env set --dev foo=monkey baz=cantaloupe

Accessing Environment Variables at Runtime

Inside a mutator, you can access environment variables with the env field of WriteTransaction:

async function myMutator(tx: WriteTransaction) {
  console.log(tx.env?.foo);
}

The env field will be defined if the mutator is running on the server, and undefined on the client.

To access environment variables in your auth handler, use the env parameter.

function myAuthHandler(
  auth: string,
  roomID: string,
  env: Record<string, string>,
) {
  console.log(env.foo);
}

Secrets

All production environment variables are encrypted at rest and suitable for storing sensitive data like passwords and API keys. To avoid exposing secrets on the command line, omit the =value when setting a variable:

npx reflect env set db_password

This will prompt you to enter a masked input.

Note that local development variables set by --dev are stored locally and not encrypted.