How to use Firebase Functions as a Simple Backend Solution to handle incoming Webhooks

Musa Kökçen
Oct 26, 2020 · 6 min read

In this article, we will explain how to create a Firebase function and use it as a callback url. This function will serve as a backend service, when it is triggered, it will interpret incoming post data and accordingly will send a push notification to a specific user.

Prerequisite:

  • Firebase Push Notification service should be configured for your project. (If you need help with this subject you can have a look at this article)
  • Firebase Realtime Database should be configured for your project.
  • Upgrading our Firebase app to Blaze plan

In order to use Firebase Functions, Google tells us that we have to upgrade our account to Blaze plan. Cloud Functions and Realtime Database are free until you reach a certain usage limits. You can see pricing here. The limits look too much to exceed but it is a bit tricky. On August 17, 2020, Google changed Container registry pricing. So you may pay some tiny amount. (e.g. $0.026)

You can check related Stack Overflow links below.

Case:

We use a third party API for our business. It sends a webhook to inform us about result of the operation. The webhook includes a multiform data. We parse operation ID and success info from the JSON in it, then, send a push notification to the owner of the operation.

There are three main issues here;

  • Storing operation ID and the owner’s push token
  • Receiving webhook
  • Parsing webhook and sending push notification

Story:

Let’s say the user presses a button and the app makes an API call. At that moment, the operation ID is saved together with the user’s push token in order to find the owner of the operation ID that we receive in the webhook.

Get Started

We store the operation ID and push token*, in the completion of the API call.

*You can access fcmToken in AppDelegate

We saved the operation ID and the user’s push token to the database, under “TransferData” object.

Create Cloud Function

We first need to enable Functions in our Firebase project.

Firebase -> Functions -> Get Started

We open Terminal and specify the location that we want to install Node.js (functions are written in Node.js).

// example: cd Desktop/MyFunctionsnpm install -g firebase-tools

If this is the first time that you install Node.js , probably you will get some errors. If so, proceed to this page.

Follow the instructions and make sure you successfully complete this step.

Still in terminal, run;

firebase init

If you face any issue in this step, try this command;

curl -sL  https://firebase.tools | bash

Then,

firebase init

The API that we use sends webhook with a multiform data. Therefore, we also need to install Busboy. This library parses multiform data.

npm install busboy

here is a small tip on .gitignore

We did not want to have separate projects for our app and functions. Therefore, we created functions folder in our app’s folder. So that, we can access it when we clone or pull the project. However, the default .gitignore file in the functions is not enough in this case. We added node_modules folder (which is created by firebase) to the main .gitignore file as functions/node_modules

Let’s start developing our function — in Index.js

Now, everything is ready. We only need to write our functions and deploy them to Firebase. The functions are written in Index.js file. We open this file. (You can either use JavaScript or TypeScript, we chose JavaScript)

  • Let’s first import busboy. On the top of the file;
const Busboy = require('busboy');

Let’s give a name to our function. This name will be also included in the url. You should give a proper name if it is going to be a public url.

  • Change the default name after “export”;
export.dataTransferResult
  • The function is now named “dataTransferResult”. We want it to be triggered when we receive a webhook. Therefore, we write;

When we deploy this function to Firebase, it will have a url. We will assign it as our callback url. The API will send webhook and this function will be triggered on request.

We receive multiform data, so that we need to parse it;

const busboy = new Busboy({ headers: request.headers });

Then, we look for the field that we need and parse it;

Once a webhook is sent, dataTransferResult function is called. Then, we parse the data and get the ID and the success info. Now, we need to find the user who sent this data and send him/her a push notification with the result.

We get the ID for the operation in the webhook. We need to find that ID in the database and then find the push token that the ID is belong to.

We stored the push token as a key under “TransferData/id”. Above code gives us a snapshot for a specific ID. We will parse that snapshot and get the push token.

We parsed the snapshot and got the token. Let’s send a push notification to the user with that token;

Final look should be like this;

If everything is fine so far, save the file, open terminal, and deploy it to Firebase;

firebase deploy

If you have multiple projects in Firebase, you can switch between them and deploy each one by using the project id, which is found under Firebase -> Project Overview -> Project Settings.

firebase use <project_id>
firebase deploy

That’s all.

From now on when the API is called in the app, it will send a webhook to the given url, which means that it will trigger the firebase function. After that, our firebase function will send a push notification to the user about the result of the operation.

At Plus Minus One, we love to learn and share our experiences. We hope this article makes your life easier 🙏🏻

Plus Minus One

Plus Minus One — Stories & Experiences

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store