Firebase Firestore Python - Getting Started

Firebase Firestore Python - Getting Started

I'm going to guide you through the workings of this Firebase Firestore Python code, breaking it down for you so you can hit the ground running.

Installation

In order to install the Firebase Admin SDK for Python using pip, the following command should be executed in your terminal or command prompt. This command retrieves the firebase-admin package from the Python Package Index (PyPI) and proceeds to install it.

pip install firebase-admin

Service Account Creation of a new private key file for your service account is necessary. Follow these steps to create one:

Navigate to Settings > Service Accounts in the Firebase console.

Select Generate New Private Key, then affirm your choice by clicking Generate Key.

The JSON file that holds the key should be stored securely. We will be storing it as 'serviceAccount.json' in this scenario.

Import

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

Initialization

At this point, you can start your Firebase application using the credentials obtained from the service account key the code below accomplishes this.

cred = credentials.Certificate('path/to/serviceAccount.json')
app = firebase_admin.initialize_app(cred)
db = firestore.client()

Add Data

The code below creates a document reference. It does not create the document at this point, but it will be utilized for that purpose later.

data = {"name": "Los Angeles", "state": "CA", "country": "USA"}

The set() method can be used to append data to this document:

# Add a new doc in collection 'cities' with ID 'LA'
db.collection("cities").document("LA").set(data)snippets.py

This action creates (or replaces if it already exists) the document "LA" in the "users" collection with the provided data.

Read Data

Reading Data from Firestore The stream() method, which retrieves all documents from a collection, can be used to read data from Firestore.

docs = db.collection("cities").stream()

Then, loop over each document in the collection and print the data:

pythonCopy codefor doc in docs:
    print(f"{doc.id} => {doc.to_dict()}")

doc.id fetches the document's ID (in this case "LA"), while doc.to_dict() transforms the document data into a Python dictionary.

Full Code

Congratulations! You have now crafted a Python script that can both write and read data from Firebase Firestore. This simple operation can act as a launch pad for constructing more intricate queries and operations to suit the needs of your application.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

# Use a service account.
cred = credentials.Certificate('./serviceAccount.json')
app = firebase_admin.initialize_app(cred)
db = firestore.client()

##
## ADD DATA

data = {"name": "Los Angeles", "state": "CA", "country": "USA"}

# Add a new doc in collection 'cities' with ID 'LA'
db.collection("cities").document("LA").set(data)

##
## READ DATA

docs = db.collection("cities").stream()

for doc in docs:
    print(f"{doc.id} => {doc.to_dict()}")