Environment Configuration

Often you'll need additional configuration for your functions, such as third-party API keys or tuneable settings. The Firebase SDK for Cloud Functions offers built-in environment configuration to make it easy to store and retrieve this type of data for your project without having to redeploy your functions.

Set environment configuration for your project

To store environment data, you can use the firebase functions:config:set command in the Firebase CLI. Each key can be namespaced using periods to group related configuration together. For instance, to store the Client ID and API key for "Some Service", you might run:

firebase functions:config:set someservice.key="THE API KEY" someservice.id="THE CLIENT ID"

Retrieve current environment configuration

To inspect what's currently stored in environment config for your project, you can use firebase functions:config:get. It will output JSON something like this:

{
  "someservice": {
    "key":"THE API KEY",
    "id":"THE CLIENT ID"
  }
}

This functionality is based on the Google Cloud Runtime Configuration API.

Access environment configuration in a function

Some configuration is automatically provided under the reserved firebase namespace. Environment configuration is made available inside your running function via functions.config(). To use the configuration above, your code might look like this:

const functions = require('firebase-functions');
const request = require('request-promise');

exports.userCreated = functions.database.ref('/users/{id}').onWrite(event => {
  let email = event.data.child('email').val();

  return request({
    url: 'https://someservice.com/api/some/call',
    headers: {
      'X-Client-ID': functions.config().someservice.id,
      'Authorization': `Bearer ${functions.config().someservice.key}`
    },
    body: {email: email}
  });
});

Use environment configuration to initialize a module

Some Node modules are ready without any configuration. Other modules need extra configuration to initialize correctly. We recommend you store this configuration in environment configuration variables rather than hard-coding it. This helps you keep your code much more portable, which lets you open source your application or easily switch between production and staging versions.

For example, to use the Slack Node SDK module, you might write this:

const functions = require('firebase-functions');
const IncomingWebhook = require('@slack/client').IncomingWebhook;
const webhook = new IncomingWebhook(functions.config().slack.url);

Prior to deploying, set the slack.url environment config variable:

firebase functions:config:set slack.url=https://hooks.slack.com/services/XXX

When you deploy functions using the Firebase CLI, functions.config().firebase is auto-populated with configuration needed to initialize the firebase-admin SDK.

So you can put this in your code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

Additional Environment Commands

  • functions:config:unset [key1] [key2...] removes the specified keys from the config
  • functions:config:clone --from <fromProject> clones another project's environment into the currently active project.

Send feedback about...

Need help? Visit our support page.