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
The Twilio platform serves APIs for text messaging, VoIP, and voice calls. The Anypoint Connector for Twilio provides connectivity to the Twilio text messaging API.
Anypoint Connector for Twilio (Twilio Connector) provides an API for sending and receiving text messages. To get started with Twilio, follow the steps below to gain access to the free Twilio 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:
-
Browse to Try Twilio.
-
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.
-
Click Numbers in the Twilio website navigation to go to the Manage Numbers portion of their website.
-
Click Verify Numbers, and then click Verify a number.
-
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.
-
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 this Connector
-
In Anypoint Studio, click the Exchange icon in the Studio taskbar.
-
Click Login in Anypoint Exchange.
-
Search for the connector and click Install.
-
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.
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.
Field | Description |
---|---|
Basic Settings: |
Name of the connector configuration. Type: String |
Connection: |
Your username (Account SID) to access your Twilio API. Type: String |
Password |
Your password (Auth Token) to access your Twilio API. Type: String |
General: |
Twilio API you want to access. Type: String |
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.
-
Namespace:
http://www.mulesoft.org/schema/mule/twilio
-
Schema Location:
http://www.mulesoft.org/schema/mule/connector/current/mule-twilio.xsd
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.
|
<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.
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-twilio</artifactId>
<version>x.y.z</version>
</dependency>
Replace x.y.z
with the version that corresponds to the connector you are using.
To obtain the most up-to-date pom.xml
file information, access the connector in Anypoint Exchange and click Dependency Snippets.
Send and Redact Messages
In the following example, a Mule application sends a message to a phone number, and then redacts it.
-
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.
-
Add an empty flow and drag an HTTP endpoint to the inbound part of the flow. Set its path to
/send/{toNumber}
. -
Drag a Transform Message at the flow and prepare the input for the Twilio connector:
%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" }
-
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]
.
-
-
Drag a Variable component and configure the following parameters:
-
Set Name to
messageSid
. -
Set Value to
#[payload.getSid()]
.
-
-
Add another Transform Message to create the input for the Redact Message operation:
%dw 1.0 %output application/java --- { body: "", from: payload.from, to: payload.'to' } as :object { class : "org.mule.modules.twilio.pojo.redactmessagerequest.MessageInput" }
-
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]
.
-
-
Put an Object to JSON transformer at the end of the flow.
-
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.
See Also
-
Access the Twilio Connector Release Notes.
-
Visit Twilio’s official REST API Reference.