Sept 15, 2023
In today's digital world, communication is key, and Salesforce is at the forefront of helping businesses manage and automate their communication processes. With the integration of Twilio's powerful SMS API, you can seamlessly send SMS messages directly from Salesforce using Apex, Salesforce's programming language. In this blog post, we'll walk you through the steps to set up and send SMS messages through Twilio API using Apex in Salesforce.
What is Twilio?
Twilio is a cloud communications platform that provides developers with a set of APIs (Application Programming Interfaces) and tools to build and integrate various communication services into their applications. It was founded in 2008 and has since become a widely used platform for enabling voice, video, messaging, and other communication features in web and mobile applications.
Here are some key aspects of Twilio:
Messaging Services:
Twilio allows developers to send and receive SMS (Short Message Service) and MMS (Multimedia Messaging Service) messages, enabling applications to interact with users via text messages. This can be used for various purposes such as two-factor authentication, notifications, and customer support.
Voice Services:
Twilio's Voice API enables developers to make and receive voice calls within their applications. It supports features like IVR (Interactive Voice Response), call recording, and conferencing, making it suitable for building voice-based applications and services.
Video Services:
Twilio Video API allows developers to integrate real-time video and audio communication capabilities into their applications. This is useful for building video conferencing, live streaming, and video chat applications.
Phone Numbers: Twilio provides access to a wide range of phone numbers, including local, toll-free, and international numbers. Developers can purchase and manage these numbers through the Twilio platform, associating them with their applications for voice and messaging.
Authentication and Verification: Twilio offers tools for verifying user phone numbers, enabling two-factor authentication (2FA) through SMS or voice calls. This helps enhance the security of applications and user accounts.
Flexibility and Scalability: Twilio's APIs are designed to be highly flexible and scalable, allowing developers to customize communication flows and integrate them seamlessly into their applications. It can scale to handle large volumes of communication traffic.
Global Reach: Twilio operates in multiple countries and provides access to phone numbers and communication services on a global scale. This is valuable for businesses and applications with an international user base.
Developer-Friendly: Twilio provides extensive documentation, code samples, and software development kits (SDKs) for various programming languages, making it accessible and developer-friendly. It also offers a cloud-based development environment called Twilio Studio for building communication workflows visually.
Pay-as-You-Go Pricing: Twilio follows a pay-as-you-go pricing model, meaning you only pay for the actual usage of its services. This makes it cost-effective for businesses of all sizes.
Twilio is commonly used by a wide range of industries, including e-commerce, customer service, healthcare, finance, and more, to enhance customer engagement and streamline communication processes. Developers and businesses can leverage Twilio's APIs to create innovative and interactive communication solutions tailored to their specific needs.
Before we dive into the implementation, you'll need to ensure you have the following prerequisites in place:
1. Salesforce Developer or Admin Access: You should have access to a Salesforce developer or admin account to write and execute Apex code.
2. Twilio Account: Sign up for a Twilio account if you don't have one already. Twilio provides a flexible cloud communications platform with APIs for SMS, voice, and more.
3. Twilio Phone Number: Obtain a Twilio phone number that you'll use to send SMS messages.
4. Twilio API Credentials: Retrieve your Twilio API credentials, including the Account SID and Auth Token, from the Twilio Console.
Basic Url : https://api.twilio.com/2010-04-01
EndPoint : https://api.twilio.com/2010-04-01/Accounts/{SID}/Messages
Method : POST
Header Parameters
Key | Type | Value |
X-Twilio-Client | String | salesforce-3.2.0 |
User-Agent | String | twilio-salesforce/3.2.0 |
Accept | String | application/json |
Accept-Charset | String | utf-8 |
Authorization | String | Basic +SID+AuthToken |
Write Below Code in your Apex Class:
public class SendSMS
{
public static void processSms(){
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twilio.com/2010-04-01/Accounts/{Twilio_SID}/Messages');
req.setMethod('POST');
String VERSION = '3.2.0';
req.setHeader('X-Twilio-Client', 'salesforce-' + '3.2.0');
req.setHeader('User-Agent', 'twilio-salesforce/' + '3.2.0');
req.setHeader('Accept', 'application/json');
req.setHeader('Accept-Charset', 'utf-8');
req.setHeader('Authorization','Basic '+EncodingUtil.base64Encode(Blob.valueOf('{Twilio_SID}'+':' +'{Twilio_AuthToken}')));
req.setHeader();
req.setBody('To='+EncodingUtil.urlEncode('your_mobile_number','UTF-8')+'&From='+EncodingUtil.urlEncode('Your_Twilio_Phone_Number','UTF-8')+'&Body='+'Test Message From Salesforce using Twilio API');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('res.getBody() >>'+ res.getBody());
if(res.getStatusCode()==201){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'SMS Sent Successfully'));
}
}
}
Note: After running the above code the SMS will be sent to your mobile number
I hope this blog helped you!