By default, Twilio's REST API returns XML, with a root element of <TwilioResponse>. For example, the default XML representation of an account:
GET http://api.twilio.com/2008-08-01/Accounts/AC35542fc30a091bed0c1ed511e1d9935d HTTP/1.1
<TwilioResponse>
<Account>
<Sid>AC35542fc30a091bed0c1ed511e1d9935d</Sid>
<FriendlyName>Friendly Account 1</FriendlyName>
<Status>1</Status>
<StatusText>Inactive</StatusText>
<DateCreated>Tue, 01 Apr 2008 11:26:32 -0700</DateCreated>
<DateUpdated>Tue, 01 Apr 2008 11:26:32 -0700</DateUpdated>
</Account>
</TwilioResponse>
Twilio can also return JSON, CSV and other response types. See the Tips & Tricks section for more information.
Twilio returns exceptions as a <RestException> element under the <TwilioResponse>. The RestException has up to 4 elements:
If you receive a 400 Status (invalid request), then the <Code> and <MoreInfo> fields will be populated to help you debug why your request was not considered valid.
For example, a simple 404:
<TwilioResponse>
<RestException>
<Status>404</Status>
<Message>The requested resource was not found</Message>
</RestException>
</TwilioResponse>
Another example, an invalid POST to the Calls resource that was missing the Called parameter:
<TwilioResponse>
<RestException>
<Status>400</Status>
<Message>No called number is specified</Message>
<Code>21201</Code>
<MoreInfo>http://www.twilio.com/docs/errors/21201</MoreInfo>
</RestException>
</TwilioResponse>
Some resources are lists of other resources. For example, the Calls resource returns a list of calls. There are several things to know about using and manipulating these lists:
If lists are long, the API returns a single page of results. The XML response returns paging info in the list's element, including:
For example:
<TwilioResponse>
<Calls page="0" numpages="1" pagesize="50" total="38" start="0" end="37">
<Call>
<Sid>CA42ed11f93dc08b952027ffbc406d0868</Sid>
<CallSegmentSid/>
<AccountSid>AC012345678901234567890123456789AF</AccountSid>
<Called>4159633717</Called>
<Caller>4156767925</Caller>
<PhoneNumberSid>PN01234567890123456789012345678900</PhoneNumberSid>
<Status>2</Status>
<StartTime>Thu, 03 Apr 2008 04:36:33 -0400</StartTime>
<EndTime>Thu, 03 Apr 2008 04:36:47 -0400</EndTime>
<Price/>
<Flags>1</Flags>
</Call>
...
</Calls>
</TwilioResponse>
You can control paging with query string parameters:
For example:
https://api.twilio.com/2008-08-01/Accounts/{MyAccountGuid}/Calls?num=5&page=1
This would limit the number of calls to 5 on each page, and would return results for the second page.
Twilio frequently return the following status codes: