Add/Set collection in Firestore with the Firebase Admin SDK : Python 🐍
·
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 collection in Firestore database through Admin SDK.
Few things to note:
1.
You cannot create an empty collection in
Firestore with Admin SDK to create so, you need to login to Firestore console
and add it manually.
3.
You need to generate Firebase Admin SDK jason
key to use in your python application.
4. 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:
1.
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.
2.
Navigate to the Service Accounts tab in your
project's settings page.
3.
Click the Generate New Private Key button at the
bottom of the Firebase Admin SDK section of the Service Accounts tab.
4.
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. create collection 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()
Since you cannot create a Firestore empty collection, we
will create a collection with a document
In our example our collection name would be
example_collection and our document name would be example_document in which
there would be a string 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.
(Note in our example python file name is demo)
python demo.py
Congratulation!! You have successfully created a python
application with Admin SDK to create a Firestore collection.
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
Post a Comment