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. 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.


        Send an outbound SMS

        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 a message with error handling

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


                    Get messages with no parameters

                    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.


                          Get messages with parameters

                          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 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


                                            Delete a message


                                                  Get messaging services

                                                  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

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


                                                              Get Taskrouter reservations

                                                              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.

                                                              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 by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.


                                                                    Thank you for your feedback!

                                                                    Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

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

                                                                    Thanks for your feedback!