How to Connect Salesforce with Twilio for Real-Time SMS & WhatsApp Communication

Introduction

Modern businesses require instant customer communication through SMS and WhatsApp.
However, using multiple communication tools creates disconnected conversations, delayed responses,
and poor customer visibility.
By integrating Salesforce with Twilio, organizations can manage real-time SMS and WhatsApp communication
directly inside Salesforce CRM using Apex, Lightning Web Components (LWC), Platform Events, Queueable Apex,
and REST APIs.

What is Twilio?

Twilio is a cloud communication platform that provides APIs for SMS, WhatsApp, Voice Calls, OTP verification, notifications, and customer engagement.

Why Integrate Salesforce with Twilio?

·       Centralized communication inside Salesforce

·       Real-time customer interaction

·       Bulk SMS and WhatsApp campaigns

·       Automated notifications and reminders

·       Conversation history tracking

·       Improved customer support experience

Step-by-Step Salesforce Twilio Integration

Step 1: Create Twilio Account

First, Search ‘https://console.twilio.com’ on your browser and Create a Free Twilio Account.

Open:

·        Twilio Console

Create your account and note the following:

·        Account SID

·        Auth Token

The deployment document mentions these credentials are required for Salesforce integration.

Step 2: Buy a Twilio Number

Navigate to:

Phone Numbers → Active Numbers → Buy a Number

Select:

·        Country

·        SMS capability

·        WhatsApp capability

Then purchase the number.

The setup guide explains how to buy and configure a Twilio number from the Twilio Console dashboard.

Step 3: Configure WhatsApp Sandbox

Navigate to:

Messaging → Try It Out → Send a WhatsApp Message

You will receive:

·        WhatsApp Number

·        Join Code

·        QR Code

Scan the QR code to connect WhatsApp.

The PDF deployment guide explains WhatsApp sandbox onboarding and QR-based registration.

Step 4: Create Apex Service Class

Below is a sample Apex class for sending SMS using Twilio REST APIs.

public with sharing class TwilioMessageService {

    @future(callout=true)
    public static void sendSMS(String toNumber, String messageBody) {

        HttpRequest req = new HttpRequest();

        req.setEndpoint(
            'callout:Twilio_API/2010-04-01/Accounts/ACCOUNT_SID/Messages.json'
        );

        req.setMethod('POST');

        req.setHeader(
            'Content-Type',
            'application/x-www-form-urlencoded'
        );

        String body =
            'To=' + EncodingUtil.urlEncode(toNumber, 'UTF-8') +
            '&From=' + EncodingUtil.urlEncode('+1234567890', 'UTF-8') +
            '&Body=' + EncodingUtil.urlEncode(messageBody, 'UTF-8');

        req.setBody(body);

        Http http = new Http();
        HttpResponse res = http.send(req);

        System.debug(res.getBody());
    }
}

Step 5: Create Inbound Webhook REST API

Below is a sample Twilio sends incoming messages to Salesforce through Apex REST APIs.

Example:

@RestResource(urlMapping='/twilio/inbound')
global with sharing class TwilioInboundWebhook {

    @HttpPost
    global static void receiveSMS() {

        RestRequest req = RestContext.request;

        String fromNumber = req.params.get('From');
        String body = req.params.get('Body');

        Message__c msg = new Message__c(
            Phone__c = fromNumber,
            Message__c = body,
            Direction__c = 'Inbound'
        );

        insert msg;

        Message_Update_Event__e eventObj =
            new Message_Update_Event__e(
                Phone__c = fromNumber
            );

        EventBus.publish(eventObj);
    }
}

Step 6: Create Salesforce Site

Salesforce Sites are required because Twilio needs a public webhook endpoint to send inbound messages.

Navigate to:

Setup → Sites

Create a new site:

Site Name: GMS Inbound Site

Save the generated:

Default Web Address

This URL will later be used in Twilio webhooks.

Step 7: Object setting

To allow Twilio webhooks to create and access messaging records inside Salesforce, you must provide the required object permissions through the Salesforce Site Public Access Settings.

Navigate to:

Setup → Sites → Public Access Settings → Object Settings

These object permissions are required because Twilio inbound webhook APIs interact with these objects to:

·        store incoming SMS and WhatsApp messages,

·        maintain conversation history,

·        track communication records,

·        and support real-time chat functionality inside Salesforce.

Without proper object access, Salesforce will block webhook processing and inbound messages may fail to store successfully.

Step 8: Configure Apex Class Access

To allow Twilio webhooks to communicate with Salesforce, you must provide public access to the required Apex REST classes through the Salesforce Site.

Navigate to:

Setup → Sites → Public Access Settings → Apex Class Access

These Apex classes are responsible for:

·        handling inbound SMS and WhatsApp messages,

·        processing Twilio delivery status callbacks,

·        receiving webhook requests from Twilio,

·        and enabling real-time communication between Salesforce and Twilio.

Without granting access, Twilio will not be able to invoke the Salesforce REST endpoints successfully.

Step 9: Configure Named Credentials

Named Credentials securely store Twilio authentication.

Navigate to:

Setup → Named Credentials

Create:

URL = https://api.twilio.com

Then configure:

·        Username = Twilio Account SID

·        Password = Twilio Auth Token

The deployment guide demonstrates configuring Twilio credentials through External Credentials and Named Credentials.

Step 10: Configure Twilio Webhook

Go to:

Develop → Phone Numbers → Active Numbers

Under Messaging Configuration:

Webhook URL =
https://YOUR_DOMAIN/services/apexrest/twilio/inbound

The deployment document shows configuring the webhook URL using Salesforce Site endpoint and Apex REST service.

Security Best Practices

·       Use Named Credentials

·       Avoid hardcoded authentication tokens

·       Validate inbound webhook requests

·       Use Permission Sets and CRUD/FLS

·       Bulkify Apex code

·       Store logs for troubleshooting

Challenges Faced During Integration

·       Phone number formatting

·       Twilio geographic permissions

·       Governor limits

·       Real-time synchronization

·       Rate limiting during bulk campaigns

·       Webhook security

Conclusion

Salesforce and Twilio integration enables businesses to build scalable real-time communication systems directly inside CRM. Using Apex, Queueable Apex, Platform Events, REST APIs, and Lightning Web Components, developers can create enterprise-grade SMS and WhatsApp platforms with real-time messaging, delivery tracking, bulk communication, scheduling, and automated workflows.

Feedback & Support

If you encounter any issues during Salesforce and Twilio integration or have suggestions for improvement, feel free to reach out or share your feedback.

0 Comments
Write a comment
Your email address will not be published. Required fields are marked *
Book a free call
Scroll