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_NAME,
		firebase.config().db.DB_USER,
		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
Dhavalhttps://androidhire.com
Dhaval is a tech geek who loves developing software, games and writing technical posts. In his free time, he loves to read novels.

Latest news

One Plus 8T: All you need to know!

One Plus unveiled its 2020 flagship in a virtual event named One Plus 8T. In this post we would go through its...

Twitter may launch new features to simplify direct messages.

Twitter is said to be working on a couple of new features that could make it easier to manage direct messages...

Zoom Adds External Authentication Feature to make Virtual Classrooms more secure.

Zoom has introduced an external authentication feature to make virtual classrooms more secure. SSO will allow students to log in one...

Facebook, Twitter take action over Trump’s misleading COVID-19 posts.

Facebook and twitter took action on posts from US president Donald Trump on Tuesday for violating their rules against coronavirus misinformation...

Related news

One Plus 8T: All you need to know!

One Plus unveiled its 2020 flagship in a virtual event named One Plus 8T. In this post we would go through its...

Twitter may launch new features to simplify direct messages.

Twitter is said to be working on a couple of new features that could make it easier to manage direct messages...

Zoom Adds External Authentication Feature to make Virtual Classrooms more secure.

Zoom has introduced an external authentication feature to make virtual classrooms more secure. SSO will allow students to log in one...

Facebook, Twitter take action over Trump’s misleading COVID-19 posts.

Facebook and twitter took action on posts from US president Donald Trump on Tuesday for violating their rules against coronavirus misinformation...