Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Twilio Connector

Select

Twilio is a cloud communications infrastructure as a service (IaaS).

This connector provides an API for making and receiving telephone calls, and sending and receiving text messages. To get started with Twilio, follow the steps below to gain access to their free sandbox service to send SMS text-messages. You can configure the Twilio connector in Anypoint Studio with your API credentials.

The Anypoint Connector for Twilio provides connectivity to the Twilio platform, which serves APIs for text messaging, VoIP, and voice calls.

Prerequisites

This document assumes that you are familiar with Twilio, Mule, Anypoint Connectors, Anypoint Studio, Mule concepts, elements in a Mule flow, and Global Elements.

You need login credentials to test your connection to your target resource.

For hardware and software requirements and compatibility information, see the Connector Release Notes.

To use this connector with Maven, view the pom.xml dependency information in the Dependency Snippets in Anypoint Exchange.

Note: To use the Twilio connector, you must have an active Twilio.com account, either as a Trial or Paid. To create a Twilio account, browse to the Try Twilio web site at the https://www.twilio.com/try-twilio URL.

What’s New in this Connector

Support for Mule 4 has been added.

To Create a New Twilio Account

To create a new Twilio account:

  1. Browse to the Try Twilio web site at the https://www.twilio.com/try-twilio URL.

  2. Sign up and click Get Started.

    With a free developer account, you need to verify your SMS-enabled phone before you can send text messages to it.

  3. When prompted, enter your cell phone number. Follow the instructions provided to validate your number. You receive an automated text message and an authorization code.

  4. After you enter the authorization code, Twilio takes you to the main screen.

  5. Note the Account SID and Auth Token values, and copy the credentials for later use in the Twilio connector configuration menu in Anypoint Studio.

    Tip: As you copy fields from the Twilio website to the Anypoint Studio connector configuration, be sure to not copy in additional leading and trailing characters or spaces. It is a good idea to visually confirm that your copy and paste functions did not capture surrounding characters.

  6. Click Phone Numbers > Manage Numbers

  7. Click Get Started.

  8. Click Get Your First Twilio Phone Number. Twilio lists the number for you. Copy this number for later use.

To Connect in Design Center

  1. In Design Center, click Create and choose Mule Application.

  2. Click a trigger. You can create a global element by selecting this connector as their trigger. If a global element is not needed, you can use an HTTP Listener or Scheduler trigger.

    Trigger options

  3. To create an HTTP global element for the connector, set these fields:

    HTTP Listener configuration

    Field Description

    Protocol

    Protocol selected for the HTTP connector, it can be HTTP or HTTPS (secure).

    Host

    IP address where your Mule application listens for requests.

    Port

    Port address where your Mule application listens for requests.

    Base Path

    Path where your Mule application listens for requests.

  4. Select the plus sign to add a component.

    add connector

  5. Select the connector as a component.

  6. Select an operation:

    Operation Connector

  7. Configure the Global element for the connector

    config connector

    Field Description

    Protocol

    Protocol selected for the HTTP connector, it can be HTTP or HTTPS (secure).

    Host

    IP address where your Mule application listens for requests.

    Port

    Port address where your Mule application listens for requests.

    Base Path

    Path where your Mule application listens for requests.

    Username

    Your username (Account SID) to access your Twilio API.

    Password

    Your password (Auth Token) to access your Twilio API.

To Install this Connector in Studio 7

  1. In Anypoint Studio, click the Exchange icon in the Studio taskbar.

  2. Click Login in Anypoint Exchange.

  3. Search for the connector and click Install.

  4. Follow the prompts to install the connector.

When Studio has an update, a message displays in the lower right corner, which you can click to install the update.

Configure in Studio

  1. Drag and drop the connector to the Studio Canvas.

  2. Configure the Global element for the connector.

    Field Description

    Protocol

    Protocol selected for the HTTP connector, it can be HTTP or HTTPS (secure).

    Host

    IP address where your Mule Application listens for requests.

    Port

    Port address where your Mule Application listens for requests.

    Base Path

    Path where your Mule Application listens for requests.

    Username

    Your username (Account SID) to access your Twilio API.

    Password

    Your password (Auth Token) to access your Twilio API.

  3. Select an operation:

    • Delete Media

    • Delete Message

    • Get Media

    • Get Media List

    • Get Message

    • Get Message List

    • Redact Message

    • Send Message

These fields can accompany an operation:

Field Description

Account Sid

Enter the Account SID to connect to Twilio. The unique ID of the Account that sent this message.

Date Created

When the resource was created.

Media Sid

Unique ID for the media.

Message Sid

Unique ID for the message. This ID is generated after you send a message.

Date Sent

Date a message was sent from Twilio. In RFC 2822 format.

From

Phone number or sender ID that initiated a message. The number or ID is in E.164 format.

To

Phone number that received a message. The number is in E.164 format.

Entity Reference

MEL expression for what you want to view, such as the payload.

For more information on each format, see the See Also section at the end of this document.

Use Case: Studio

In the following example, a Mule application sends a message to a phone number, and then redacts it.

  1. Create a new Mule application and add the following properties to the mule-app.properties file:

    Property Description

    accountSid

    Your Account SID.

    authToken

    Your Authentication Token.

    fromNumber

    The phone number from where SMS is to be sent. This is configured inside the Twilio instance. This is the phone number you received from Twilio.

  2. Add an empty flow and drag an HTTP connector to the inbound part of the flow. Set its path to /send/{toNumber}.

  3. Drag a Transform Message at the flow and prepare the input for the Twilio connector:

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    %dw 2.0
    output application/json
    ---
    {
        body: "You are now subscribed!",
        from: "${fromNumber}",
        to: "+" ++ inboundProperties.'http.uri.params'.toNumber
    
    }
  4. Add a Twilio Connector after the Transform Message and apply the following settings:

    • Select the Send Message operation.

    • Set Account Sid to ${accountSid}, and Entity Reference to #[payload].

  5. Drag a Variable component and configure the following parameters:

    • Set Name to messageSid.

    • Set Value to #[payload.getSid()].

  6. Add another Transform Message to create the input for the Redact Message operation:

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    
    %dw 2.0
    output application/json
    ---
    {
        body: "",
        from: payload.from,
        to: payload.'to'
    }
  7. Drag a Twilio Connector after the Transform Message and apply the following settings:

    • Select the Redact Message operation.

    • Set Account Sid to ${accountSid}.

    • Set Message Sid to #[messageSid] (this is the variable we stored two steps above).

    • Set Entity Reference to #[payload].

  8. Put Transform Message at the end of the flow.

  9. Run the application and point your browser to http://localhost:8081/send/{toNumber}, replacing the toNumber with the phone number that you got from Twilio.

See Also