Add/Set array in Firestore with the Firebase Admin SDK : Python 🐍


Add/Set array in Firestore with the Firebase Admin SDK : Python 🐍
The 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 FCM server protocols.
·         Generate and verify Firebase auth tokens.
·         Access Google Cloud Platform resources like Cloud Storage buckets and 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.

Currently Node.js, Java, Python, Go, C# are supported languages in Admin SDK.

This tutorial demonstrates adding array in Firestore database through Admin SDK using python.


Few things to note:

·         Your system needs Python 2.7+ or 3.x.
·         You need to generate Firebase Admin SDK jason key to use in your python application.
·         Also, you need to install firebase-admin SDK in your system in python environment.



To do so run pip install firebase-admin command in your command prompt (This step is to be performed after Python installation completes)

 To generate jason key perform the following steps:

·         If you don't already have a Firebase project, add one in the Firebase console. The Add project dialog also gives you the option to add Firebase to an existing Google Cloud Platform project.
·         Navigate to the Service Accounts tab in your project's settings page.
·         Click the Generate New Private Key button at the bottom of the Firebase Admin SDK section of the Service Accounts tab.
·         After you click the button, a JSON file containing your service account's credentials will be downloaded.

The jason file contains sensitive information, including your service account's private encryption key. Keep it confidential and never store it in a public repository.

Once you have done all setup we can proceed to our objective i.e. adding array in Firestore database using python

Rename the jason file to ServiceAccountKey.json and copy in the directory in which your python code is saved

Following is the code to setup Firestore client

import firebase_admin
from firebase_admin import credentials,firestore
creed=credentials.Certificate('./ServiceAccountKey.json')
default_app=firebase_admin.initialize_app(creed)

db=firestore.client()
In our example our collection name would be example_collection and our document name would be example_document in which there would be an array object test.
db.collection(u'example_collection').document(u'example_document').set({
 u'test':[
         'hello','World'
         ]    
})
Run the following code and make sure your system has an active internet connection with the following command in the command prompt with path to the python file. (In this example the python file name is demo.py)
python demo.py




Congratulation!! You have successfully created a python application with Admin SDK to create a Firestore array.

All the Firestore database and Firebase Admin SDK operations are subject to billing as per usage. Please read the pricing information of required entities on Google official site for Cloud products and perform your task at your own risk.

Comments

Popular posts from this blog

Add/Set collection in Firestore with the Firebase Admin SDK : Python 🐍

Writing to external SD card in Android 5.0 and above