How to Integrate Firebase Into Your App

William HillWilliam is a Software Engineer at Lawrence Livermore Lab by day and a Senior Instructor for Hackbright’s Intro to Programming course by night. He developed a passion for teaching while earning his Bachelor’s and Master’s degrees in Computer Science from Mississippi State University.  He has a drive for increasing diversity in tech and has volunteered with Black Girls Code, the Hidden Genius Project and is a member of /dev/color. When he isn’t churning out code, he enjoys playing basketball, strength training, and playing video games. Follow him on twitter at @emjay_hill.

Looking for an easy way to add a database to your Python app? We’ll show you how to get started with Firebase. Creating an app isn’t easy, but learning how to store your app’s vital data is! Here is a step-by-step tutorial to help you figure out how to connect to a database with Firebase and take the headache out of data storage and retrieval. A Python app without Firebase is like a chicken without a hen-house; if you don’t have a place to store your eggs, it’s time to re-think the on fire

You have created this awesome new app that you want to share with the world.  Your code has elegantly structured Classes, efficient Loops, and well commented Functions. You fire up your app and start creating Objects.  Life is good.


But then you shut down your interpreter and all of your newly created data is gone.  


Surely there has to be some simple way to save data associated with you app.  Someone must have thought of a better way than just throwing data in text files, right?  Right?!?  Well you’re in luck, because that’s exactly what databases are for.

A database is a collection of data that can be structured to describe attributes and relationships between data entities of a program.  The ability to save data associated with your app makes your app…useful.

This tutorial will show you how to connect to a database hosted by Firebase to store and retrieve data.

Creating a Firebase Account

Creating a Firebase account is simple.  You can register using an existing Google account at

Installing pip

Before we can connect to our Firebase database with Python, we need to download a couple of helper modules.  We are going to use pip to install them.

Pip is a package manager (i.e. a software application that helps you download and update software packages) that can install and manage Python packages obtained from third party repositories (i.e. sites where anyone can write and share code for other people to use). Many python distributions have pip pre-installed.

You can check to see if you currently have pip install by running the command in the terminal:

pip --version

If it is not currently installed, you can install pip by running following command:

curl | python

Installing Pyrebase

Pyrebase is a Python interface to Firebase’s REST API. In layman’s terms, it allows you to use Python to manipulate your Firebase database. The documentation for Pyrebase can be found at

We will install the Pyrebase and its dependencies using pip

sudo pip install Pyrebase

Aside: sudo is a terminal command that temporarily grants the current user (aka you) root access to the file system.  It will typically ask you to enter a password, which will normally be the same password to use to login to your computer. Don’t freak out if start entering the password and don’t see any characters showing up in the terminal.  Your password is still being entered (though its not the best UX design by any stretch).

Connecting to your database

Now we can import the Pyrebase module into a file and make a connection to our Firebase database

import pyrebase
config = {
  "apiKey": "apiKey",
  "authDomain": "",
  "databaseURL": "",
  "storageBucket": "",
  "serviceAccount": "path/to/serviceAccountCredentials.json"

firebase = pyrebase.initialize_app(config)

We define a dictionary named config with several key-value pairs that configure the connection to the database.  The apiKey, authDomain, databaseUrl, and storageBucket values needed to connect to your database can be found at by clicking the Web Setup link in the Auth tab of the Firebase console.  The value for the serviceAccount is actually a JSON file that can be generated by Firebase.

You’ll need to create a service account by going to and clicking the Create Service Account button in the Service Account tab.  Be sure to check the Furnish a new private key option.  The JSON file will then be downloaded and you can reference its file path in the config dictionary.


The default permissions of a Firebase database requires authentication to perform read or write actions.  The simplest way to authenticate is by signing in with an email and password.  A new user can be created in the Auth tab of the Firebase console.  Once a user is created, the following Pyrebase code can be used to created an authentication connection:

auth = firebase.auth()
#authenticate a user
user = auth.sign_in_with_email_and_password("", "mySuperStrongPassword")

The user variable now holds a reference to an authenticated user.  The user’s token that is needed to perform certain actions can be referenced like so:


The user token will need to be passed as a parameter to the get(), push(), set(), update() and remove() functions described below in order to authorize the actions.

Now that we are connected to the database and properly authenticated, let’s perform some actions.

CRUD-dy Behavior

The main functionalities of a database interface are the abilities to create, read, update, and delete data.  The aforementioned abilities are commonly referred to as the CRUD functions. The Pyrebase module provides a simple interface for performing each of the CRUD functions.


There are two methods available for creating a new data entity in your database, push() and set().  These methods can be used in conjunction with the child() method, which builds paths to objects in the database.

The push() method creates a unique, time-stamped based alphanumeric key for the data that’s being added to your database.  That’s good in some cases, but not great when you want to refer to that key.

archer = {"name": "Sterling Archer", "agency": "Figgis Agency"}
db.child("agents").push(archer, user['idToken'])

The set method on the other hand provides a much nicer way to create keys that can be retrieved.

lana = {"name": "Lana Kane", "agency": "Figgis Agency"}
db.child("agents").child("Lana").set(lana, user['idToken'])


Data can be read from your database using the get() method.  The get() method returns a PyreResponse object from the database.  Using the val() method on the PyreResponse object will return the actual data.

You can get all of the values of an object

all_agents = db.child("agents").get(user['idToken']).val()

Or you can get a specific value of an object

lana_data = db.child("agents").child("Lana").get(user['idToken']).val()


Updates can be made with the update() method.

db.child("agents").child("Lana").update({"name": "Lana Anthony Kane"}, user['idToken'])


Lastly, we can delete an object from our database.  The remove() method removes objects from the database.


Or a specific value from an object


You should now be able to set up a basic database for your Python apps. The example code can be found here:


Until next time, Happy Hacking!