The Twilio for Salesforce package includes an Apex helper class that reduces the amount of code needed to interact with Twilio APIs. The Lightning components in the package focus only on SMS, so the helper class provides you with a way to write custom Apex to interact with any of Twilio's APIs, such as Voice and Chat.
Code samples are written in Apex, but are labeled as Java for syntax highlighting purposes.
1//Post Example2TwilioSF.TwilioApiClient api = new TwilioSF.TwilioApiClient();3api.addUrlPart('Accounts');4api.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');5api.addUrlPart('Messages.json');67api.addParam('To','+15558675310');8api.addParam('From','+15017122661');9api.addParam('Body','Here is my message');10TwilioSF.TwilioApiClientResponse response = api.doPost();1112//Example with key value pairs13String sid = response.getTwilioJsonParser().get('sid').getString();14Integer totalSegments = response.getTwilioJsonParser().get('num_segments').getInteger();1516//Example a key with object17String media = response.getTwilioJsonParser().get('subresource_uris').get('media').getString();18
Twilio will verify that you've included a valid Twilio phone number in the From
parameter, then either queue the call or return an error.
Check out the Messages API reference for the full list of parameters you can include in your request. You can add these using the addUrlPart
method. You can also find the properties you can expect in the response from Twilio.
1//Put Example with failures2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();3//Switch for throwing exceptions,=.4//client.setThrowExceptionsOnFailedResponses();5client.addUrlPart('Accounts');6client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');7client.addUrlPart('Messages.json');89client.addParam('To','+15017122661');10client.addParam('From','+15558675310');11client.addParam('Body','Here is my message');121314TwilioSF.TwilioApiClientResponse response = client.doPut();15system.debug(response.hasError());16system.debug(response.getErrorMessage());17
Use TwilioApiClientResponse
to retrieve the status code and error message from your request.
1//Get Messages No Params2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();3client.addUrlPart('Accounts');4client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');5client.addUrlPart('Messages');6client.addUrlPart('.json');7TwilioSF.TwilioApiClientResponse response = client.doGet();89system.debug(response.getTwilioJsonParser().get('first_page_uri').getString());10system.debug(response.getTwilioJsonParser().get('previous_page_uri').getString());11system.debug(response.getTwilioJsonParser().get('end').getInteger());12system.debug(JSON.serializePretty(response.toMap()));13
This example shows you how to pull a list of messages from Twilio. By default, this method will return 50 results. The maximum number of records in a single request is 1000. You can set how many results are returned using the pageSize
param.
See the "get messages with pagination" examples for retrieving larger groups of messages.
1//Get Messages with Params2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();3client.addUrlPart('Accounts');4client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');5client.addUrlPart('Messages');6client.addUrlPart('.json');78client.addParam('To','+15558675310');9client.addParam('PageSize','5');10client.addParam('DateSent','2018-06-01','>=');11TwilioSF.TwilioApiClientResponse response = client.doGet();1213system.debug(response.getTwilioJsonParser().get('previous_page_uri').getString());14system.debug(response.getTwilioJsonParser().get('page_size').getInteger());15system.debug(JSON.serializePretty(response.toMap()));1617String messagesString = response.getTwilioJsonParser().get('messages').getString();18List<TwilioJsonParser> messagesArray = response.getTwilioArrayJsonParser(messagesString);19for(TwilioJsonParser p:messagesArray){20system.debug(p.get('sid').getString());21}22
This example demonstrates how to retrieve a list of messages that match specified criteria. See this documentation to view additional options for filtering messages.
1//Pagination2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();3client.addUrlPart('Accounts');4client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');5client.addUrlPart('Messages');6client.addUrlPart('.json');789TwilioSF.TwilioApiClientResponse response = client.doGet();10String nextPage = response.getTwilioJsonParser().get('next_page_uri').getString();11client.setNextUrl(nextPage);12while (client.hasNextPage()) {13response = client.doGetNext();14if(response.responseBody != null){15nextPage = response.getTwilioJsonParser().get('next_page_uri').getString();16client.setNextUrl(nextPage);17}18}19
Get all messages in your Twilio account using pagination. In this example, you get the next page URL if it exists, and then call it in your Twilio client.
1//Pagination Example2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();3client.addUrlPart('Accounts');4client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');5client.addUrlPart('Messages');6client.addUrlPart('.json');789TwilioSF.TwilioApiClientResponse response = client.doGet();10String nextPage = response.getTwilioJsonParser().get('next_page_uri').getString();11client.setNextUrl(nextPage);12if(client.hasNextPage()){13response = client.doGetNext();1415String previousPage = response.getTwilioJsonParser().get('previous_page_uri').getString();16client.setPreviousUrl(previousPage);1718if(client.hasPreviousPage()){19response = client.doGetPrevious();20system.debug(response.getTwilioJsonParser().get('uri').getString());21}22}23
1//Delete Example2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();34client.addUrlPart('Accounts');5client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');6client.addUrlPart('Messages');7client.addUrlPart('SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json');89TwilioApiClientResponse response = client.doDelete();10system.debug(response.getHtttpStatusCode());11
1//Get Messages Services2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();3TwilioSF.TwilioApiClientResponse response = client.doGet('https://messaging.twilio.com/v1/Services');4system.debug(JSON.serializePretty(response.toMap()));5
A messaging service is a pool of phone numbers that properly encodes your message body, matches the recipient's area code, and distributes SMS across multiple numbers. You can read more about messaging services here.
This code example demonstrates how to get a list of Messaging Services in your account.
1TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();2client.setBaseUrl('https://messaging.twilio.com/');3client.setApiVersion('v1');4client.addUrlPart('Services');5client.addUrlPart('MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');67client.addParam('FriendlyName','Dev Org1');8TwilioSF.TwilioApiClientResponse response = client.doPost();910system.debug(response.getTwilioJsonParser().get('friendly_name').getString());11system.debug(response.getTwilioJsonParser().get('area_code_geomatch').getBoolean());12system.debug(response.getTwilioJsonParser().get('links').get('phone_numbers').getString());13system.debug(JSON.serializePretty(response.toMap()));14
This example shows you how to update the friendly name of a messaging service using the Twilio API.
1//Task Router Get Reservations2TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();3client.setBaseUrl('https://taskrouter.twilio.com/');4client.setApiVersion('v1');5client.addUrlPart('Workspaces');6client.addUrlPart('WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');7client.addUrlPart('Workers');8client.addUrlPart('WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');9client.addUrlPart('Reservations');1011TwilioSF.TwilioApiClientResponse response = client.doGet();12system.debug(response.getTwilioJsonParser().get('meta').get('first_page_url').getString());13system.debug(JSON.serializePretty(response.toMap()));14String messagesString = response.getTwilioJsonParser().get('reservations').getString();15List<TwilioJsonParser> messagesArray = response.getTwilioArrayJsonParser(messagesString);16for(TwilioJsonParser p:messagesArray){17system.debug(p.get('worker_sid').getString());18system.debug(p.get('date_updated').getDatetime());19system.debug(p.get('links').get('task').getString());20}21
Taskrouter is an API for tracking the availability of call center workers and routing tasks to the appropriate worker. In this example, we pull the current list of open ("reserved") tasks for an individual worker.