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}
  });
});

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

Environment variables

Some Node modules, like node-fetch, are ready without any configuration. Some modules, like gcloud-node, connect to a backend and may need extra configuration to work correctly. We recommend you store this configuration in environment variables. This helps you keep your code much more portable, which lets you open source your application or easily switch between production and staging versions.

Send feedback about...

Need help? Visit our support page.