Firebase Functions Python SDK

0.5.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart defines a simple HTTP-triggered function that responds with a greeting. It demonstrates initializing the Firebase Admin SDK (which is automatic in Cloud Functions environments) and handling basic request parameters. To deploy, save this as `main.py` in your functions directory, add `firebase-functions` and `firebase-admin` to `requirements.txt`, and run `firebase deploy --only functions`.

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!")

view raw JSON →