Using Webhooks with Firebase to Automate Workflows

Friday, January 31, 2025

Webhooks allow applications to send real-time data to other services when specific events occur. In a Firebase project, webhooks can be used to automate tasks like sending notifications, syncing data with external services, or triggering business logic when Firestore data changes.

Since Firebase doesn’t have built-in webhook support, we can use Firebase Cloud Functions to listen for database changes and send HTTP requests to external APIs. This guide will walk you through setting up webhooks using Firebase Cloud Functions to automate workflows in your app.


Setting Up Firebase Functions for Webhooks

To implement webhooks in Firebase, we need Firebase Cloud Functions. These functions allow us to run backend logic in response to Firestore events or HTTP requests.

Step 1: Install Firebase CLI and Initialize Functions

First, make sure you have Firebase CLI installed. If not, install it with:

Language: bash
npm install -g firebase-tools

Log in to Firebase:

Language: bash
firebase login

Navigate to your Firebase project directory and initialize Firebase Functions:

Language: bash
firebase init functions

Select JavaScript or TypeScript when prompted. Once initialized, navigate to the functions/ directory to start writing your Cloud Functions.


Listening for Firestore Changes

We can trigger a webhook when a Firestore document is added, updated, or deleted.

Example: Triggering a Webhook on Document Creation

Inside functions/index.js (or functions/src/index.ts for TypeScript), add:

Language: javascript
const functions = require("firebase-functions");
const axios = require("axios");

exports.sendWebhookOnNewUser = functions.firestore
  .document("users/{userId}")
  .onCreate(async (snap, context) => {
    const newUser = snap.data();

    const webhookUrl = "https://your-webhook-endpoint.com";

    try {
      await axios.post(webhookUrl, {
        event: "new_user",
        data: newUser,
      });
      console.log("Webhook sent successfully");
    } catch (error) {
      console.error("Error sending webhook:", error);
    }
  });

How It Works

  • Listens for a new document added to the users collection.
  • Extracts user data and sends a POST request to a webhook URL.
  • Uses axios to send the data and logs the response.

  • Sending Data to an External API

    Webhooks can be used to notify an external system when Firestore data changes. Here’s an example of sending order data to an external service when a new order is placed:

    Language: javascript
    exports.sendOrderWebhook = functions.firestore
      .document("orders/{orderId}")
      .onCreate(async (snap, context) => {
        const orderData = snap.data();
        const webhookUrl = "https://example.com/webhook";
    
        try {
          await axios.post(webhookUrl, {
            event: "new_order",
            data: orderData,
          });
          console.log("Order webhook sent successfully");
        } catch (error) {
          console.error("Error sending order webhook:", error);
        }
      });
    

    Use Cases for Webhooks in Firebase

  • Syncing Data: Send Firestore updates to an external database.
  • Triggering Notifications: Notify users via email or SMS when an event occurs.
  • Logging Events: Store important events in an external logging system.

  • Securing Your Webhook Requests

    Since webhook requests can be intercepted, it’s important to secure them.

    Use Environment Variables for API Keys

    Instead of hardcoding API keys, store them in environment variables:

    Language: bash
    firebase functions:config:set webhook.url="https://your-webhook-endpoint.com"

    Access them in your function:

    Language: javascript
    const webhookUrl = functions.config().webhook.url;

    Validate Payloads

    To prevent unauthorized requests, add a secret token and validate it:

    Language: javascript
    const webhookSecret = "your-secret-token";
    
    exports.secureWebhook = functions.firestore
      .document("secure-data/{docId}")
      .onCreate(async (snap, context) => {
        const data = snap.data();
        const webhookUrl = "https://your-webhook-endpoint.com";
    
        try {
          await axios.post(webhookUrl, {
            event: "secure_event",
            data: data,
            secret: webhookSecret,
          });
        } catch (error) {
          console.error("Error sending secure webhook:", error);
        }
      });

    On the receiving end, validate the secret before processing the request.


    Testing and Deployment

    Deploying the Firebase Function

    Once your webhook function is ready, deploy it using:

    Language: bash
    firebase deploy --only functions

    Testing the Webhook

  • Use Postman or cURL to manually send test requests to your webhook URL.
  • Check the Firebase Logs to ensure the function executes correctly:
  • Language: bash
    firebase functions:log

    Conclusion

    Webhooks allow Firebase applications to interact with external services in real-time. By using Firebase Cloud Functions, you can automate workflows, send notifications, and sync data with other platforms.