You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

SMS from Salesforce Using Twilio API

By: Radha

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().get('phNumber');

       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 '+EncodingUtil.base64Encode(Blob.valueOf(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(res.getBody(),errorResponseWrapper.class);

           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,erw.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.

Reference: Twilio Helper Library for Salesforce