Skip to contentSkip to navigationSkip to topbar
On this page

Send Messages with Twilio


The Send Messages with Twilio Firebase extension(link takes you to an external page) will automatically send messages (SMS or WhatsApp) using the Twilio Programmable Messaging API based on information from documents added to a specified Cloud Firestore collection. The extension will also record the delivery status of each message.

Adding a document triggers this extension to send a message built from the document's fields. The document's fields specify who to send the message to and the body of the message and can optionally define the number to send the message from.

Here's an example document that would trigger this extension:

1
admin.firestore().collection('messages').add({
2
to: '+15551234567',
3
body: 'Hello from Firebase!'
4
});

Installation

installation page anchor

You can install the extension using either the Firebase console or the Firebase CLI. To install or manage extensions, you must be assigned either the Owner, Editor or Firebase Admin role. Your Firebase project must also be on the Blaze (pay as you go plan)(link takes you to an external page).

Pre-installation

pre-installation page anchor

Before you install the extension you will need to do the following:

  1. If you don't already have a Twilio account, sign up for a free account(link takes you to an external page)

  2. Find your Twilio Account Sid and Auth Token on your Twilio console(link takes you to an external page)

  3. Ensure you have a Twilio Phone number(link takes you to an external page) ready to use with the extension

    If you don't have a Twilio number that can send messages, you can buy a new number(link takes you to an external page).

  4. (Optional) Set up a Messaging Service(link takes you to an external page)

    A Messaging Service is a higher-level "bundling" of messaging functionality around a common set of senders, features, and configuration. The same settings and feature configuration applies to all of the senders (long code numbers, short codes, toll-free numbers, etc.) in the Messaging Service's pool. You can use a Messaging Service with this extension to send messages from your number pool.

  5. Create a collection in Firestore for your messages

    This extension listens for new documents in a Cloud Firestore collection that you specify. When it finds a new document, the extension sends a message based on the document's fields. You can use any Cloud Firestore collection for this purpose.

  6. Set up security rules for your email documents collection.

    This extension can be used to trigger message delivery directly from client applications. However, you should carefully control client access to the collection to avoid potential abuse (you don't want users able to send arbitrary messages from your company's phone number or WhatsApp account!).

    Security rules will vary from application to application, but you should always make sure that messages are sent only to intended recipients and free-form content is kept to a minimum.

Installation via the Firebase CLI

installation-via-the-firebase-cli page anchor

Make sure you have the latest version of the Firebase CLI and you have your Firebase project ID or alias to hand.

Run this command to install the extension:

firebase ext:install twilio/send-message --project=projectId-or-alias

The command will prompt you for details like your Account Sid and Auth Token, a Twilio phone number or a Messaging Service Sid (note: if you provide both, the Messaging Service will be preferred), and the name of a the Cloud Firestore collection you want to use.

Once installation is complete, check out the instructions below for how to use the extension.

Installation via the Firebase console

installation-via-the-firebase-console page anchor

Visit the Firebase Extensions directory(link takes you to an external page) to find the Send Messages with Twilio extension, or install the extension directly here(link takes you to an external page). Click Install and follow the on-screen instructions to install the extension. The installation process will prompt you for the details you collected earlier, including your Account Sid, Auth Token, Twilio phone number or Messaging Service Sid and the name of your Cloud Firestore collection.


After installation, this extension monitors all document writes to the collection you configured.

The extension accepts document writes with the following fields:

Field NameRequired?Description
ToYesThe phone number or WhatsApp number you want to send the message to. Phone numbers should be in e.164 format, and WhatsApp messages require the whatsapp: prefix.
BodyYesThe body of the message.
FromNoA Twilio phone number or Messaging Service you want to use to send the message. Overrides the from number or Messaging Service set in the extension settings

Messages are delivered based on the contents of the document's fields.

Example: Send an SMS message

example-send-an-sms-message page anchor

To send an SMS message, add a document to your messages collection with a to field and a body field with the following content:

1
{
2
to: '+15551234567',
3
body: 'Hello from Firebase!'
4
}

The message will be sent from the number or Messaging Service you configured in the extension.

You can send from a different Twilio phone number or Messaging Service by providing a from field in the document.

Need further assistance?

On GitHub, you can take a look at the source code, or read through the existing issues to see if somebody else had a similar problem. If you're still stuck, feel free to create a pull request or open an issue yourself!

Check out the GitHub Repo(link takes you to an external page)

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.