Menu

Expand
Rate this page:

REST API: Twilio's Response

Response Formats

Twilio can respond to your requests with various formats. The most common are detailed below. For special cases and extra formats see the Tips & Tricks section.

XML

By default, Twilio's REST API returns XML, with a root element of <TwilioResponse>. For example, here is the default XML representation of a SMS message:

GET /2010-04-01/Accounts/ACXXXXX.../Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f
<TwilioResponse>
    <SMSMessage>
        <Sid>SM1f0e8ae6ade43cb3c0ce4525424e404f</Sid>
        <DateCreated>Fri, 13 Aug 2010 01:16:24 +0000</DateCreated>
        <DateUpdated>Fri, 13 Aug 2010 01:16:24 +0000</DateUpdated>
        <DateSent/>
        <AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
        <To>+13455431221</To>
        <From>+15104564545</From>
        <Body>A Test Message</Body>
        <Status>queued</Status>
        <Flags>
            <Flag>outbound</Flag>
        </Flags>
        <ApiVersion>2010-04-01</ApiVersion>
        <Price/>
        <Uri>
            /2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f
        </Uri>
    </SMSMessage>
</TwilioResponse>

JSON

Twilio also supports returning resource representations as JSON. Simply add the .json extension to any resource URI. Here is the above resource represented as JSON:

GET /2010-04-01/Accounts/ACXXXXX.../Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f.json
{
    "sid": "SM1f0e8ae6ade43cb3c0ce4525424e404f",
    "date_created": "Fri, 13 Aug 2010 01:16:24 +0000",
    "date_updated": "Fri, 13 Aug 2010 01:16:24 +0000",
    "date_sent": null,
    "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "to": "+15305431221",
    "from": "+15104564545",
    "body": "A Test Message",
    "status": "queued",
    "flags":["outbound"],
    "api_version": "2010-04-01",
    "price": null,
    "uri": "\/2010-04-01\/Accounts\/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\/Messages\/SM1f0e8ae6ade43cb3c0ce4525424e404f.json"
}

Other

Twilio can also return CSV, HTML and other response types. See the Tips & Tricks section for more information.

Exceptions

Twilio returns exceptions in the HTTP response body when something goes wrong. In XML, these appear as a <RestException> element within the <TwilioResponse>. An exception has up to four properties:

Property Description
Status The HTTP status code for the exception.
Message A more descriptive message regarding the exception.
Code (Conditional) An error code to find help for the exception.
MoreInfo (Conditional) The URL of Twilio's documentation for the error code.

If you receive an exception with status code 400 (invalid request), the 'Code' and 'MoreInfo' properties are useful for debugging what went wrong.

Examples

Example 1

Here is a simple 404:

Loading Code Sample...
        
        

        Requested Resource Not Found

        Example 2

        An invalid POST to the Calls resource without a 'To' parameter would return:

        Loading Code Sample...
              
              

              Response Exception Example 2

              Hypermedia References

              One of the core principles of REST is "Hypermedia As The Engine Of Application State." Basically this means that every resource representation obtained from a REST API request must include URIs that identify that resource and the resources related to it. Hence the Twilio REST API returns representations that include various URIs to help you navigate the API.

              Every representation contains a self-referencing URI (i.e. the URI used to retrieve it). For example, when you GET an OutgoingCallerId instance resource, its URI is one of the properties returned:

              Loading Code Sample...
                    
                    

                    Hypermedia References

                    Hypermedia in List Resources

                    There is extra hypermedia reference information related to the paging information included in list resource representations. Details are in the Paging Information section below.

                    Hypermedia in Instance Resources

                    If an instance resource has subresources, its representation will include the URIs for those resources within a subresource URIs property. For example, notice the subresources returned when GETting a Call instance resource:

                    Loading Code Sample...
                          
                          

                          Hypermedia in Instance Resources

                          List Resources

                          Some resources are lists of other resources. For example, the Calls list resource returns a list of calls. There are several important things to know about using and manipulating these lists.

                          Paging Information

                          If lists are long, the API returns partial results in a single "page". The representation includes the following information:

                          Property Description
                          uri The URI of the current page.
                          first_page_uri The URI for the first page of this list.
                          next_page_uri The URI for the next page of this list.
                          previous_page_uri The URI for the previous page of this list.
                          page The current page number. Zero-indexed, so the first page is 0.
                          page_size How many items are in each page

                          For example:

                          Loading Code Sample...
                                
                                

                                Paging Information

                                Paging Through API Resources

                                When fetching multiple pages of API results, use the provided nextpageuri parameter to retrieve the next page of results.

                                All of the Twilio Helper Libraries handle paging automatically. You do not need to explicitly request individual pages when using a Helper Library to fetch lists of resources.

                                You can control the size of pages with the PageSize parameter:

                                Parameter Description
                                PageSize How many resources to return in each list page. The default is 50, and the maximum is 1000.

                                For example, to limit the number of calls returned to five per page:

                                GET /2010-04-01/Accounts/{AccountSid}/Calls?PageSize=5

                                Data Formats

                                Phone Numbers

                                All phone numbers in requests from Twilio are in E.164 format if possible. For example, (415) 555-4345 would come through as '+14155554345'. However, there are occasionally cases where Twilio cannot normalize an incoming caller ID to E.164. In these situations Twilio will report the raw caller ID string.

                                Dates & Times

                                All dates and times in requests from Twilio are GMT in RFC 2822 format. For example, 6:13 PM PDT on August 19th, 2010 would be 'Fri, 20 Aug 2010 01:13:42 +0000'

                                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.

                                Loading Code Sample...
                                      
                                      
                                      

                                      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!

                                      thanks-feedback-gif