Email via Firebase Firestore Cloud Function Triggers

By Ron Royston

May 2018

Table of Contents


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.


Firebase Account w Billing Enabled
Sendgrid Account
Firestore Enabled
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

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";

Dialog Title

Cell Phone Number

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

Delete location?

A password reset link will be emailed to you.

New users register here for an email authenticated account.

Forget your password? Click here to have a new one emailed to you.

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.