Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Twilio Connector

Twilio is a cloud communications Infrastructure as a Service (IaaS).

MuleSoft maintains this connector under the Select support policy.

About the Twilio Connector

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.

Note: To use the Twilio connector, you must have an active Twilio.com account, either as a Trial or Paid. To create an account, see Try Twilio.

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

You can use this document to understand how to set up and configure a basic flow using the connector. You can track feature additions, compatibility, limitations, and API version updates with each release of the connector using the Connector Release Notes. Review the connector operations and functionality using the Technical Reference with the demo applications.

About Prerequisites

This document assumes that you are familiar with Mule, Anypoint Connectors, and Anypoint Studio. To increase your familiarity with Studio, consider completing a Anypoint Studio Tutorial. This page requires some basic knowledge of Mule Concepts, Elements in a Mule Flow, and Global Elements.

About Hardware and Software Requirements

For hardware and software requirements, visit the Hardware and Software Requirements page.

To Create a New Twilio Account

To create a new Twilio account:

  1. Browse to Try Twilio.

  2. After you log in to your developer account, you see the Twilio dashboard. Note the Account SID and Auth Token values, and copy the credentials to the Twilio connector configuration menu in Anypoint Studio.

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

  3. Click Numbers in the Twilio website navigation to go to the Manage Numbers portion of their website.

  4. Click Verify Numbers, and then click Verify a number.

  5. Enter your cell phone number, and click Call this number. Follow the instructions provided to validate your number. You receive an automated phone call and need to enter an authorization code.

  6. Finally, to send SMS-messages, you need to use the Sandbox from number. This phone number needs to be entered into the Twilio connector configuration menu in Anypoint Studio. This Sandbox number is also located on the Twilio dashboard, with the Account SID and Auth Token.

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

To Install the Connector in Studio

You can install the connector in Anypoint Studio using the instructions in Installing a Connector from Anypoint Exchange.

To Upgrade from an Older Version

If you’re currently using an older version of the connector, a small popup appears in the bottom right corner of Anypoint Studio with an "Updates Available" message.

  1. Click the popup and check for available updates. 

  2. Click the Connector version checkbox and click Next and follow the instructions provided by the user interface. 

  3. Restart Studio when prompted. 

  4. After restarting, when creating a flow and using the connector, if you have several versions of the connector installed, you may be asked which version you would like to use. Choose the version you would like to use.

Additionally, we recommend that you keep Studio up to date with its latest version.

To Configure Global Properties in Studio

To use the Twilio connector in your Mule project, search for "twilio" and drag the connector to your Studio canvas. Click the green plus sign to the right of Connector Configuration and set the fields as shown in this example.

twilio-globalprops

Field Description

Basic Settings:
Name

Name of the connector configuration.

Type: String
Required: Yes
Default: Twilio__Basic_Authentication
XML Parameter: name

Connection:
Username

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

Type: String
Required: Yes
Default: none
XML Parameter: username

Password

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

Type: String
Required: Yes
Default: none
XML Parameter: password

General:
API URL

Twilio API you want to access.

Type: String
Required: Yes
Default: https://api.twilio.com/2010-04-01
XML Parameter: apiUrl

After setting the parameters, click Test Connection to ensure you can reach the Twilio.com API.

About Configuring Operations

You can set the following operations:

  • Get Message List

  • Get Message

  • Send Message

  • Redact Message

  • Delete Message

  • Get Media List

  • Get Media

  • Delete Media

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.

About the Connector’s Namespace and Schema

When designing your application in Studio, the act of dragging the connector from the palette onto the Anypoint Studio canvas automatically populates the XML code with the connector namespace and schema location.

If you are manually coding the Mule application in Studio’s XML editor or other text editor, define the namespace and schema location in the header of your Configuration XML, inside the <mule> tag.

          
       
1
2
3
4
5
6
7
8
9
10
11
12
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:twilio="http://www.mulesoft.org/schema/mule/twilio"
      xsi:schemaLocation="
               http://www.mulesoft.org/schema/mule/core
               http://www.mulesoft.org/schema/mule/core/current/mule.xsd
               http://www.mulesoft.org/schema/mule/twilio
               http://www.mulesoft.org/schema/mule/twilio/current/mule-twilio.xsd">

      <!-- put your global configuration elements and flows here -->

</mule>

Maven Dependency Information

If Maven is backing the application, this XML snippet must be included in your pom.xml file.


          
       
1
2
3
4
5
<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-module-twilio</artifactId>
  <version>2.0.0</version>
</dependency>

Inside the <version> tags, put the desired version number, the word RELEASE for the latest release, or SNAPSHOT for the latest available version.

To Configure Use Cases

The following are common use cases for the Twilio connector:

To Configure Send and Redact Messages

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

twilio-use-case-flow

  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.

  2. Add an empty flow and drag an HTTP endpoint 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
    10
    
    %dw 1.0
    %output application/java
    ---
    {
        body: "You are now subscribed!",
        from: "${fromNumber}",
        to: "+" ++ inboundProperties.'http.uri.params'.toNumber
    } as :object {
        class : "org.mule.modules.twilio.pojo.sendmessagerequest.MessageInput"
    }
  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
    9
    10
    
    %dw 1.0
    %output application/java
    ---
    {
        body: "",
        from: payload.from,
        to: payload.'to'
    } as :object {
        class : "org.mule.modules.twilio.pojo.redactmessagerequest.MessageInput"
    }
  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 an Object to JSON transformer at the end of the flow.

  9. Run the application and point your browser to http://localhost:8081/send/{toNumber}, replacing the toNumber with a valid mobile phone number.

About Connector Performance

To define the pooling profile for the connector manually, access the Pooling Profile tab in the applicable global element for the connector. For background information on pooling, see Tuning Performance.

About Other Resources