Twilio Integration With Salesforce Part I - How to Send A Message From Salesforce to a Phone Number

This blog is the first part of  Two part blog on Integrating your Twilio Account with Salesforce.

In this blog, I will be displaying how to send an SMS from your Salesforce org to a Mobile Phone using the Twilio Integration.

Twilio is a cloud communication Platform as a Service (PaaS) company that allows developers to programmatically make/receive phone calls, send/ receive messages and other functions using its web service API. This blog deals with sending an SMS.


The first step you need to do here is to create a trial Twilio account in After you create an account, you need to get a Twilio Number to send messages through Twilio API.

Note - After getting a number, you need to verify the numbers you are going to send messages to. Also, make sure to enable the regions where you want to send messages to the Global Permissions page.

In this blog, I will show two approaches to send a message. The first one is installing the helper libraries that Twilio provides for Salesforce. The other approach is to create a custom apex class and use Http methods to send the messages.


Twilio provides a helper library that is available for installation as an unmanaged package to send messages.  In your Twilio Account, Click on Docs > Helper Libraries. Select Salesforce on the tab on your left on the SDK page. Click on the  ‘twilio-salesforce project’ link which will take you to a Github page. Scroll Down the readme section and click on the link to install the library as an unmanaged package.

You will be asked to log in to Salesforce. Use your credentials and login to the org.  Install the package for all users. Click on the checkbox to grant access to Third-Party websites and install it. You should see the Twilio library under the Installed Packages section in your org. Twilio installs a number of Apex Classes, Remote Site Settings, etc to your org which will be needed while sending an SMS.

Now to send the message, you only need to call certain apex classes that are present in the helper library. Go to Developer Console and open the Executive Anonymous Window.

In your Executive Anonymous Window, paste the following code.

String account = 'Your Account SID'; 
String token = 'Your Account Token'; //  
TwilioRestClient client = new TwilioRestClient(account, token); 
Map<String,String> params = new Map<String,String> {
    'To'   => 'To the number you want to send this number to',
    'From' => 'From the number you want to send this to. This is your Twilio number ',
     'Body' => 'This is the body of the message you want to send this message'
TwilioSMS sms = client.getAccount().getSMSMessages().create(params);

Account SID  is the number used to identify a user of an account. You can get it from your Twilio Account as it is assigned to you once you make an account with Twilio. Account Token is the authentication token needed to authorize your account. You can find this in your Twilio account as well.

After executing the above code in the Executive Anonymous Window, you should be able to get an SMS with the body as entered in the ‘body’ key of the map ‘param’.


This is the second approach to send an SMS to a verified phone where you create a custom class. You do not have to install the helper library in this one. So, let's start.

First, create a Remote  Site Settings using the URL This is used to connect your Salesforce org to Twilio API to send the messages.

Now open your Developer Console and Create a new class. Name it TwilioSendSMS.

Paste the below code in the class.

public class TwilioSendSMS {    
    public static void sendSms(){            
        String phNumber ='To phone number';        
        String accountSid = 'Account SID';        
        String token = 'Auth Token';        
        String fromPhNumber = 'Your twilio Number';           
        String smsBody = 'This is the body of the sms sent by my class';        
        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);        
        System.Debug('Message sending Successful');        
            System.Debug('Message sending Unsuccessful');

As you can see, I have used the Apex integration classes like Http, HttpRequest, and HttpResponse to communicate with the Twilio API. I have passed Account SID, Authentication Code and set the Headers of the request according to the Twilio API with a POST Request.

Open up your Executive Anonymous Window and type ‘TwilioSendSMS.sendSMS();’  and click execute. You should be able to receive an SMS in the phone number you sent to phNumber with a content written in smsBody.

To sum up, integration of Twilio with Salesforce provides a lot of convenience to your Sales Reps who can now keep track of all the leads they are in contact with and respond with ease while navigating their org. This facilitates convenience for everyone, faster and more accurate response times and much better customer relations. I will be posting another blog which will be part 2 of the Twilio Integration with Salesforce blog where I will show how to receive a message from a cellphone to your Salesforce org.

dont miss out iconCheck out next part of the two-part blog here: Twilio Integration With Salesforce Part - II

Popular Salesforce Blogs