0
0
GcpHow-ToBeginner · 4 min read

How to Use Firestore with Python: Simple Guide

To use Firestore with Python, install the google-cloud-firestore library, set up authentication with a service account key, then create a Firestore client in your Python code to read and write data. Use client.collection() to access collections and document() to work with documents.
📐

Syntax

Here is the basic syntax to connect and use Firestore in Python:

  • Import library: Import Firestore client from google.cloud.
  • Create client: Initialize Firestore client to connect to your database.
  • Access collection: Use client.collection('collection_name') to get a collection reference.
  • Access document: Use collection.document('doc_id') to get a document reference.
  • Set data: Use document.set(data_dict) to write data.
  • Get data: Use document.get() to read data.
python
from google.cloud import firestore

# Initialize Firestore client
client = firestore.Client()

# Reference a collection
collection_ref = client.collection('users')

# Reference a document
doc_ref = collection_ref.document('user_123')

# Set data in document
doc_ref.set({'name': 'Alice', 'age': 30})

# Get data from document
doc = doc_ref.get()
if doc.exists:
    print(doc.to_dict())
Output
{'name': 'Alice', 'age': 30}
💻

Example

This example shows how to add a user document to Firestore and then read it back.

python
from google.cloud import firestore

# Create Firestore client
client = firestore.Client()

# Add a new user document
user_ref = client.collection('users').document('user_001')
user_ref.set({
    'name': 'John Doe',
    'email': 'john@example.com',
    'age': 28
})

# Retrieve the user document
user_doc = user_ref.get()
if user_doc.exists:
    print('User data:', user_doc.to_dict())
else:
    print('No such document!')
Output
User data: {'name': 'John Doe', 'email': 'john@example.com', 'age': 28}
⚠️

Common Pitfalls

Common mistakes when using Firestore with Python include:

  • Not setting up authentication with a service account JSON key or not setting the GOOGLE_APPLICATION_CREDENTIALS environment variable.
  • Using incorrect collection or document names causing DocumentReference errors.
  • Trying to read a document that does not exist without checking doc.exists.
  • Forgetting to install the google-cloud-firestore package.

Always check for document existence before accessing data and ensure authentication is properly configured.

python
import os

# Wrong: No authentication setup
from google.cloud import firestore
client = firestore.Client()  # This will fail if credentials are missing

# Right: Set environment variable for credentials
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/service-account.json'
client = firestore.Client()  # Now works with proper auth
📊

Quick Reference

Here is a quick reference for common Firestore Python client methods:

MethodDescription
firestore.Client()Creates a Firestore client to connect to the database
client.collection('name')Gets a reference to a collection
collection.document('id')Gets a reference to a document
document.set(data)Writes data to a document
document.get()Reads data from a document
document.update(data)Updates fields in a document
document.delete()Deletes a document

Key Takeaways

Install and import the google-cloud-firestore library to use Firestore with Python.
Set up authentication using a service account JSON key and the GOOGLE_APPLICATION_CREDENTIALS environment variable.
Use client.collection() and document() to access Firestore data.
Always check if a document exists before reading its data.
Handle common errors by verifying collection/document names and authentication setup.