Twilio Service

Twilio provides messaging, voice, and chat services for web and mobile apps. MongoDB Stitch supports integrating apps with Twilio’s Programmable SMS service.

You will need the following to integrate Twilio with your MongoDB Stitch app:

  • Twilio account SID
  • Twilio authorization token
  • Twilio phone number

You will also need a MongoDB Stitch incoming webhook to receive requests from Twilio. However, your app may send messages to Twilio without a webhook.

Add a Twilio Service to Your MongoDB Stitch App

Use the following procedure to add a Twilio service to your MongoDB Stitch app:

A. Get Twilio Account Info


Login to Twilio.

Go to Twilio and either:

  • Create a new account and your Twilio project, or
  • Log in with your existing credentials and select an existing Twilio project or create a new Twilio project.

Get Accout SID and Auth Token.

From you project’s dashboard, retrieve the:

  • Account SID, and
  • Auth Token.

B. Add Twilio Service to the MongoDB Stitch App


Go to the MongoDB Atlas project associated with your MongoDB Stitch application.


If you have not set up your cluster or set up your MongoDB Stitch application, see Getting Started.

  1. Log into Atlas.
  2. Navigate to the project associated with your MongoDB Stitch application.
  3. Select Stitch Apps in the left-hand nav panel.
  4. Select the application you wish to integrate with the Twilio Service. You will be redirected to the MongoDB Stitch console.

Add the Twilio service.

  1. In the MongoDB Stitch console, click Add service in the left-hand nav panel.
  2. Select Twilio.
  3. Enter a name for your Twilio service.
  4. Click Add service

C. Configure the Twilio Service


Click Config for your added service.


Enter your Twilio Accout SID and Auth Token.

Enter the Account SID and Auth Token with the values retrieved in an earlier step.


Click Save.

D. Create a MongoDB Stitch Incoming Webhook

A MongoDB Stitch incoming webhook is a callback URL used by a third-party service, such as Twilio, to execute a MongoDB Stitch function. MongoDB Stitch functions provide developers the flexibility to concatenate multiple services in the same app. A function defines one or more stages, where each stage consists of the following:

  • A MongoDB Stitch service defined for the app
  • An action for the specified service
  • Arguments to pass to the service

Use the following procedure to create a MongoDB Stitch incoming webhook for your app:


Click the Incoming Webhooks tab for your added Twilio service.


Click New Webhook.


Enter the following properties for the webhook.

Property Action
Name Enter the name for your new webhook.
Respond with Result Optional. Switch to enabled to send responses to URLs that invoke the incoming webhook.
Output Type

Select the output type for the webhook function:

  • Select Array to return all documents from the function.
  • Select Single Document to return a single document from the function.
  • Select Boolean to return a boolean value indicating whether the function returned any output.

Specify the webhook function.

Use the function editor to write the webhook function code.

Example: The following webhook function inserts incoming data into a MongoDB collection.

exports = function(args) {
    var mdb ="mongodb-atlas");
    var coll = mdb.db("test").collection("requestlogs");
        "body": args.Body,
        "from": args.From

The args document is passed by the Twilio service and contains information from the request.


Click Create.

Upon successful creation of the webhook, its Webhook URL is displayed.

E. Create a Twilio Messaging Service

Use the following procedure to add your MongoDB Stitch incoming webhook to your Twilio Programmable SMS messaging service:


Login to Twilio.


Go to the Programmable SMS Dashboard.

In the left-hand navigation pane for your Twilio project, click the ellipses icon and Programmable SMS.


Create a Messaging Service.

  1. If this is the first messaging service for your Twilio account, click Get Started to first get a Twilio phone number.
  2. Once you have your number, click Messaging Services.
  3. If this is your first messaging service, click Create new Messaging Service. Otherwise, click the plus sign to create a new messaging service.

#. Enter the name and use case to create the service. Once created, you will be redirected to the service configuration page.

  1. In the configuration page, check the PROCESS INBOUND MESSAGES box.
  2. Enter the MongoDB Stitch incoming webhook URL in the Request URL box.
  3. Click Save.

Your MongoDB Stitch app is now integrated with Twilio’s SMS messaging service. Send a message to your Twilio phone number to invoke the incoming webhook for your MongoDB Stitch app.

For an example, see Integrate ToDo with Twilio.

Functions and Twilio Service

With MongoDB Stitch, you can define a sequence of actions to perform as a function of stages. For the Twilio service, MongoDB Stitch provides the following actions:

Action Description
twilioService.send() Sends a text message to a specified phone number.


A function can consist of stages that use different MongoDB Stitch services.