Use Twilio REST APIs from Apex
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.
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.
Use TwilioApiClientResponse
to retrieve the status code and error message from your request.
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 retreiving larger groups of messages.
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 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.
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.
This example shows you how to update the friendly name of a messaging service using the Twilio API.
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?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.