Contact Free trial Login

Twilio Connector - Mule 4

Support Category: Select

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.

Prerequisites

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:

<dependency>
  <groupId>com.mulesoft.connectors</groupId>
  <artifactId>mule-twilio-connector</artifactId>
  <version>x.x.x</version>
  <classifier>mule-plugin</classifier>
</dependency>

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:

  1. Go to Anypoint Exchange.

  2. In Exchange, click Login and supply your Anypoint Platform username and password.

  3. In Exchange, search for twilio.

  4. Select the connector.

  5. Click Dependency Snippets near the upper right of the screen.

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.

Connect in Design Center

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

  2. Click a trigger such as an HTTP Listener or the 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 Twilio 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.

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

  1. In Studio, create a Mule project.

  2. Click the Exchange icon (X) in the upper-left of the Studio task bar.

  3. In Exchange, click Login and supply your Anypoint Platform username and password.

  4. In Exchange, search for "twilio".

  5. Select the connector and click Add to project.

  6. Follow the prompts to install the connector.

Add the Connector in Studio

  1. In Studio, create a Mule project.

  2. In the Mule Palette view, click (X) Search in Exchange.

  3. In Add Modules to Project, type "twilio" in the search field.

  4. Click this connector’s name in Available modules.

  5. Click Add.

  6. Click Finish.

Configure in Studio

  1. Drag 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

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:

  1. Create a new Mule project in Studio.

  2. 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>
  3. Place the file in the project’s src/main/resources folder.

  4. 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:

  1. Drag the HTTP Listener operation to the Studio canvas and set its path to /send/{toNumber}.

  2. 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"
    }
  3. Drag the Twilio Send Message operation to the right of the Transform Message component and set the Account Sid field to ${accountSid}.

  4. 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].

  5. 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'
    }
  6. 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.

  7. 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
  8. Run the application and point your browser to http://localhost:8081/send/{toNumber}, replacing the toNumber with the phone number obtained from Twilio.