Setting Config for Firebase Cloud Functions with JSON
Setting the environment configuration for Firebase Cloud Functions is a little awkward. Here’s the example from the official doc:
$ firebase functions:config:set someservice.key="THE API KEY" someservice.id="THE CLIENT ID"
Basically you need to write a command passing all the keys and values using a [key]=[value]
pair. However, when we get these values they come beautifully formatted as a JSON object.
$ firebase functions:config:get
{
"someservice": {
"key": "THE API KEY",
"id": "THE CLIENT ID"
}
}
hmmmm… Why not set them as JSON too? Well, turns out it’s not that complicated! 🙆
The plan
The plan is quite simple:
- Parse the JSON and transform it into a list of
[key]=[value]
pairs - Build the “firebase config set” command
- Execute the command
The script
All the steps above are done in a single Node script. Since Cloud Functions runs on Node, I’m assuming everyone using it to have it already installed.
Below is the script named parser.js
. Hopefully the code is self-explanatory.
Putting the plan in practice
First we need a JSON file (named env-vars.json
) with our environment configuration:
{
"android": {
"app_package_name": "best.app.<3"
},
"play_credentials": {
"client_email": "ultra@secret.email",
"private_key": "pkey"
},
"cant_have_root": {
"root_var": 123
}
}
Then, we pipe its content to our script:
$ cat env-vars.json | node parser.js
# Ps.: The "pipe" works in Linux. Not test in others platforms.
And that’s it! 🤙
Conclusions
This is a simple solution, but a much better solution to entering all the details manually. The JSON configuration can be stored in a version control. Also, it’s possible to get the current environment configuration as JSON, modify it, and then upload again.
The current behavior is to only set the keys of the JSON file. This means it will not delete keys missing in the JSON file. But a more complete solution would be nice 😉