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.

POM File Information

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

Mule converts RELEASE to the latest version. To specify a version, view Anypoint Exchange and click Dependency Snippets.

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.

Use Case: Studio

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

This example uses variables for field values. You can provide actual values for each variable by using a properties file. Place the file at /src/main/resources and use the tag <configuration-properties> to point to the file. For more information, see Properties Files.

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

    Property Description

    accountSid

    Your account SID

    authToken

    Your authentication token

    fromNumber

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

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

  3. Drag a Transform message component to the flow and prepare the input for the Twilio connector:

    %dw 2.0
    output application/java
    ---
    {
        Body: "You are now subscribed!",
        From: "${fromNumber}",
        To: "+" ++ attributes.uriParams.toNumber default "my default value"
    }
  4. Add a Twilio Connector after the Transform message component and apply the following settings:

    • Select the Send Message operation.

    • Set Account Sid to ${accountSid}.

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

    • Set Name to messageSid.

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

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

    %dw 2.0
    output application/java
    ---
    {
        Body: "",
        From: payload.from,
        To: payload.'to'
    }
  7. Drag a Twilio Connector after the Transform message component 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).

  8. Put a Transform message component 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.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.