In this Python Firebase article iam going to show you Firebase SDK Integration
with Realtime Database. 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 the complete video tutorial for this article at the end.
More Python Firebase Tutorials
1: Python Firebase Admin SDK Integration & Realtime Database
2: Python Firebase Full Course For Beginners
If you are interested in Python Firebase Admin SDK, read the below articles
1: Firebase SDK Integration with Real Time Database
2: Firebase SDK working with Cloud Firestore
3: Python Firebase SDK Working with 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
OK so 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.
1 |
pip install firebase-admin |
To authenticate a service account and authorize it to access Firebase services, you must
generate a private key file in JSON format.
- In the Firebase console, open Settings > Service Accounts.
- Click Generate New Private Key, then confirm by clicking Generate Key.
- Securely store the JSON file containing the key.
and add that json file to your working directory.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('firebase-sdk.json') # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, { 'databaseURL': 'https://testpro-72243.firebaseio.com/' }) |
So 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.
1 |
cred = credentials.Certificate('firebase-sdk.json') |
Because we are working with Realtime Database, so after enabling Reatime Database you
need to add the url of the database.
1 2 3 |
firebase_admin.initialize_app(cred, { 'databaseURL': 'https://url of the realtime database/' }) |
Now the setup is completed we need to add some data to our realtime database using this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
ref = db.reference('/') ref.set({ 'Employee': { 'emp1': { 'name': 'Parwiz', 'lname': "Forogh", 'age': 24, }, 'emp2': { 'name': 'John', 'lname': 'Doe', 'age': 20, } } }) |
You can use set for adding data to your realtime database.
For updating you need to add this code.
1 2 3 4 5 |
ref = db.reference('Employee') box_ref = ref.child('emp1') box_ref.update({ 'name': 'updated name' }) |
If you want to work with multiple path update, you can use this code.
1 2 3 4 5 |
ref = db.reference('Employee') ref.update({ 'emp1/lname': 'updated lname', 'emp2/lname': 'updated lname' }) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
ref = db.reference('Employee2') # Generate a reference and add some data using push() emp_ref = ref.push({ 'name': 'Bob', 'lname': "Logan", 'age': 26 }) # Get the unique key generated emp_key = emp_ref.key print(emp_key) |
You can retrieve the data easily using this code.
1 2 |
ref = db.reference('Employee') print(ref.get()) |
This is the complete code for this article.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file cred = credentials.Certificate('firebase-sdk.json') # Initialize the app with a service account firebase_admin.initialize_app(cred, { 'databaseURL': 'https://Firebase Realtime Database URL/' }) ref = db.reference('/') ref.set({ 'Employee': { 'emp1': { 'name': 'Parwiz', 'lname': "Forogh", 'age': 24, }, 'emp2': { 'name': 'John', 'lname': 'Doe', 'age': 20, } } }) ''' #updating data ref = db.reference('Employee') box_ref = ref.child('emp1') box_ref.update({ 'name': 'updated name' }) ''' #working with mullti path update ''' ref = db.reference('Employee') ref.update({ 'emp1/lname': 'updated lname', 'emp2/lname': 'updated lname' }) ''' #adding data in second way you can create the key ''' ref = db.reference('Employee2') # Generate a reference to a location emp_ref = ref.push({ 'name': 'Bob', 'lname': "Logan", 'age': 26 }) # Get the unique key generated emp_key = emp_ref.key print(emp_key) ''' #retreiving data ''' ref = db.reference('Employee') print(ref.get()) ''' |
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
thanks, it worked!