Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.

https://firebase.google.com/docs/functions/

Key capabilities

Some the key capabilities of Firebase Function that you should use are:

  • HTTP Triggers
  • Realtime Database Triggers
  • Cloud Firestore Triggers

For this tutorial we will create a HTTP Trigger as an example. A HTTP Trigger is basically just a REST endpoint. So you're probably familiar with it.

Prerequisites

  • Firebase Project
  • NodeJs
  • Firebase CLI
  • Text Editor

If you have completed all of the prerequisites above then let's start.

Initialize a Firebase Functions Project

Open your terminal or cmd and cd to the directory of your choice. Create a folder called hello-functions.

From the hello-functions folder type:

firebase init

You should see this cool Firebase text art.

Here you will see some options, these are the features of Firebase that you can use. In our case go ahead a choose the Functions: Configure and deploy Cloud Functions. Navigate to it and press SPACE BAR to select, then press ENTER to confirm selection.

This next step will ask you to choose the Firebase Project that you want to upload your Firebase Functions to. For tihsexample I will choose my personal project Kitty Ipsum.

The next steps will ask you to choose a language, choose Javascript.

Then you will be asked if you want to use ESLint, just choose No.

The last step will ask you if you want to install the dependencies using npm. Choose Yes.

After the initialization and installation of dependencies you should see this on your terminal screen.

The Project Structure

Now open your hello-functions project in a text editor.

Inside you project folder are the following:

  • .firebaserc - Some firebase configuration.
  • .gitignore - This file contains the files that you would like to be ignored by git.
  • firebase.json - Used for hosting configuration, similar to `.httaccess` in apache.
  • functions - This folder contains the package.json and the index.js where we will write our codes.

Let's write some code

Open the  index.js

The  index.js contains a sample code generated by the  Firebase CLI.

Just uncomment the commented lines. This function is an example of HTTP Trigger.

Let's customize this a bit.

Some small changes has been made

On line 6, I just renamed the function from  helloWorld to  helloRanromCodeTips.

You will notice that I added the line:

const name = request.query.name;

Since HTTP Triggers is basically a REST protocol you can do whatever you can do with frameworks like express.js. So this line parse a query parameter called name.

On line 8: response.send('Hello ' + name);

This is just a simple HTTP response. We will send the string Hello + whatever the content of the variable name is.

Deploy

We can now deploy our Firebase Function.

Go back to your terminal and type the following:

firebase deploy --only functions -P kitty-ipsum

This command is deploy the our function  helloRandomCodeTips to  -P project  kitty-ipsum.

The deployment was successful

Once the deployment is successful go ahead and open you firebase project dashboard and go to the Functions tab. You should see the function there.

You will see the endpoint of you function in this table.

To test our example go to this link. You can change the value of the query name in the URL to see how it works.

HTTP Triggers is just one of the many amazing things you can create with Firebase Functions. Go ahead and play with the documentation and see what else you can do.

Congrats! You just created your first Firebase Function.