Skip to contentSkip to navigationSkip to topbar
On this page

Use Twilio REST APIs from Apex


The Twilio for Salesforce package includes an Apex(link takes you to an external page) 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.

(information)

Info

Code samples are written in Apex, but are labeled as Java for syntax highlighting purposes.


Send Outbound SMS

send-outbound-sms page anchor
1
//Post Example
2
TwilioSF.TwilioApiClient api = new TwilioSF.TwilioApiClient();
3
api.addUrlPart('Accounts');
4
api.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
5
api.addUrlPart('Messages.json');
6
7
api.addParam('To','+15558675310');
8
api.addParam('From','+15017122661');
9
api.addParam('Body','Here is my message');
10
TwilioSF.TwilioApiClientResponse response = api.doPost();
11
12
//Example with key value pairs
13
String sid = response.getTwilioJsonParser().get('sid').getString();
14
Integer totalSegments = response.getTwilioJsonParser().get('num_segments').getInteger();
15
16
//Example a key with object
17
String 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.


Send Message with Error Handling

send-message-with-error-handling page anchor
1
//Put Example with failures
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
//Switch for throwing exceptions,=.
4
//client.setThrowExceptionsOnFailedResponses();
5
client.addUrlPart('Accounts');
6
client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
7
client.addUrlPart('Messages.json');
8
9
client.addParam('To','+15017122661');
10
client.addParam('From','+15558675310');
11
client.addParam('Body','Here is my message');
12
13
14
TwilioSF.TwilioApiClientResponse response = client.doPut();
15
system.debug(response.hasError());
16
system.debug(response.getErrorMessage());
17

Use TwilioApiClientResponse to retrieve the status code and error message from your request.


Get Messages with no Params

get-messages-with-no-params page anchor
1
//Get Messages No Params
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
client.addUrlPart('Accounts');
4
client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
5
client.addUrlPart('Messages');
6
client.addUrlPart('.json');
7
TwilioSF.TwilioApiClientResponse response = client.doGet();
8
9
system.debug(response.getTwilioJsonParser().get('first_page_uri').getString());
10
system.debug(response.getTwilioJsonParser().get('previous_page_uri').getString());
11
system.debug(response.getTwilioJsonParser().get('end').getInteger());
12
system.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.


Get Messages with Params

get-messages-with-params page anchor
1
//Get Messages with Params
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
client.addUrlPart('Accounts');
4
client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
5
client.addUrlPart('Messages');
6
client.addUrlPart('.json');
7
8
client.addParam('To','+15558675310');
9
client.addParam('PageSize','5');
10
client.addParam('DateSent','2018-06-01','>=');
11
TwilioSF.TwilioApiClientResponse response = client.doGet();
12
13
system.debug(response.getTwilioJsonParser().get('previous_page_uri').getString());
14
system.debug(response.getTwilioJsonParser().get('page_size').getInteger());
15
system.debug(JSON.serializePretty(response.toMap()));
16
17
String messagesString = response.getTwilioJsonParser().get('messages').getString();
18
List<TwilioJsonParser> messagesArray = response.getTwilioArrayJsonParser(messagesString);
19
for(TwilioJsonParser p:messagesArray){
20
system.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.


Get Messages with Pagination - Example 1

get-messages-with-pagination---example-1 page anchor
1
//Pagination
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
client.addUrlPart('Accounts');
4
client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
5
client.addUrlPart('Messages');
6
client.addUrlPart('.json');
7
8
9
TwilioSF.TwilioApiClientResponse response = client.doGet();
10
String nextPage = response.getTwilioJsonParser().get('next_page_uri').getString();
11
client.setNextUrl(nextPage);
12
while (client.hasNextPage()) {
13
response = client.doGetNext();
14
if(response.responseBody != null){
15
nextPage = response.getTwilioJsonParser().get('next_page_uri').getString();
16
client.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.


Get Messages with Pagination - Example 2

get-messages-with-pagination---example-2 page anchor
1
//Pagination Example
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
client.addUrlPart('Accounts');
4
client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
5
client.addUrlPart('Messages');
6
client.addUrlPart('.json');
7
8
9
TwilioSF.TwilioApiClientResponse response = client.doGet();
10
String nextPage = response.getTwilioJsonParser().get('next_page_uri').getString();
11
client.setNextUrl(nextPage);
12
if(client.hasNextPage()){
13
response = client.doGetNext();
14
15
String previousPage = response.getTwilioJsonParser().get('previous_page_uri').getString();
16
client.setPreviousUrl(previousPage);
17
18
if(client.hasPreviousPage()){
19
response = client.doGetPrevious();
20
system.debug(response.getTwilioJsonParser().get('uri').getString());
21
}
22
}
23

1
//Delete Example
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
4
client.addUrlPart('Accounts');
5
client.addUrlPart('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
6
client.addUrlPart('Messages');
7
client.addUrlPart('SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json');
8
9
TwilioApiClientResponse response = client.doDelete();
10
system.debug(response.getHtttpStatusCode());
11

1
//Get Messages Services
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
TwilioSF.TwilioApiClientResponse response = client.doGet('https://messaging.twilio.com/v1/Services');
4
system.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.


Update Messaging Services

update-messaging-services page anchor
1
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
2
client.setBaseUrl('https://messaging.twilio.com/');
3
client.setApiVersion('v1');
4
client.addUrlPart('Services');
5
client.addUrlPart('MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
6
7
client.addParam('FriendlyName','Dev Org1');
8
TwilioSF.TwilioApiClientResponse response = client.doPost();
9
10
system.debug(response.getTwilioJsonParser().get('friendly_name').getString());
11
system.debug(response.getTwilioJsonParser().get('area_code_geomatch').getBoolean());
12
system.debug(response.getTwilioJsonParser().get('links').get('phone_numbers').getString());
13
system.debug(JSON.serializePretty(response.toMap()));
14

This example shows you how to update the friendly name of a messaging service using the Twilio API.


Get Taskrouter Reservations

get-taskrouter-reservations page anchor
1
//Task Router Get Reservations
2
TwilioSF.TwilioApiClient client = new TwilioSF.TwilioApiClient();
3
client.setBaseUrl('https://taskrouter.twilio.com/');
4
client.setApiVersion('v1');
5
client.addUrlPart('Workspaces');
6
client.addUrlPart('WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
7
client.addUrlPart('Workers');
8
client.addUrlPart('WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
9
client.addUrlPart('Reservations');
10
11
TwilioSF.TwilioApiClientResponse response = client.doGet();
12
system.debug(response.getTwilioJsonParser().get('meta').get('first_page_url').getString());
13
system.debug(JSON.serializePretty(response.toMap()));
14
String messagesString = response.getTwilioJsonParser().get('reservations').getString();
15
List<TwilioJsonParser> messagesArray = response.getTwilioArrayJsonParser(messagesString);
16
for(TwilioJsonParser p:messagesArray){
17
system.debug(p.get('worker_sid').getString());
18
system.debug(p.get('date_updated').getDatetime());
19
system.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.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.