0
0
FirebaseHow-ToBeginner · 3 min read

How to Set Environment Config Function in Firebase

Use firebase functions:config:set to define environment variables for Firebase Functions. Access these variables in your code with functions.config() to keep sensitive data and settings outside your codebase.
📐

Syntax

The command to set environment config variables is firebase functions:config:set. You specify key-value pairs to store settings. In your Firebase Functions code, use functions.config() to access these values.

Example parts:

  • firebase functions:config:set key1.value1="your_value" key2.value2="another_value" - sets config variables
  • functions.config().key1.value1 - accesses the variable in code
bash
firebase functions:config:set someservice.key="THE_API_KEY" someservice.id="THE_CLIENT_ID"
💻

Example

This example shows how to set environment config variables and use them in a Firebase Cloud Function.

javascript
/* Step 1: Set config variables in terminal */
// Run this command in your terminal:
// firebase functions:config:set weather.key="12345apikey" weather.unit="celsius"

/* Step 2: Access config in your Firebase Function code */
const functions = require('firebase-functions');

exports.getWeather = functions.https.onRequest((request, response) => {
  const apiKey = functions.config().weather.key;
  const unit = functions.config().weather.unit;
  response.send(`API Key: ${apiKey}, Unit: ${unit}`);
});
Output
API Key: 12345apikey, Unit: celsius
⚠️

Common Pitfalls

Common mistakes include:

  • Not deploying functions after setting config variables. You must run firebase deploy --only functions to apply changes.
  • Trying to access config variables before setting them, which returns undefined.
  • Using process.env instead of functions.config() for Firebase Functions environment variables.
javascript
/* Wrong way: Accessing config without setting it first */
const functions = require('firebase-functions');
const apiKey = functions.config().weather.key; // undefined if not set

/* Right way: Set config then deploy functions */
// firebase functions:config:set weather.key="your_key"
// firebase deploy --only functions
const apiKey = functions.config().weather.key; // now defined
📊

Quick Reference

Command / CodeDescription
firebase functions:config:set key.value="your_value"Set environment config variables
functions.config().key.valueAccess config variables in Firebase Functions code
firebase deploy --only functionsDeploy functions to apply config changes
firebase functions:config:getView current config variables
firebase functions:config:unset keyRemove a config variable

Key Takeaways

Use 'firebase functions:config:set' to define environment variables for Firebase Functions.
Access these variables in your code with 'functions.config()' to keep secrets safe.
Always deploy your functions after setting or changing config variables.
Do not use 'process.env' for Firebase Functions environment config.
Use 'firebase functions:config:get' to check your current config settings.