Firebase SDK for Cloud Functions Quickstart - Firestore

Introduction

This sample app does two things:

  • Creates messages in Firestore using a simple HTTPS request which is handled by an HTTP function. Writing to Firestore is done using the Firebase Admin SDK.
  • When a message gets added in Firestore, a function triggers and automatically makes these messages all uppercase.

Set up the sample

Before you can test the functions locally or deploy to a Firebase project, you'll need to run npm install in the functions directory.

Run locally with the Firebase Emulator suite

The Firebase Local Emulator Suite allows you to build and test apps on your local machine instead of deploying to a Firebase project.

  1. Create a Firebase project in the Firebase Console

    Wondering why this step is needed? Even though the emulator will run this sample on your local machine, it needs to interact with a Firebase project to retrieve some configuration values.

  2. Set up or update the Firebase CLI
  3. Run firebase emulators:start
  4. Open the Emulator Suite UI
    1. Look in the output of the firebase emulators:start command for the URL of the Emulator Suite UI. It defaults to localhost:4000, but may be hosted on a different port on your machine.
    2. Enter that URL in your browser to open the UI.
  5. Trigger the functions
    1. Look in the output of the firebase emulators:start command for the URL of the http function "addMessage". It will look similar to: http://localhost:5001/MY_PROJECT/us-central1/addMessage
      1. MY_PROJECT will be replaced with your project ID
      2. The port may be different on your local machine
    2. Add the query string ?text=uppercaseme to the end of the function's URL. It should now look something like: http://localhost:5001/MY_PROJECT/us-central1/addMessage?text=uppercaseme
      1. Optionally, you can change the message "uppercaseme" to a custom message
    3. Create a new message by opening the URL in a new tab in your browser
  6. View the effects of the functions in the Emulator Suite UI

    1. In the "Logs" tab, you should see new logs indicating that the functions "addMessage" and "makeUppercase" ran:

      functions: Beginning execution of "addMessage"

      functions: Beginning execution of "makeUppercase"

    2. In the "Firestore" tab, you should see a document containing your original message as well as the uppercased version of your message (if it was originally "uppercaseme", you'll see "UPPERCASEME")

Deploy and test on a live Firebase project

To deploy and test the sample:

  1. Create a Firebase project on the Firebase Console
  2. Deploy your project's code using firebase deploy
  3. Create a message by opening the following URL in your browser: https://us-central1-[MY_PROJECT].cloudfunctions.net/addMessage?text=uppercaseme (Replace [MY_PROJECT] by your project ID and you can change the message "uppercaseme").

You should see your text value displayed in the console and uppercase.

Contributing

We'd love that you contribute to the project. Before doing so please read our Contributor guide.

License

© Google, 2016. Licensed under an Apache-2 license.