DEV Community

Rajesh Kumaravel
Rajesh Kumaravel

Posted on

Google Firebase Functions : Setting and accessing environment variable

It's always a good idea to externalize application keys/secrets from code.
Google Firebase Functions has a feature that allows you to specify environment variables of key/value pairs using Firebase CLI and can be accessed by your code at runtime.

Set env variable

firebase functions:config:set config.key="SECRET_KEY" config.pass="SECRET_PASS"
Enter fullscreen mode Exit fullscreen mode

NOTE: You must redeploy functions to make the new configuration available.

Deploy functions

firebase deploy --only functions
Enter fullscreen mode Exit fullscreen mode

Accessing env variables

const secretKey  = firebase.config().config.key;
const secretPass = firebase.config().config.pass;
Enter fullscreen mode Exit fullscreen mode

Retrieve all variables

firebase functions:config:get
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "config": {
    "key": "SECRET_KEY",
    "pass": "SECRET_PASS"
  }
}
Enter fullscreen mode Exit fullscreen mode

Unset a variable

firebase functions:config:unset config.key
Enter fullscreen mode Exit fullscreen mode

Let's consider using a file where we can keep all the environment variables.

env.json, a file that will contain all our environment variables.

{
  "config": {
    "host": "domain",
    "key": "SECRET_KEY",
    "pass": "SECRET_PASS"
  }
}
Enter fullscreen mode Exit fullscreen mode

How to deploy the variables with the env.json file?

firebase functions:config:set env="$(cat env.json)"
Enter fullscreen mode Exit fullscreen mode

Great! Now we can make a bulk update of our variables and keep track of them.

Top comments (8)

Collapse
 
jamm3e3333 profile image
Jakub Vala • Edited

thank you a lot ๐Ÿ™‡ ๐Ÿ’™ I was desperate of accessing the process.env vars during the deploy, finding out it's not really working and this has saved me a lot of time. Very much appreciated

Collapse
 
p19ky profile image
Pinkovai Krisztian • Edited

Just a heads up for people in 2023 using functions.config(). It is not the recommended way anymore as it errors on firebase deploy with the new generation of functions SDK.

See: firebase.google.com/docs/functions...

Caution: Environment configuration with functions.config() is supported only in the first-generation Cloud Functions SDK. Using functions.config() with the 2nd-generation SDK, currently in public preview, fails at deployment.

Also helpful: firebase.google.com/docs/functions...

Collapse
 
thewizardofwikacy profile image
The Wizard of Wikacy

I wish someone would make a bot to flag every outdated and misleading post and tutorial, such as this one, posted spuriously for the purpose of attention-whoring.

Collapse
 
lasharela profile image
Lasha Kvantaliani

good catch but process.env.VAR is not working as well.
Probably I'm missing something. there is no documentation as well.

Collapse
 
mjoycemilburn profile image
MartinJ

Nice post Rajesh. But I think

const secretKey = firebase.config().config.key;
const secretPass = firebase.config().config.pass;

should reference functions.config()r rather than firebase.config()

Collapse
 
thewizardofwikacy profile image
The Wizard of Wikacy

Unfortunately this teaches absolutely nothing. You have not explained the convention behind the naming of the keys and values, or otherwise explained where to include the actual value of the variable.

Collapse
 
yeshwants profile image
yeshwants

*I want to set the git commit sha and attach it to a cloud function during deployment.
*

How to set a variable for a particular function. For example if we use the GCP UI in cloud functions we can edit the source code and also add a Runtime varible to that function which is applible only to that cloud function even if we have 100s of other CFs.
But from firebase deploy command we dont have anything like
firebase deploy --only functions:function1 commit_id=sha

Please let me know if there is any other way I can do this .

Collapse
 
trejocode profile image
Sergio A. Trejo

Thank you <3