Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

REST API: Twilio's Response



Response Formats

Twilio can respond to your requests in 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 an SMS message:


_10
GET /2010-04-01/Accounts/ACXXXXX.../Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f


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


_10
GET /2010-04-01/Accounts/ACXXXXX.../Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f.json


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

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:

PropertyDescription
StatusThe HTTP status code for the exception.
MessageA 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:

Requested Resource Not Found

xml
json

_10
<TwilioResponse>
_10
<RestException>
_10
<Status>404</Status>
_10
<Message>The requested resource was not found</Message>
_10
</RestException>
_10
</TwilioResponse>

Example 2

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

Response Exception Example 2

xml
json

_10
<TwilioResponse>
_10
<RestException>
_10
<Status>400</Status>
_10
<Message>No to number is specified</Message>
_10
<Code>21201</Code>
_10
<MoreInfo>http://www.twilio.com/docs/errors/21201</MoreInfo>
_10
</RestException>
_10
</TwilioResponse>


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:

Hypermedia References

xml
json

_11
<TwilioResponse>
_11
<OutgoingCallerId>
_11
<Sid>PNe905d7e6b410746a0fb08c57e5a186f3</Sid>
_11
<AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
_11
<FriendlyName>(510) 555-5555</FriendlyName>
_11
<PhoneNumber>+15105555555</PhoneNumber>
_11
<DateCreated>Tue, 27 Jul 2010 20:21:11 +0000</DateCreated>
_11
<DateUpdated>Tue, 27 Jul 2010 20:21:11 +0000</DateUpdated>
_11
<Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/OutgoingCallerIds/PNe905d7e6b410746a0fb08c57e5a186f3</Uri>
_11
</OutgoingCallerId>
_11
</TwilioResponse>

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:

Hypermedia in Instance Resources

xml
json

_28
<TwilioResponse>
_28
<Call>
_28
<Sid>CAe1644a7eed5088b159577c5802d8be38</Sid>
_28
<DateCreated>Tue, 10 Aug 2010 08:02:17 +0000</DateCreated>
_28
<DateUpdated>Tue, 10 Aug 2010 08:02:47 +0000</DateUpdated>
_28
<ParentCallSid/>
_28
<AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
_28
<To>+14153855708</To>
_28
<From>+14158141819</From>
_28
<PhoneNumberSid></PhoneNumberSid>
_28
<Status>completed</Status>
_28
<StartTime>Tue, 10 Aug 2010 08:02:31 +0000</StartTime>
_28
<EndTime>Tue, 10 Aug 2010 08:02:47 +0000</EndTime>
_28
<Duration>16</Duration>
_28
<Price>-0.03000</Price>
_28
<Flags>
_28
<Flag>outbound-api</Flag>
_28
</Flags>
_28
<ApiVersion>2008-08-01</ApiVersion>
_28
<ForwardedFrom/>
_28
<CallerName/>
_28
<Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAe1644a7eed5088b159577c5802d8be38</Uri>
_28
<SubresourceUris>
_28
<Notifications>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAe1644a7eed5088b159577c5802d8be38/Notifications</Notifications>
_28
<Recordings>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAe1644a7eed5088b159577c5802d8be38/Recordings</Recordings>
_28
</SubresourceUris>
_28
</Call>
_28
</TwilioResponse>


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:

PropertyDescription
uriThe URI of the current page.
first_page_uriThe URI for the first page of this list.
next_page_uriThe URI for the next page of this list.
previous_page_uriThe URI for the previous page of this list.
pageThe current page number. Zero-indexed, so the first page is 0.
page_sizeHow many items are in each page

For example:

Paging Information

xml
json

_33
<TwilioResponse>
_33
<Calls page="0" pagesize="50"
_33
uri="/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls"
_33
firstpageuri="/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls?Page=0&PageSize=50"
_33
previouspageuri=""
_33
nextpageuri="/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls?Page=1&PageSize=50&AfterSid=CA228399228abecca920de212121">
_33
<Call>
_33
<Sid>CA92d4405c9237c4ea04b56cbda88e128c</Sid>
_33
<DateCreated>Fri, 13 Aug 2010 01:16:22 +0000</DateCreated>
_33
<DateUpdated>Fri, 13 Aug 2010 01:16:22 +0000</DateUpdated>
_33
<ParentCallSid/>
_33
<AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
_33
<To>+15305431221</To>
_33
<From>+15104563443</From>
_33
<PhoneNumberSid>PNe2d8e63b37f46f2adb16f228afdb9058</PhoneNumberSid>
_33
<Status>queued</Status>
_33
<StartTime/>
_33
<EndTime/>
_33
<Duration/>
_33
<Price/>
_33
<Flags>
_33
<Flag>outbound-api</Flag>
_33
</Flags>
_33
<ApiVersion>2010-04-01</ApiVersion> <ForwardedFrom/> <CallerName/>
_33
<Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CA92d4405c9237c4ea04b56cbda88e128c</Uri>
_33
<SubresourceUris>
_33
<Notifications>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CA92d4405c9237c4ea04b56cbda88e128c/Notifications</Notifications>
_33
<Recordings>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CA92d4405c9237c4ea04b56cbda88e128c/Recordings</Recordings>
_33
</SubresourceUris>
_33
</Call>
_33
...
_33
</Calls>
_33
</TwilioResponse>

Paging Through API Resources

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

(information)

Info

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:

ParameterDescription
PageSizeHow 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: