Create and Deploy Your First Cloud Functions

This tutorial describes how to write and deploy Cloud Functions by walking through a sample. The sample stores and transforms messages in the Realtime Database using two separate but related functions:

  • addMessage(), which exposes a URL that accepts a text value and writes it to the Realtime Database.
  • makeUppercase(), which triggers on the Realtime Database write and transforms the text to uppercase.

We've chosen the Realtime Database and HTTP-triggered functions for this sample, but you have many more options for triggering functions. See the how-to guides for authentication events, analtyics events, and more.

Build the sample in your Firebase project

The following sections of this tutorial detail the steps required to build and deploy the sample.

Set up Firebase SDK for Cloud Functions

First, complete the setup steps described in Get started with Cloud Functions.

Once you have completed the setup and initialized your project, you can open myproject/functions/index.js and start adding code as described in the following sections.

Import the required modules and initialize

For this sample, your project must import the Functions and Admin SDK modules using Node require statements. Add lines like the following to your index.js file:

These lines load the firebase-functions and firebase-admin modules, and initialize an admin app instance from which Realtime Database changes can be made.

The Firebase CLI automatically installs the Firebase and Firebase SDK for Cloud Functions Node modules when you initialize your project. To add 3rd party libraries to your project, you can modify your functions/package.json, run npm install, and require as you normally would. For more information, see Handle Dependencies.

Add the addMessage() function

For the addMessage() function, add these lines to index.js:

The addMessage() function is an HTTP endpoint. Any request to the endpoint results in ExpressJS-style Request and Response objects passed to the onRequest() callback.

HTTP functions are synchronous, so you should send a response as quickly as possible and defer work using the Realtime Database. The addMessage() HTTP function passes a text value to the HTTP endpoint and inserts it into the Realtime Database under the path /messages/:pushId/original using the previously initialized admin app.

Deploy and execute addMessage()

To deploy and execute the addMessage() function, follow these steps:

  1. Run this command to deploy your functions:

    $ firebase deploy --only functions
    

    After you deploy, the Firebase CLI outputs the URL for any HTTP function endpoints. In your terminal, you should see a line like the following:

    Function URL (addMessage): https://us-central1-MY_PROJECT.cloudfunctions.net/addMessage
    
  2. Add a text query parameter to the addMessage() URL, and open it in a browser:

    https://us-central1-MY_PROJECT.cloudfunctions.net/addMessage?text=uppercaseme
    

    The function executes and redirects the browser to the Firebase console at the database location where the text string is stored. You should see your text value displayed in the console.

After deploying and executing functions, you can view logs in the Firebase console.

Add the makeUppercase() function

For the makeUppercase() function, add these lines to index.js:

The makeUppercase() function executes when the Realtime Database is written to. The ref(path) function defines the part of the database to listen on. For performance reasons, you should be as specific as possible.

Braces — for example, {pushId} — surround "parameters," wildcards that expose their matched data in the callback.

The Realtime Database triggers the onWrite() callback whenever data is written or updated on the given path.

Event-driven Cloud Functions such as Realtime Database events are asynchronous. The callback function should return either a null, an Object, or a Promise. If you do not return anything, the function times out, signaling an error, and is retried. See Sync, Async and Promises.

Deploy and execute makeUppercase()

To complete the tutorial, deploy your functions again, and then execute addMessage() to trigger makeUppercase().

  1. Run this command to deploy your functions:

    $ firebase deploy --only functions
    
  2. Using the addMessage() URL output by the CLI, add a text query parameter, and open it in a browser:

    https://us-central1-MY_PROJECT.cloudfunctions.net/addMessage?text=uppercasemetoo
    

    The function executes and redirects the browser to the Firebase console at the database location where the text string is stored. This write event triggers makeUppercase(), which writes an uppercased version of the string.

After deploying and executing functions, you can view logs in the Firebase Console for Cloud Functions.

Review complete sample code

Here's the completed functions/index.js containing the functions addMessage() and makeUppercase(). These functions allow you to pass a parameter to an HTTP endpoint that writes a value to the Realtime Database, and then transforms it by uppercasing all characters in the string.

Next steps

In this documentation you can find more information on Cloud Functions general concepts, as well as guides for writing functions to handle the event types supported by Cloud Functions.

To learn more about Cloud Functions, you could also do the following:

Send feedback about...

Need help? Visit our support page.