<dependency>
<groupId>com.mulesoft.connectors</groupId>
<artifactId>mule-twilio-connector</artifactId>
<version>x.x.x</version>
<classifier>mule-plugin</classifier>
</dependency>
Twilio Connector - Mule 4
Twilio Connector v3.0
Anypoint Connector for Twilio (Twilio Connector) is a cloud communications infrastructure as a service (IaaS).
The Twilio platform serves APIs for text messaging, VoIP, and voice calls. Twilio Connector provides connectivity to the Twilio text messaging API.
This connector provides an API for 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.
Release Notes: Twilio Connector Release Notes
Exchange: Twilio Connector
Before You Begin
To use this information, you should be familiar with Twilio, Mule runtime engine (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 software requirements and compatibility information, see the Connector Release Notes.
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.
Add a POM File Dependency
The Apache Maven pom.xml
file generated by Anypoint Studio contains dependencies for a Mule app. If you code a Mule app manually, include this XML snippet in your pom.xml
file to enable access to this connector:
Replace x.x.x
with the version that corresponds to the connector you are using.
To obtain the most up-to-date pom.xml
file information:
-
Go to Anypoint Exchange.
-
In Exchange, click Login and supply your Anypoint Platform username and password.
-
In Exchange, search for
twilio
. -
Select the connector.
-
Click Dependency Snippets near the upper right of the screen.
Create a New Twilio Account
-
Browse to the Try Twilio web site at the
https://www.twilio.com/try-twilio
URL. -
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.
-
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.
-
After you enter the authorization code, Twilio takes you to the main screen.
-
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.
-
Click Phone Numbers > Manage Numbers
-
Click Get Started.
-
Click Get Your First Twilio Phone Number. Twilio lists the number for you. Copy this number for later use.
Connect in Design Center
-
In Design Center, click Create and choose Mule Application.
-
Click a trigger such as an HTTP Listener or the Scheduler trigger.
-
To create an HTTP global element for the connector, set these fields:
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.
-
Select the plus sign to add a component.
-
Select the Twilio connector as a component.
-
Select an operation:
-
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.
Add the Connector to a Studio Project
Anypoint Studio provides two ways to add the connector to your Studio project: from the Exchange button in the Studio taskbar or from the Mule Palette view.
Add the Connector Using Exchange
-
In Studio, create a Mule project.
-
Click the Exchange icon (X) in the upper-left of the Studio task bar.
-
In Exchange, click Login and supply your Anypoint Platform username and password.
-
In Exchange, search for "twilio".
-
Select the connector and click Add to project.
-
Follow the prompts to install the connector.
Add the Connector in Studio
-
In Studio, create a Mule project.
-
In the Mule Palette view, click (X) Search in Exchange.
-
In Add Modules to Project, type "twilio" in the search field.
-
Click this connector’s name in Available modules.
-
Click Add.
-
Click Finish.
Configure in Studio
-
Drag the connector to the Studio Canvas.
-
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.
-
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 |
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, generated after you send a message |
Date Sent |
Date the message was sent from Twilio, in RFC 2822 format |
From |
Phone number or sender ID that initiated the message, in E.164 format |
To |
Phone number that received a message, in E.164 format |
For more information about each format, see the See Also section at the end of this document.
Example
In this example, a Mule app sends a message to a phone number and then redacts it. The example uses variables in place of the Twilio credentials and the phone number from which the SMS is sent.
Preliminary steps
Follow these steps to use property placeholders for field values:
-
Create a new Mule project in Studio.
-
Add the following properties to a YAML or XML file:
config.accountSid=<Your account SID> config.authToken=<Your authentication token> config.fromNumber=<The phone number from which the SMS is sent, as configured inside the Twilio instance>
-
Place the file in the project’s
src/main/resources
folder. -
Reference this file in the connector configuration, as described in Use Property Placeholders for Property Values.
Configure the Mule App
Follow these steps to configure the Mule app for this example:
-
Drag the HTTP Listener operation to the Studio canvas and set its path to
/send/{toNumber}
. -
Drag a Transform Message component to the right of Listener and use DataWeave to prepare the input for the Twilio connector:
%dw 2.0 output application/java --- { Body: "You are now subscribed!", From: p('fromNumber'), To: "+" ++ attributes.uriParams.toNumber as String default "my default value" }
-
Drag the Twilio Send Message operation to the right of the Transform Message component and set the Account Sid field to
${accountSid}
. -
Drag a Set Variable component to the right of the Transform Message component and configure the following fields:
-
Set the Name field to
messageSid
. -
Set the Value field to `#[payload.sid].
-
-
Drag a Transform Message component to the right of Set Variable to create the input for the Redact Message operation:
%dw 2.0 output application/java --- { Body: "", From: payload.from, To: payload.'to' }
-
Drag a Twilio Redact Message operation to the right of the Transform Message component and configure the following fields:
-
Set the Account Sid field to
${accountSid}
. -
Set the Message Sid field to
#[messageSid]
, which is the variable stored in step 5.
-
-
Drag a Transform Message component to the right of Redact Message and set the output to
application/json
:%dw 2.0 output application/json --- payload
-
Run the application and point your browser to
http://localhost:8081/send/{toNumber}
, replacing thetoNumber
with the phone number obtained from Twilio.