Integrate WhatsApp with Salesforce Using Twilio: A Complete Guide
In today’s world of instant messaging, businesses can no longer afford to rely solely on email or phone calls to communicate with their customers. WhatsApp—with over 2 billion users globally—has become a critical channel for customer engagement.
So, I decided to explore how to integrate WhatsApp with Salesforce, the world’s #1 CRM, using Twilio, a cloud communication platform.
In this blog, I’ll walk you through:
- Why this integration matters
- A step-by-step implementation guide
- Real-life use cases
- Testing & deployment strategies
- Tips to go live with a production setup
🛠 Prerequisites
Before starting, ensure the following are ready:
- Salesforce Developer Edition Org
- Twilio Account (free trial works for testing)
- WhatsApp Sandbox set up in Twilio
- Basic knowledge of Apex, HTTP Callouts, and Salesforce Named Credentials
- Your business use case (e.g., lead follow-up, support update)
🔗 Step-by-Step Integration: WhatsApp + Salesforce via Twilio
Step 1: Set Up WhatsApp Sandbox in Twilio
- Log in to your Twilio Console.
- Navigate to Messaging > Try it Out > Send a WhatsApp message.
- You’ll see a sandbox number (e.g., +14155238886) and a unique code (e.g., join wood-turtle).
- Send the code via WhatsApp to the number to activate the sandbox.
- Test sending messages from the Twilio console UI.
Note: This sandbox is ideal for development/testing. For production, you’ll apply for a verified business number.
Step 2: Configure a Messaging Service (Optional but Recommended)
- In Twilio, go to Messaging > Services and create a new service.
- Attach your sandbox number to this service.
- Copy the Service SID for future reference (optional in basic setups).
Step 3: Retrieve Twilio API Credentials
From your Twilio Console Dashboard:
- Copy your Account SID
- Copy your Auth Token These credentials will be used to authenticate Salesforce callouts.
Step 4: Create a Named Credential in Salesforce
- Go to Setup > Named Credentials.
- Click New Named Credential.
- Fill in:
- Label: Twilio
- URL: https://api.twilio.com
- Identity Type: Named Principal
- Authentication Protocol: Password Authentication
- Username: Your Twilio Account SID
- Password: Your Twilio Auth Token
- Save and test the connection.
🔒 Named Credentials securely store API credentials and eliminate the need to hardcode them in Apex.
Step 5: Create Apex Class to Send WhatsApp Messages
(Use case: Send whatsapp Message when a new contact is created)
public class TwilioWhatsAppSender {
public static final String ACCOUNT_SID = YOUR_ACCOUNT_SID;
public static final String AUTH_TOKEN = YOUR_ACCOUNT_TOKEN;
public static final String TWILIO_WHATSAPP_NUMBER = ‘whatsapp:+14155238886’;
public static final String TO_PHONE_NUMBER = ‘whatsapp:+919876543210’;
public static void sendCarUpdateNotification(String Messages) {
sendWhatsAppMessage(Messages);
}
@future(callout=true)
public static void sendWhatsAppMessage(String message) {
String endpoint = ‘https://api.twilio.com/2010-04-01/Accounts/’ + ACCOUNT_SID + ‘/Messages.json’;
Map<String, String> params = new Map<String, String>{
‘To’ => TO_PHONE_NUMBER,
‘From’ => TWILIO_WHATSAPP_NUMBER,
‘Body’ => message
};
String requestBody = ”;
for (String key : params.keySet()) {
requestBody += key + ‘=’ + EncodingUtil.urlEncode(params.get(key), ‘UTF-8’) + ‘&’;
}
requestBody = requestBody.substring(0, requestBody.length() – 1);
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod(‘POST’);
req.setHeader(‘Authorization’, ‘Basic ‘ + EncodingUtil.base64Encode(Blob.valueOf(ACCOUNT_SID + ‘:’ + AUTH_TOKEN)));
req.setHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
req.setBody(requestBody);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(‘Twilio Response: ‘ + res.getBody());
}
}
📌 Replace YOUR_ACCOUNT_SID & YOUR_ACCOUNT_TOKEN with your actual SID & actual Token.
Step 6: Invoke from Apex, Flow, or Process Builder
You can now send messages from:
- 🔁 Apex Triggers (e.g., on Case or Lead insert)
- 🔄 Record-Triggered Flows
- 🧩 Custom Lightning Buttons
- 🎯 Process Builder with Apex action
Example Apex Trigger
trigger SendWhatsappOnContact on Contact (after insert) {
for (Contact con : Trigger.New) {
String message = ‘Welcome to the twilio’;
TwilioWhatsAppSender.sendCarUpdateNotification(message);
}
}
🧪 Testing the Integration
- Use the sandbox number in development.
- Monitor responses in Twilio’s message logs.
- Confirm status codes (200 = Success) in Apex debug logs.
Moving to Production (Live WhatsApp)
Once your use case is validated:
- Apply for WhatsApp Business API Access through Twilio.
- Verify your Facebook Business Manager account.
- Register your business phone number.
- Use approved WhatsApp templates for outbound messaging.
💼 Real-World Use Cases
🛍 E-Commerce:
- Order confirmation
- Shipping updates
- Delivery feedback
🧾 Finance & Insurance:
- Payment reminders
- Policy updates
- OTPs & 2FA via WhatsApp
🎟 Events & Travel:
- Booking confirmations
- Event check-in links
- Emergency alerts
⚙️ Things to Keep in Mind
- Salesforce’s API Callout limit may affect high-volume use
- Use Batch Apex or Platform Events for large sends
- WhatsApp has strict rules around message content — templates must be pre-approved
- 2-way communication is possible, but receiving WhatsApp replies in Salesforce requires webhook & middleware setup (can be done with Twilio Functions or Heroku)
🎯 Final Thoughts
Integrating Salesforce with WhatsApp using Twilio is a game-changer for businesses aiming to improve communication, customer service, and automation. With minimal code and Twilio’s developer-friendly APIs, this integration can go live quickly and scale to fit enterprise needs.
It’s been exciting to build and test this feature — and I look forward to extending it with 2-way chat and AI-powered auto-responses in the future.