Firebase Admin Python SDK enables server-side Python developers to integrate Firebase services into their applications from privileged environments.
npx @tessl/cli install tessl/pypi-firebase-admin@7.1.0The Firebase Admin Python SDK enables server-side (backend) Python developers to integrate Firebase services into their applications and services from privileged environments like servers or cloud platforms. It provides comprehensive access to Firebase Authentication, Cloud Firestore, Realtime Database, Cloud Messaging, Cloud Storage, Remote Config, Cloud Functions, App Check, Machine Learning, and Project Management APIs.
pip install firebase-adminimport firebase_admin
from firebase_admin import credentialsCommon service imports:
from firebase_admin import auth
from firebase_admin import firestore
from firebase_admin import db
from firebase_admin import messaging
from firebase_admin import storage
from firebase_admin import tenant_mgtimport firebase_admin
from firebase_admin import credentials
from firebase_admin import auth
# Initialize the SDK with a service account
cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://your-project-default-rtdb.firebaseio.com/',
'storageBucket': 'your-project.appspot.com'
})
# Use Firebase Authentication
user = auth.get_user('user-uid')
print(f'Successfully fetched user data: {user.display_name}')
# Create custom token
custom_token = auth.create_custom_token('user-uid')
print(f'Custom token: {custom_token.decode()}')Firebase Admin SDK follows a service-oriented architecture:
Each service module provides both high-level convenience functions and access to underlying client objects for advanced operations.
Core application lifecycle management including initialization, configuration, and cleanup. Manages credentials and provides the foundation for all Firebase service access.
def initialize_app(credential=None, options=None, name='[DEFAULT]'):
"""
Initializes and returns a new App instance.
Args:
credential: A credential object (optional)
options: Configuration options dict (optional)
name: Name of the app (optional)
Returns:
App: A newly initialized App instance
"""
def get_app(name='[DEFAULT]'):
"""
Retrieves an App instance by name.
Args:
name: Name of the App instance to retrieve
Returns:
App: An App instance with the given name
"""
def delete_app(app):
"""
Gracefully deletes an App instance.
Args:
app: The app instance to be deleted
"""Comprehensive user authentication and management capabilities including custom token generation, ID token verification, user CRUD operations, and multi-tenant authentication.
def create_custom_token(uid, developer_claims=None, app=None):
"""Create a custom token for the given UID."""
def verify_id_token(id_token, app=None, check_revoked=False, clock_skew_seconds=0):
"""Verify a Firebase ID token."""
def get_user(uid, app=None):
"""Get a user by UID."""
def create_user(**kwargs):
"""Create a new user."""
def update_user(uid, **kwargs):
"""Update an existing user."""
def delete_user(uid, app=None):
"""Delete a user."""Complete Cloud Firestore database integration providing access to the full Google Cloud Firestore client with document and collection operations, queries, transactions, and batch operations.
def client(app=None, database_id=None):
"""
Return a client for interacting with the Cloud Firestore database.
Args:
app: Firebase app instance (optional)
database_id: Database ID (optional)
Returns:
google.cloud.firestore.Client: Firestore client instance
"""Firebase Realtime Database operations including data reading, writing, querying, transactions, and real-time listeners for JSON tree-structured data.
def reference(path='/', app=None, url=None):
"""
Return a database reference for the specified path.
Args:
path: Database path (default: '/')
app: Firebase app instance (optional)
url: Database URL (optional)
Returns:
Reference: Database reference instance
"""Firebase Cloud Messaging (FCM) for sending push notifications and managing topic subscriptions across Android, iOS, and web platforms.
def send(message, dry_run=False, app=None):
"""Send a message to Firebase Cloud Messaging."""
def send_each(messages, dry_run=False, app=None):
"""Send multiple messages to Firebase Cloud Messaging."""
def subscribe_to_topic(tokens, topic, app=None):
"""Subscribe registration tokens to a topic."""
def unsubscribe_from_topic(tokens, topic, app=None):
"""Unsubscribe registration tokens from a topic."""Firebase Cloud Storage integration providing access to Google Cloud Storage buckets for file upload, download, and management operations.
def bucket(name=None, app=None):
"""
Return a handle to a Cloud Storage bucket.
Args:
name: Bucket name (optional, uses default from config)
app: Firebase app instance (optional)
Returns:
google.cloud.storage.Bucket: Storage bucket instance
"""Firebase Cloud Functions integration for enqueueing tasks in Cloud Task queues associated with callable functions.
def task_queue(function_name, extension_id=None, app=None):
"""
Get a TaskQueue instance for the specified function.
Args:
function_name: Name of the Cloud Function
extension_id: Extension ID (optional)
app: Firebase app instance (optional)
Returns:
TaskQueue: Task queue instance
"""Firebase ML model management for deploying and managing custom machine learning models in Firebase projects.
def create_model(model, app=None):
"""Create a new ML model in Firebase."""
def get_model(model_id, app=None):
"""Get an ML model by ID."""
def list_models(list_filter=None, page_size=None, page_token=None, app=None):
"""List ML models in the project."""Firebase Remote Config server-side template management for dynamic app configuration without requiring app updates.
class ServerTemplate:
"""Server-side Remote Config template management."""
def evaluate(self, context=None):
"""Evaluate the template with optional context."""
def load(self):
"""Load the current Remote Config template."""Firebase project management for creating and managing Android and iOS app configurations within Firebase projects.
def create_android_app(package_name, display_name=None, app=None):
"""Create a new Android app in the Firebase project."""
def create_ios_app(bundle_id, display_name=None, app=None):
"""Create a new iOS app in the Firebase project."""
def list_android_apps(app=None):
"""List all Android apps in the project."""
def list_ios_apps(app=None):
"""List all iOS apps in the project."""Multi-tenant authentication management using Google Cloud Identity Platform (GCIP), enabling isolated authentication environments within a single project.
def auth_for_tenant(tenant_id, app=None):
"""Get an Auth client scoped to a specific tenant."""
def get_tenant(tenant_id, app=None):
"""Get a tenant by ID."""
def create_tenant(display_name, allow_password_sign_up=None, enable_email_link_sign_in=None, app=None):
"""Create a new tenant."""
def update_tenant(tenant_id, display_name=None, allow_password_sign_up=None, enable_email_link_sign_in=None, app=None):
"""Update an existing tenant."""
def delete_tenant(tenant_id, app=None):
"""Delete a tenant."""
def list_tenants(page_token=None, max_results=100, app=None):
"""List tenants with pagination."""class App:
"""Firebase application instance."""
@property
def name(self):
"""The name of this App instance."""
@property
def credential(self):
"""The credential used to initialize this App."""
@property
def options(self):
"""The configuration options for this App."""
@property
def project_id(self):
"""The Google Cloud project ID associated with this App."""
class FirebaseError(Exception):
"""Base exception for all Firebase-related errors."""
def __init__(self, code, message, cause=None, http_response=None):
"""
Initialize FirebaseError.
Args:
code: Error code string
message: Error message
cause: Underlying cause exception (optional)
http_response: HTTP response object (optional)
"""
@property
def code(self):
"""The error code."""
@property
def cause(self):
"""The underlying cause of this error."""
@property
def http_response(self):
"""The HTTP response that caused this error."""