Zur Konsole

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.

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. Keep in mind that only lowercase characters are accepted in keys; uppercase characters are not allowed.

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

Additional Environment Commands

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

Automatically populated environment variables

There are environment variables that are automatically populated in the functions runtime and in locally emulated functions, including:

process.env.GCLOUD_PROJECT: Provides the Firebase project ID

process.env.FIREBASE_CONFIG: Provides the following Firebase project config info:

  databaseURL: 'https://databaseName.firebaseio.com',
  storageBucket: 'projectId.appspot.com',
  projectId: 'projectId'

This configuration is applied automatically when you initialize the Firebase Admin SDK with no arguments. If you are writing functions in JavaScript, initialize like this:

const admin = require('firebase-admin');

If you are writing functions in TypeScript, initialize like this:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import 'firebase-functions';

If you need to initialize the Admin SDK with the default project configuration using service account credentials, you can load the credentials from a file and add them to FIREBASE_CONFIG like this:

serviceAccount = require('./serviceAccount.json');

const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG);
adminConfig.credential = admin.credential.cert(serviceAccount);