Firebase Admin Python SDK
The Firebase Admin Python SDK enables server-side (backend) Python developers to integrate Firebase into their services and applications. It provides programmatic access to Firebase services from trusted environments, allowing for tasks such as custom authentication, managing user data, sending FCM messages, and accessing Cloud Firestore and Storage. The current version is 7.3.0, and it maintains a regular release cadence with frequent updates.
Warnings
- breaking Version 7.0.0 dropped support for Python 3.7 and 3.8. Python 3.9 support is deprecated; developers are strongly advised to use Python 3.10 or higher.
- breaking In version 7.0.0, the `send_all()` and `send_multicast()` FCM APIs were removed.
- breaking The dependency on `google-api-python-client` was removed in v7.0.0, significantly reducing the SDK's bundle size. While this generally improves performance, ensure your project does not implicitly rely on this dependency.
- deprecated The `ActionCodeSettings.dynamic_link_domain` parameter was deprecated in v7.1.0 in favor of `link_domain` for customizing Firebase Hosting domains in email action flows.
- gotcha The Python Admin SDK for Firebase Realtime Database does not support real-time event listeners. All data retrieval operations are blocking.
- gotcha Service account JSON key files contain sensitive credentials. Never commit them directly to version control.
Install
-
pip install firebase-admin
Imports
- firebase_admin
import firebase_admin
- credentials
from firebase_admin import credentials
- firestore
from firebase_admin import firestore
- auth
from firebase_admin import auth
- storage
from firebase_admin import storage
- messaging
from firebase_admin import messaging
Quickstart
import os
import firebase_admin
from firebase_admin import credentials, firestore
# Best practice: store service account key in an environment variable
# and load it, or use Application Default Credentials on Google Cloud.
# Replace 'path/to/your/serviceAccountKey.json' with actual path if not using env var.
SERVICE_ACCOUNT_KEY_PATH = os.environ.get('FIREBASE_SERVICE_ACCOUNT_KEY_PATH', '')
if SERVICE_ACCOUNT_KEY_PATH:
cred = credentials.Certificate(SERVICE_ACCOUNT_KEY_PATH)
else:
# Fallback for Google Cloud environments where ADC are available
# or if you prefer not to use a file directly for local testing
cred = credentials.ApplicationDefault()
# Initialize the app
firebase_admin.initialize_app(cred, {
'projectId': os.environ.get('FIREBASE_PROJECT_ID', 'your-project-id'),
'databaseURL': os.environ.get('FIREBASE_DATABASE_URL', 'https://your-project-id.firebaseio.com')
})
db = firestore.client()
# Add data to Firestore
doc_ref = db.collection('users').document('alovelace')
doc_ref.set({
'first': 'Ada',
'last': 'Lovelace',
'born': 1815
})
print(f"Added document with ID: {doc_ref.id}")
# Read data from Firestore
users_ref = db.collection('users')
docs = users_ref.stream()
print("\nAll users:")
for doc in docs:
print(f"{doc.id} => {doc.to_dict()}")
# Clean up (optional, for demonstration purposes)
# firebase_admin.delete_app(firebase_admin.get_app())