Rate this page:

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. While the Lightning Components in the package focus only on SMS, the helper class allows you to write custom Apex to interact with any of Twilio's API services, such as Voice and Chat.

Note: 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 full API Reference for Messages 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 parameters you can expect back from Twilio.


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


                    This example shows 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, we get the next page URL if it exists and then call it in our Twilio client.


                                            Basic example of message deletion.


                                                  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 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 are pulling the current list of open ("reserved") tasks for an individual worker.

                                                              Rate this page:

                                                              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 browsing the Twilio tag on Stack Overflow.


                                                                    Thank you for your feedback!

                                                                    We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                                                                    Sending your feedback...
                                                                    🎉 Thank you for your feedback!
                                                                    Something went wrong. Please try again.

                                                                    Thanks for your feedback!

                                                                    Refer us and get $10 in 3 simple steps!

                                                                    Step 1

                                                                    Get link

                                                                    Get a free personal referral link here

                                                                    Step 2

                                                                    Give $10

                                                                    Your user signs up and upgrade using link

                                                                    Step 3

                                                                    Get $10

                                                                    1,250 free SMSes
                                                                    OR 1,000 free voice mins
                                                                    OR 12,000 chats
                                                                    OR more