Send a text message with Twilio

Create a pipe to send text messages using Twilio.

Overview

This template shows how to create a pipe to send text messages with Twilio. The message and phone numbers are passed as parameters to the pipe, so you can use it as a cloud function for text message notifications.

Code template

Common uses

You can set up a serverless function as a general-purpose tool for sending text messages. Common uses for this include:

  • Adding a text message notification feature to your app
  • Sending a text message notice when a batch process runs
  • Forwarding notifications or messages from other web apps automatically as a text message

How it works

When Flexio.pipe() is called, a local pipe object is created and returned. This pipe object contains information about the pipe, including an array of tasks that will be executed sequentially when the pipe is run.

In this case, a pipe is created and a single request task is added to the pipe that issues a POST request to Twilio’s message API endpoint.

When run() is called on the pipe, the tasks in the pipe object are sent to Flex.io, and the tasks are executed sequentially. Here, the single request task is executed, telling Twilio to send a message.

Adding your own account

To use the pipe with your own account, you’ll need to do the following:

  • Sign up for Twilio and get your Account SID along with an Auth Token
  • Create a connection in Flex.io with your Auth Token
  • Update the request task to use your Account SID and Auth Token

First, sign up for Twilio and get your Account SID and Auth Token in the console. Once you have these, update the url, username and password parameters in the request task in the pipe with your Account SID and Auth Token:

After making these changes, you can send a test message by running the pipe.

Next, you’ll want to hide your Account SID and Auth Token so that you can send messages, but not expose them in code that you check into a repository.

To do this, create a connection in Flex.io by logging into Flex.io, selecting Connections in the left nav bar, then selecting New in the upper right of the connection page. Once you’ve created a new connection, give your connection a name and an alias, then enter the API endpoint with your Account SID 'https://api.twilio.com/2010-04-01/Accounts/{Account SID}/Messages.json' into the url parameter on the connection page. Then select Basic Auth and enter your Account SID for the username and your Auth Token for the password. To finish, press the Create Connection button at the bottom of the page to create the connection.

Finally, once you’ve created a connection, you can use it in the request object by specifying it as the first parameter in the request object using the connection alias and then supplying other parameters:

When the pipe is run, an HTTP request is made using the parameters specified in the connection and any additional parameters in the request function supplementing those defined in the connection.

Extend it

In the template above, the code snippet is an all-in-one example which sends a text message via Twilio, but you can also create a general-purpose version of this operation as a serverless function.

Example 1

To set up a serverless function to send text messages, you can parameterize the code as follows:

Later on, you can run pipe in the following manner: