Menu

Expand
Rate this page:

Send Messages with Twilio

The Send Messages with Twilio Firebase extension 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:

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

Installation

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).

Pre-installation

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
  2. Find your Twilio Account Sid and Auth Token on your Twilio console
  3. Ensure you have a Twilio Phone number ready to use with the extension

    If you don't have a Twilio number that can send messages, you can buy a new number.

  4. (Optional) Set up a Messaging Service

    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

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

Visit the Firebase Extensions directory to find the Send Messages with Twilio extension, or install the extension directly here. 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.

Usage

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

The extension accepts document writes with the following fields:

Field Name Required? Description
To Yes The phone number or WhatsApp number you want to send the message to. Phone numbers should be in e.164 format.
Body Yes The body of the message.
From No A 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

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

{
  to: '+15551234567',
  body: 'Hello from Firebase!'
}

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?

Check out the GitHub Repo

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!

Rate this page:
        
        
        

        Thank you for your feedback!

        Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more