SMS from Salesforce Using Twilio API

Twilio is a cloud communication company that allows software developers to programmatically make and receive phone calls, and send and receive text messages using its web service APIs

Twilio API

Twilio is a cloud communication company that allows software developers to programmatically make and receive phone calls, and send and receive text messages using its web service APIs

In this article, I have explained the steps for sending an SMS from Salesforce to mobile phones.

Step1: Twilio API Credentials

First, create a trial account in and then get the API Credentials for making the API calls, and also create a phone number for your account from which you can send the SMS. In the trial account, you can send the SMS to only the verified numbers; so, you must verify all the phone numbers you are going to send the SMS.

Step 2: Remote Site Settings

Create a Remote Site Settings in Salesforce for the below URL

Step 3: Create an Apex class (Sendsms)

In this class, we need to replace the value of the AccountSid variable with the ACCOUNT SID value, also replace the value of the token variable with the AUTH TOKEN, and replace the value of the fromPhNumber variable with the From phone number you got from the Twilio Account.

public class Sendsms


   public String phNumber{get;set;}

   public String smsBody{get;set;}

   String accountSid;

   string token;

   String fromPhNumber;

   errorResponseWrapper erw;

   public sendsms(){

    phNumber ='+'+Apexpages.currentpage().getparameters().

       accountSid = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';

       token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

       fromPhNumber = 'xxxxxxxx';


 public void processSms(){

       HttpRequest req = new HttpRequest();



       String VERSION  = '3.2.0';

       req.setHeader('X-Twilio-Client', 'salesforce-' + VERSION);

       req.setHeader('User-Agent', 'twilio-salesforce/' + VERSION);

       req.setHeader('Accept', 'application/json');

       req.setHeader('Accept-Charset', 'utf-8');

       req.setHeader('Authorization','Basic '+
(accountSid+':' +token)));


       Http http = new Http();

       HTTPResponse res = http.send(req);



           ApexPages.addmessage(new ApexPages.message
(ApexPages.severity.INFO,'SMS Sent Successfully'));


           erw =(errorResponseWrapper)json.deserialize

           ApexPages.addmessage(new ApexPages.message



   public class errorResponseWrapper{

       String code;

       String message;

       String moreInfo;

       String status;    



Step 4: Create a Visualforce Page (To send the SMS)

<apex:page controller="Sendsms" sidebar="false" showHeader="false" title="Send SMS">

<apex:pagemessages />

<apex:form >


<center><b>Send SMS</b> <br/><br/>

<b>To</b> &nbsp;&nbsp;&nbsp;&nbsp;<apex:inputtext value="{!phNumber}"/> <br/><br/><br/>

<b>Body</b>&nbsp;<apex:inputtext value="{!smsBody}"  /> &nbsp;<br/>(160 Char Max)<br/><br/><br/>

<apex:commandButton value="Send Sms" action="{!processSms}"/>




Step 5: Create a custom button (To send the SMS) in the Lead object, and add it to the page Layout.

Step 6: Execute the Visualforce Page

Click the Send SMS button on the Lead object detail page. A Mobile number should be present in that lead record.

Step 7: Send SMS from Salesforce

Now, a popup window will appear for you to enter the SMS body and click the Send SMS button to send the SMS.

Steps 8: Receiving the SMS


By using the Twilio account, we can send SMS to any country. In the trial account, Twilio provides only the SMS for free. If you want to make a call using the API, You need to upgrade your account.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Require Permission to View Lookup Fields (Critical Update)

We have a scenario to assign the record owner to a Queue based on some criteria. The non-admin users are unable to view the value of the Owner field once the Owner field is updated with the Queue. We found the root cause for this issue. One of the features of the Spring 20 release (Require Permission to View Record Names in Lookup Fields (Critical Update) causes this issue.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.