Firebase Admin Python SDK enables server-side Python developers to integrate Firebase services into their applications from privileged environments.
—
Core Firebase application lifecycle management including initialization, configuration, and cleanup. The App class serves as the entry point for all Firebase services, managing credentials and providing the foundation for Firebase service access.
Initialize Firebase applications with credentials and configuration options. Applications can be initialized with service account credentials, application default credentials, or OAuth2 refresh tokens.
def initialize_app(credential=None, options=None, name='[DEFAULT]'):
"""
Initializes and returns a new App instance.
Creates a new App instance using the specified options and app name.
If an instance already exists by the same app name, a ValueError is raised.
If options are not provided, an attempt is made to load options from the
FIREBASE_CONFIG environment variable.
Args:
credential: A credential object used to initialize the SDK (optional).
If none is provided, Google Application Default Credentials are used.
options: A dictionary of configuration options (optional). Supported options
include 'databaseURL', 'storageBucket', 'projectId',
'databaseAuthVariableOverride', 'serviceAccountId' and 'httpTimeout'.
name: Name of the app (optional). Defaults to '[DEFAULT]'.
Returns:
App: A newly initialized instance of App.
Raises:
ValueError: If the app name is already in use, or any of the provided
arguments are invalid.
"""Retrieve existing Firebase application instances by name for use across different parts of your application.
def get_app(name='[DEFAULT]'):
"""
Retrieves an App instance by name.
Args:
name: Name of the App instance to retrieve (optional).
Defaults to '[DEFAULT]'.
Returns:
App: An App instance with the given name.
Raises:
ValueError: If the specified name is not a string, or if the specified
app does not exist.
"""Gracefully delete Firebase applications and clean up associated resources including active service connections.
def delete_app(app):
"""
Gracefully deletes an App instance.
This will clean up any services associated with the App and prevent
further use of the App instance.
Args:
app: The app instance to be deleted.
Raises:
ValueError: If the app is not initialized or not of type App.
"""Firebase Admin SDK supports multiple credential types for different authentication scenarios.
class Certificate:
"""Service account certificate credential."""
def __init__(self, cert):
"""
Initialize with service account certificate.
Args:
cert: Path to service account JSON file or certificate dict
"""
@property
def project_id(self):
"""The project ID from the service account."""
@property
def service_account_email(self):
"""The service account email address."""class ApplicationDefault:
"""Google Application Default Credentials."""
def __init__(self):
"""Initialize with application default credentials."""
@property
def project_id(self):
"""The project ID from the credential."""class RefreshToken:
"""OAuth2 refresh token credential."""
def __init__(self, refresh_token):
"""
Initialize with refresh token.
Args:
refresh_token: Path to refresh token JSON file or token dict
"""
@property
def client_id(self):
"""The OAuth2 client ID."""
@property
def client_secret(self):
"""The OAuth2 client secret."""
@property
def refresh_token(self):
"""The refresh token string."""# Valid configuration keys
CONFIG_KEYS = [
'databaseAuthVariableOverride', # Custom auth variable override
'databaseURL', # Realtime Database URL
'httpTimeout', # HTTP request timeout in seconds
'projectId', # Google Cloud project ID
'storageBucket' # Cloud Storage bucket name
]The SDK can automatically load configuration from the FIREBASE_CONFIG environment variable:
# JSON string format
export FIREBASE_CONFIG='{"projectId":"my-project","databaseURL":"https://my-project-default-rtdb.firebaseio.com"}'
# File path format
export FIREBASE_CONFIG='/path/to/firebase-config.json'import firebase_admin
from firebase_admin import credentials
# Initialize with service account
cred = credentials.Certificate("serviceAccountKey.json")
app = firebase_admin.initialize_app(cred)# Initialize multiple apps
default_app = firebase_admin.initialize_app(cred)
secondary_app = firebase_admin.initialize_app(cred, name='secondary')
# Retrieve apps
app1 = firebase_admin.get_app() # Default app
app2 = firebase_admin.get_app('secondary')app = firebase_admin.initialize_app(cred, {
'databaseURL': 'https://my-project-default-rtdb.firebaseio.com/',
'storageBucket': 'my-project.appspot.com',
'httpTimeout': 60
})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 AccessTokenInfo:
"""OAuth2 access token information."""
def __init__(self, access_token, expiry):
"""
Initialize access token info.
Args:
access_token: The access token string
expiry: Token expiration datetime
"""Install with Tessl CLI
npx tessl i tessl/pypi-firebase-admin