How to save and Access Environment Variables in Firebase

This is a small tutorial in which you would learn how to save and access environment variables in Firebase in Android Studio.

Now before starting this tutorial i would let you readers know about Firebase. What it is? and how it works? Remember that you need to set up firebase sdk setup before you can work upon Firebase.

What is Google Firebase?

Firebase is a mobile and web application development platform developed by Firebase, Inc. in 2011, then acquired by Google in 2014. As of March 2020, the Firebase platform has 19 products, which are used by more than 1.5 million apps. You can use this in implmentation of firebase cloud messaging too.

How to save and Access Environment Variables in Firebase 1
Save and access environment variables in firebase

What are Environment variables?

Environment variables in the cloud functions allows you to set/get key values at the time of deployment which can later be used in the code through environment variables. Firebase cloud functions also allows to specify key value pairs as config environment variables.

Environment variables can be saved by setting config set command through firebase CLI. Once  Firebase project is setup you can call Firebase CLI config set function to store key value pairs.

How to save environment variables in Firebase?

Suppose you want to save database configuration in environemnt variables then it can be set by calling functions:config:set command.

firebase functions:config:set db.DB_USER="username" db.DB_PASSWORD="password" db.DB_HOST="host_url" db.DB_NAME="database_name"

How to access environment variables in Firebase?

Now after we have deployed config variables its time to see how we can use them in Firebase cloud code.

Config variables can be accessed via firebase.config() . Example of setting our database connection to external MySql database is shown in the below snippet.

const Sequelize = require('sequelize');
 const sequelize = new Sequelize(
		firebase.config().db.DB_PASSWORD, {
			dialect: 'mysql',
			host: firebase.config().db.DB_HOST
		}, {
			pool: {
				max: 5,
				min: 0,
				idle: 20000,
				acquire: 20000

If you want to learn via video, we found one of the good tutorial on this from Youtube.



Dhaval is a tech geek who loves developing software, games and writing technical posts. In his free time, he loves to read novels.

We Recommend