Twilio and Salesforce connect through a combination of the Salesforce REST API, Apex callouts, and Twilio's messaging and voice APIs to embed SMS and calling capabilities directly inside Sales Cloud and Service Cloud. Sales reps can click to call from any Contact or Lead record, receive inbound calls routed through Twilio, and have every interaction logged as a Task or Call activity automatically without leaving Salesforce. This setup is used by enterprise sales teams who need conversation history tied to Opportunities, by support centers routing calls through skills-based assignment, and by operations teams automating transactional SMS from Salesforce Flow.
What You Need Before You Start
Create a Salesforce Connected App in Setup, then App Manager to obtain a client ID and client secret for OAuth 2.0 authorization, which your middleware uses to post call and SMS data back into Salesforce on behalf of a system integration user. In the Connected App settings, enable OAuth and add the scopes api, refresh_token, and offline_access, then set the callback URL to your middleware redirect endpoint. From Twilio you need your Account SID, Auth Token, and at least one phone number, plus a middleware service in Node.js, Python, or an equivalent runtime to handle the bidirectional data flow between both platforms. For click-to-call directly inside Salesforce, install the Twilio Salesforce Package from the Salesforce AppExchange, which provides a CTI adapter that connects to the Salesforce Open CTI framework and embeds a softphone panel in the agent console without requiring a separate browser extension.
Step-by-Step Integration Guide
Implement the OAuth 2.0 JWT Bearer flow in your middleware to obtain Salesforce access tokens using a service account rather than requiring each agent to authenticate individually, and store the refresh token securely in your application secrets manager. When a Twilio inbound call arrives, query the matching Salesforce Contact using GET https://[instance].salesforce.com/services/data/v56.0/query?q=SELECT+Id,Name+FROM+Contact+WHERE+Phone='+{E164number}' and use the Contact ID to trigger a screen pop in the Salesforce CTI adapter via the Open CTI sreenPop JavaScript API. When the call ends, capture the Twilio StatusCallback payload including CallSid, Duration, CallStatus, and RecordingUrl, then create a Salesforce Task record by posting to https://[instance].salesforce.com/services/data/v56.0/sobjects/Task/ with WhoId set to the Contact ID, Subject set to Twilio Call, Status set to Completed, and CallDurationInSeconds set from the Duration field. For outbound SMS, create a named Apex class that makes a callout to your middleware endpoint passing the contact phone and message body, and invoke this class from a Salesforce Flow step that fires when a specific field condition is met on a Contact, Lead, or Opportunity record.
Common Issues and How to Fix Them
Salesforce outbound callouts to Twilio fail with a CalloutException when the Twilio API domain or your middleware domain is not added to Salesforce Remote Site Settings. Navigate to Setup, then Security, then Remote Site Settings and add both https://api.twilio.com and your middleware domain as approved remote sites before any Apex HTTP callout will be permitted. JWT Bearer token generation fails with an invalid_grant response when the Salesforce integration user's profile does not have the API Enabled permission checked, or when the X.509 certificate used to sign the JWT does not match the certificate uploaded to the Connected App. Verify the user profile permission under Setup, then Users, then Profiles, confirm the Connected App certificate configuration, and ensure the certificate Common Name matches exactly including case sensitivity. Duplicate Task records appear when both the Twilio StatusCallback and an Apex trigger attempt to log the call simultaneously on overlapping events. Add the Twilio CallSid as an External ID custom field on the Task object and use the Salesforce upsert operation keyed on that field instead of insert, so concurrent callbacks resolve to a single record rather than creating duplicates.
How to Get More from This Integration
Add an SMS Opt-In custom checkbox field to the Contact and Lead objects and gate every outbound Twilio SMS in your Apex callout class on that field being true, fetching the value in the same SOQL query that retrieves the phone number so compliance is enforced at the code level. Use Twilio Conversations to maintain a persistent SMS thread per Salesforce Contact by storing the Conversation SID in a custom field named Twilio_Conversation_SID__c, then routing all subsequent messages from that phone into the same Conversation so agents always have the full thread without searching across separate activity records. Extend the Twilio CTI adapter configuration to support skill-based routing by reading custom Salesforce user fields for agent skills, mapping those skills to Twilio TaskRouter Worker attributes via the Workers REST API, and configuring TaskRouter Workflow filters to match inbound call tasks to the most qualified available agent. Build Salesforce reports on the Task object filtered to tasks created by the Twilio integration user to produce dashboards showing call volume, average duration, SMS response rates, and outcome distribution segmented by owner and Opportunity stage.
Conclusion
Twilio and Salesforce together create a complete communication layer inside your CRM where every interaction is logged, routed, and reportable without manual effort from your reps. Get in touch with Telphi Consulting to design and build this integration for your Sales Cloud or Service Cloud environment.
Ready to Transform Your Business Communications?
Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.
Comments (0)
Join the discussion and share your thoughts (AI-moderated for quality)
Be the first to comment
No comments yet. Share your thoughts below.