Cloud Functions for Firebase Samples

repository·main·Indexed 11 days ago

https://github.com/firebase/functions-samples

A collection of sample projects demonstrating various use cases for Cloud Functions for Firebase. Includes examples covering different triggers (Firestore, Auth, Storage) and runtimes including Node.js, Python, and experimental Dart support for callable functions and HTTPS triggers.

Tokens
121.3K
Snippets
387
Records
505
Agent score
94%

What's inside Cloud Functions for Firebase Samples

  1. Quickstart: Uppercaser for Firestore

    main

    This sample demonstrates how to use Cloud Functions for Firebase with Firestore. The application implements two main workflows:

    1. HTTP Trigger: An HTTP function (addMessage) receives a text message via an HTTPS request and writes it to Firestore using the Firebase Admin SDK.
    2. Firestore Trigger: A background function (makeUppercase) triggers automatically whenever a new document is added to Firestore, converting the message text to uppercase.

    Prerequisites

    • A Firebase project created in the Firebase Console.
    • The Firebase CLI installed and configured.
  2. Overview of the Assistant Say Number sample

    main

    This sample demonstrates how to build a Google Assistant action using the Actions SDK hosted on Cloud Functions. The action is designed to listen for a number provided by the user and respond with its ordinal equivalent (e.g., if the user says "Twelve", the action responds with "The ordinal of twelve is twelfth").

    Key Technologies:

    • Google Actions SDK: Used to handle incoming Google Assistant requests.
    • Cloud Functions for Firebase: Hosts the logic for the action.
    • Actions SDK: Documentation.
    • Firebase SDK: Documentation.
  3. Use server-side Remote Config with Cloud Functions and Vertex AI

    main

    This sample demonstrates how to use a Cloud Function (generateWithVertex) to generate text using the Google Vertex AI Gemini API. It leverages the Firebase Admin SDK for Node.js and Firebase Remote Config to dynamically manage model parameters, safety settings, and feature flags on the server side.

    To ensure security, this function is designed to run with a service account that has unauthenticated access blocked. This follows the recommended pattern for protecting API endpoints in server-side Remote Config implementations.

    If you require a callable function with App Check protection, refer to the call-vertex-remote-config-server sample instead.

  4. Send FCM notifications for new followers

    main

    This sample demonstrates how to trigger a Firebase Cloud Messaging (FCM) notification using a Cloud Function that listens to changes in the Firebase Realtime Database.

    How it works

    1. Device Tokens: A Web client saves device tokens to the Realtime Database under /users/$uid/notificationTokens.
    2. Trigger: The Function is triggered whenever a follow relationship is created at /followers/$followedUid/$followerUid.
    3. Notification: The Function uses the Firebase Admin SDK to send the notification to the device tokens associated with the $followedUid.

    Database Schema

    To use this pattern, your Realtime Database should follow this structure:

    User Notification Tokens:

    /users
      /$uid
        /notificationTokens
          /$token: true

    Follower Relationships:

    /followers
      /$followedUid
        /$followerUid: true
  5. Enable full text search on Firestore

    main

    You can implement full text search for Firestore documents by using Cloud Functions to sync document changes to a hosted search service. This template demonstrates how to integrate Firestore with one of the following services:

    When a document is created or modified in Firestore, a Cloud Function is triggered to send the document content to the chosen search engine for indexing.

  6. Use Node.js (1st gen) samples

    main

    This directory contains sample code specifically for 1st generation Cloud Functions for Firebase using Node.js.

    Note: 2nd generation functions are the recommended platform for Cloud Functions for Firebase. Use these 1st gen samples only if your project specifically requires the 1st gen runtime or if you are maintaining legacy infrastructure.

  7. Use Python (2nd gen) Cloud Functions for Firebase

    main

    This repository contains sample implementations for Python functions using the 2nd generation Cloud Functions for Firebase.

    Important Note on Preview Status: Python support in Cloud Functions for Firebase is currently in public preview. As a result:

    • Functionality may change in backward-incompatible ways.
    • The preview release is not subject to any SLA or deprecation policy.
    • Support may be limited or unavailable.

    To see a complete list of all available samples, refer to the root README.

  8. Automatically convert audio files using ffmpeg

    main

    This sample demonstrates how to use a Cloud Function to automatically convert audio files uploaded to Cloud Storage into the FLAC format with mono-channel audio at 16000Hz.

    Workflow:

    1. An audio file is uploaded to the default Cloud Storage bucket.
    2. The function triggers on the upload.
    3. The function downloads the file to the local /tmp directory using the google-cloud SDK.
    4. ffmpeg (via fluent-ffmpeg) performs the conversion.
    5. The converted file is saved back to Cloud Storage with a _output.flac suffix.
  9. Stripe integration features and workflow

    main

    This sample implements a complete Stripe payment workflow using Firebase services:

    • Customer Creation: Automatically creates a Stripe customer object when a new user signs up via Firebase Authentication.
    • Payment Method Collection: Uses Stripe Elements to securely collect and store card details for future use.
    • Automated Charging: Triggers a payment on the customer's card whenever a new document is written to the payments collection in Firestore.
      • Security Note: This sample creates the payment document on the client with an amount and currency. In production, you must validate price details within the Cloud Function (e.g., by looking up product information in Firestore).
    • SCA/3D Secure Support: Handles 3D Secure authentication if required by the card issuer to comply with Strong Customer Authentication (SCA) regulations.
  10. Access Node.js (2nd gen) Cloud Functions samples

    main
    This directory contains sample implementations for Cloud Functions for Firebase using the 2nd generation Node.js runtime. These samples demonstrate how to implement various triggers (Firestore, Realtime Database, Auth, etc.) using the latest generation of Cloud Functions features. For a complete list and description of all available samples in the repository, refer to the root README.
  11. Send in-app feedback to Jira via App Distribution Alerts

    main

    This sample demonstrates how to automate Jira issue creation using a Firebase Cloud Function triggered by an inappfeedbackpayload from a Firebase App Distribution Alert. When a tester provides in-app feedback, the function captures the feedback details and screenshots, then uses Jira's REST APIs to create a corresponding issue in a specified Jira project.

    You can customize the implementation in functions/index.js to support on-premise Jira instances, custom issue types, or different reporting workflows.