How to create your first Cloud Function for Firebase

One of the biggest brand features that Firebase released in the past Google Cloud Next’17 and Google I/O’17 events was Cloud Functions for Firebase.

Whit this feature, finally we can create a “Backend” just using Firebase. It is a approach to a serverless architecture.

Cloud Functions runs JavaScript code in the server and this allows you write secret code like as API Keys and Secrets, payment processes, manage Cloud Storage and Database as admin.

As I said before, Cloud Functions should be written in JavaScript using a Node.js (v6.9.x) environment for now. In the future they will add new languages.

In this post we will show how install the tools and init the project to start to work with Cloud Functions for Firebase. Let’s go!

Environment setup

First of all, you need have installed Node.js, at least the version 6.9.x or higher. You can download and install for your SO from the Node.js project website. When you have done it, you can install the Firebase CLI in order to init a new project and setup functions feature. You can install it globally using npm:

$ npm install -g firebase-tools

You can verify if it is correctly installed whit the command:

$ firebase version
> 3.9.1
Make sure you have the Firebase CLI version 3.5.x or higher, if not the functions feature doesn’t work.

The following step is to create a new project in Firebase Console Dashboard and in the terminal with the command:

$ firebase init
> Which Firebase CLI features do you want to setup for this folder? Press Space to select features, then Enter to confirm your choices. (Press <space
> to select)
❯◯ Database: Deploy Firebase Realtime Database Rules
◯ Functions: Configure and deploy Cloud Functions
◯ Hosting: Configure and deploy Firebase Hosting sites

You need choose the “Functions” options to use Cloud Functions in your project. It creates a new folder called ‘functions’ inside your project. This is where the JavaScript code will go.

This folder contains a NPM project with a package.json file and a index.js where the Cloud functions must be declared.

The package.json file defined two libraries we will use, firebase-functions and firebase-admin.

  • firebase-functions allow us trigger Cloud Functions based on events from Realtime Database, Cloud Storage, HTTPS urls, Analytics conversions, etc…
  • firebase-admin allow us write and read the Realtime Database as admin role and to initialize the app at first.

To install this dependencies we must go to the functions directory of your project and run the following command:

$ npm install

This command download all the dependencies named in package.json file into a new folder called node_modules.

Developing your first Cloud Function

When you done it, you can require the libraries and use it from the index.js like the Node.js form (CommonJS):

// functions/index.js
const admin = require('firebase-admin');
const functions = require('firebase-functions);

The next line configure the Firebase app in the server side retrieving the firebase configuration from functions.config().firebase

// functions/index.js
admin.initializeApp(functions.config().firebase);
If you are not going to use the Storage and/or the Database from the Cloud functions, you don’t need to use the admin library either functions library.

To create and be able to use the Cloud Functions that we’ll develop, these must be exported it in order to Node.js environment created by Firebase in the deploy event can use them.

In this tutorial, I’m going to create a HTTPS function example that will be executed when we type the URL in the browser.

// functions/index.js
exports.helloWorld = functions.https.onRequest((req, res) => {
// TODO
});

HTTPS Cloud Functions syntax is likely ExpressJS. The Request event triggers a callback function with a request and a response object.

We type the function name (helloWorld) and we use the functions library with the https API and the onRequest event.

Inside the functions, we send to the browser only a response with the text: “Hello, World!” to test the workflow:

// functions/index.js
exports.helloWorld = functions.https.onRequest((req, res) => {
res.status(200).send('Hello, World!');
});

Now, it’s time to deploy to the cloud. To do this, we need use the command in the terminal:

$ firebase deploy --only functions
or, if your project only use functions, you can use firebase deploy command

This process can to take some time the first time.

After the function is deployed, you can invoke it typing the URL in the browser. The URL will be like this:

https://us-central1-<project-id>.cloudfunctions.net/helloWorld

project-id is your project-id and helloWorld is the function name that we chosen before. If you type it in the browser, you can see the following:

Hello, World!

That’s it! Just we deploy a server function without worries about server configuration, management, SO install, environment, etc… With this serverless architecture, you can focus in your app and in your code.

You can find a similar article in spanish in my blog: carlosazaustre.es

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.