Python Firebase
About Lesson

In this Python Firebase lesson we are going to learn about Firebase Admin SDK with Realtime Database for Python and how you can work with Firebase Realtime Database using Firebase Admin SDK, before this we have learned that how you can use Real Time Database with pyrebase library.


With the Admin SDK, you can read and write Realtime Database data with full admin privileges, or with finer-grained limited privileges. also Admin SDK lets you interact with Firebase from privileged environments to perform actions like:

  • Read and write Realtime Database data with full admin privileges.
  • Programmatically send Firebase Cloud Messaging messages using a simple, alternative approach to the Firebase Cloud Messaging server protocols.
  • Generate and verify Firebase auth tokens.
  • Access Google Cloud Platform resources like Cloud Storage buckets and Cloud Firestore databases associated with your Firebase projects.
  • Create your own simplified admin console to do things like look up user data or change a user’s email address for authentication.


You can check Firebase Admin SDK features that are available for different programming languages in this link, Firebase Admin SDK Features.



Setup Firebase Project & Service Account

To use the Firebase Admin SDKs, you’ll need the following:

  • A Firebase project
  • A service account to communicate with Firebase
  • A configuration file with your service account’s credentials



After creation of your project in Firebase Console, you need to enable Realtime Database in your project. after that for integration of Firebase SDK you need to install the SDK for the language of your choice. because we are using python, so we do the process for python programming language. you can easily install Firebase Admin SDK with pip like this.



To authenticate a service account and authorize it to access Firebase services, you must generate a private key file in JSON format.

  1. In the Firebase console, open Settings > Service Accounts.
  2. Click Generate New Private Key, then confirm by clicking Generate Key.
  3. Securely store the JSON file containing the key.



Add that json file to your working directory.

Python Firebase Admin SDK with Realtime Database
Python Firebase Admin SDK with Realtime Database




Admin SDK Integration 

Before you can access the Firebase Realtime Database from a server using the Firebase Admin SDK, you must authenticate your server with Firebase. When you authenticate a server, rather than sign in with a user account’s credentials as you would in a client app.


When you initialize the Firebase Admin SDK with the credentials for a service account with the Editor role on your Firebase project, that instance has complete read and write access to your project’s Realtime Database.




In the above code this is for fetching the service account key json file. and make that you have downloaded the key and added that to your working directory.



Because we are working with Realtime Database, so after enabling Reatime Database you need to add the url of the database.



Now the setup is completed we need to add some data to our realtime database using this code. You can use set() method for adding data to your realtime database.




For updating you need to add this code.



If you want to work with multiple path update, you can use this code.



There is also another way that you can add data, using this way you can create key for your data, in the first way that we have used for adding data, there is no key for our data.




You can retrieve the data easily using this code.




This is the complete code for Python Firebase Admin SDK with Realtime Database.