Over a million developers have joined DZone.

How to Use Twilio Connector in Mule 3.8

DZone's Guide to

How to Use Twilio Connector in Mule 3.8

In this post, you'll learn how to use a Twilio API to send a receive messages in your Mule application. Read on for more!

· Integration Zone ·
Free Resource

Continue to drive demand for API management solutions that address the entire API life cycle and bridge the gap to microservices adoption.  

Hello everyone! Today, in this tutorial, we are going to look at how to use the Twilio Connector in Mule 3.8. I am going to send the number to which the Twilio Connector sends a Message.

Before you use the Twilio connector, you must have an active Twilio Account. If you don’t have an account, click here.

  • Once you have successfully logged in, you will be routed to Programmable SMS/Learn & Build. Click on Get Twilio Number, as shown below:Image title
  • Then you will be assigned a Twilio Number to which you can send text messages.
  • Now, go to tje Dashboard. There you can find the Account SID and Auth Token values as shown below:

Image title

  • Since you have registered for a free Developer account, you need to verify a phone number before you send text messages to it.
  • Now, click on the All Products and Services tab, and, under Super Network, click on the Phone Numbers tab, as shown below:

Image title

  • In the Phone Numbers tab, click on Verified Called IDs, as shown below:

Image title

  • Under the Verified Caller IDs tab, Add a Number to which the text messages will be sent, as shown below:

Image title

Twilio Connector Installation:

  • 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.
  • After the installation, restart Anypoint Studio. Then, find the Twilio Connector in the Mule Palette.

About 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.

According to MuleSoft's documentation, "The Anypoint Connector for Twilio provides connectivity to the Twilio platform, which serves APIs for text messaging, VoIP, and voice calls."

Here is the complete flow:

Image title

  • Drag and drop an HTTP Listener Connector from the Mule Palette on to the design canvas and configure the HTTP Listener Port, as shown below:

Image title

  • Provide the path for the listener, as shown below:

Image title

  • Now, drag and drop the Twilio Connector from the Mule Palette on to the Design Canvas and click on Add Configuration. Provide the Account SID and Auth Token values which are  available in the Twilio Account Dashboard, as shown earlierImage title
  • Since I have defined the Account SID and Auth Token values in the property file I have used the ${AccountSID} and ${AuthToken}. Next, click on Test Connection. The test connection should be successful, as shown below:

Image title

  • Now, select the Send Message operation and also provide the Account SID in the general tab, as shown below:Image title
  • Then, drag and drop a transform message from the Mule Palette on to the Design Canvas in between the HTTP Listener and Twilio Connector. The Twilio Connector expects the Body, fromNumber and toNumber parameters to be passed. Here, toNumber is passed from the URI parameter from the HTTP listener and fromNumber is the Twilio Number that got assigned to us in the Twilio Account. Body is where I have written the message we'll send. I have passed the values in Data Weave code, as shown below:

Image title

Here is the code snippet:

%dw 1.0
%output application/java
    body: "Welcome to MuleSoft Messaging Via Twilio",
    from: "${fromNumber}",
    to: "+" ++ inboundProperties.'http.uri.params'.toNumber
} as :object {
    class : "org.mule.modules.twilio.pojo.sendmessagerequest.MessageInput"
  • Since the output data comes in the format of an object from Twilio, let’s drag and drop the Object to a JSON transformer from the Mule Palette on to the Design Canvas after the Twilio Connector.
  • Now save the application and right click on Design Canvas. Next, click on 'Run Application' and the application will get deployed successfully.
  • Now go to Postman and type localhost:8090/send/91**********. Remember, don’t forget to provide the 91 before the 10-digit mobile number and make sure the fromNumber is the correct Twilio number and toNumber is the verified caller ID number. Then, click on Send Request as shown below:

Image title

  • You can see the below output from the Twilio Connector in Postman.

Image title

  • You can also see that the message that you set in the body, in Transform Message, is received by the mobile number you specified:

Image title

Here is the source code:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:context="http://www.springframework.org/schema/context" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:twilio="http://www.mulesoft.org/schema/mule/twilio" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/twilio http://www.mulesoft.org/schema/mule/twilio/current/mule-twilio.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="" port="8090" doc:name="HTTP Listener Configuration"/>
    <twilio:config name="Twilio__Basic_Authentication" username="${AccountSID}" password="${AuthToken}" doc:name="Twilio: Basic Authentication"/>
    <context:property-placeholder location="number.properties"/>
    <flow name="twilio_demoFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/send/{toNumber}" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
    body: "Welcome to MuleSoft Messaging Via Twilio",
    from: "${fromNumber}",
    to: "+" ++ inboundProperties.'http.uri.params'.toNumber
} as :object {
    class : "org.mule.modules.twilio.pojo.sendmessagerequest.MessageInput"
        <twilio:send-message config-ref="Twilio__Basic_Authentication" AccountSid="${AccountSID}" doc:name="Twilio"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>


Hope this helps!

As always, feel free to leave any feedback in the comments.

Thank you.

Discover how organizations are modernizing their application architectures for speed and agility from the growing API economy

mule ,twilio ,twilio functions ,anypoint connector ,integration ,api integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}