Firebase Functions - Trigger Email


By Ronnie Royston

Originally Published 5/21/2018
Updated 5/21/2018


This article gets you up and running as quickly and easily as possible. We are going to trigger an email to a 3rd party when a new sale document is added to a sales collection in our Firestore database. The idea is to notify the customer (the 3rd party) that their purchase has been initiated. Sendgrid will send the email as if it came from your domain and it will be prepopulated with arguments that are passed to sendgrid by Cloud Functions. So, let's jump right in.


  1. Firebase Account w Billing Enabled
  2. Sendgrid Account
  3. Firestore Enabled
  4. Node.js and Node Package Manager, NPM, Installed

Install Required Firebase SDK's and Command Line Tools

From your root directory:

npm install firebase-functions@latest --save
npm install firebase-admin@latest --save
npm install -g firebase-tools
firebase init functions

Now cd functions and from your functions directory:

npm install @sendgrid/mail --save

Setup Sendgrid

Get your Sendgrid API key from your dashboard on their website. Save your sendgrid API key in the cloud functions environment by issuing the following command from the functions directory.

firebase functions:config:set sendgrid.key=YOUR_API_KEY

Back on the sendgrid dashboard, whitelist your app's domain and update your DNS server per sendgrid instructions (optional but recommended). Create a simple transactional template with the following text

Hello {{name}}, we are processing your order.

Make note of your template id, you will need it in the next step.

Create Your Firebase Cloud Function Script

The script below uses the 1.0v SDK. For the updated documentation, see Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0

var functions = require('firebase-functions');
var sendgrid = require("@sendgrid/mail");
var admin = require('firebase-admin');
var SENDGRID_API_KEY = functions.config().sendgrid.key;
exports.newSale = functions.firestore.document("sales/{sale}").onCreate(function (snap, context) {
    var sale =;
    var msg = {
        from: "",
        subject: "New Order",
        templateId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        substitutionWrappers: ["{{", "}}"],
        substitutions: {
            name: sale.displayName
    return sendgrid.send(msg);

Wrapping Up and Testing

Deploy to Firebase.

firebase deploy

To test, create a new sale document. This document must be created in a "sales" collection at the root of your Firestore database. If you are working with a web app, from a browser console create the doc like below:

var o = {}; = "";
o.displayName = "Bob Customer";

Automate a workflow or process today with standards based, library free JavaScript in the browser, in the cloud with NodeJS, or both. High Tekk can do that! See our services page for more details.

Dialog Title

Cell Phone Number

Your order ID is . The grand total is , or bitcoin.

This action cannot be undone. Continue?

Changes will be lost. Continue?

Delete location?

A signin link will be sent to your email address.

You privacy is top priority. Email addresses are never shared with a third party. For more information see our privacy policy.

Find a bug? Experience an error? How can we do better? We appreciate your feedback.

Delete order? This action cannot be undone.

Delete order? This action cannot be undone.

This action cannot be undone. Continue?