Firebase Functions Python SDK
The `firebase-functions` Python SDK enables developers to create and deploy serverless functions that respond to events in their Firebase projects and Google Cloud environment. It leverages Cloud Functions for Firebase (2nd gen) and integrates with various Firebase features like Firestore, Realtime Database, Authentication, and HTTPS requests. The library is actively maintained with frequent releases, with the current version being 0.5.0.
Warnings
- gotcha Python support for Cloud Functions for Firebase is in Public Preview. This means the functionality might change in backward-incompatible ways, is not subject to any SLA or deprecation policy, and may receive limited support.
- breaking The `firebase-functions` library requires Python 3.10 or higher. The `firebase-admin` SDK (a common dependency) also dropped support for Python 3.7 and 3.8, and deprecated 3.9, with version 7.0.0. Ensure your local environment and deployment runtime are on a supported Python version (3.10-3.13).
- gotcha When using `firebase-admin` within your functions, you must explicitly `import firebase_admin` (in addition to any submodules like `firebase_admin.auth`) before calling `firebase_admin.initialize_app()`. Failure to do so will result in a `NameError`. Also, ensure `firebase-admin` is listed in your `requirements.txt`.
- gotcha To avoid 'cold start' performance issues, reuse expensive objects (like database connections, API clients, or large data structures) by declaring them as global variables outside of your function handler. They will be initialized once per container instance, rather than on every function invocation.
- gotcha Ensure all Python package dependencies are correctly specified in `requirements.txt` within your functions directory. Missing or incorrect dependencies can lead to deployment failures or runtime errors in your deployed functions.
- gotcha Deployment region needs to be explicitly specified for optimal performance and compliance. If not set, functions will deploy to the default `us-central1`. You can set a global region using `options.set_global_options(region=options.SupportedRegion.YOUR_REGION)` or per function.
Install
-
pip install firebase-functions
Imports
- https_fn
from firebase_functions import https_fn
- firestore_fn
from firebase_functions import firestore_fn
- options
from firebase_functions import options
- initialize_app
import firebase_admin; initialize_app()
from firebase_admin import initialize_app
Quickstart
import os
from firebase_functions import https_fn
from firebase_admin import initialize_app
# Initialize the Firebase Admin SDK. When deployed to Cloud Functions,
# the SDK automatically detects the environment's service account credentials.
initialize_app()
@https_fn.on_request()
def hello_world(request: https_fn.Request) -> https_fn.Response:
"""Responds to an HTTP request with a 'Hello from Python!' message."""
# Example of accessing environment variables (if any are set during deployment)
message_prefix = os.environ.get('MESSAGE_PREFIX', 'Hello')
# Check for a 'name' query parameter or use a default.
name = request.args.get('name', 'World')
return https_fn.Response(f"{message_prefix}, {name} from Python!")