Skip to contentSkip to navigationSkip to topbar
On this page

REST API: Twilio's Response



Response Formats

response-formats page anchor

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

response-formats-xml page anchor

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:

GET /2010-04-01/Accounts/ACXXXXX.../Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f
1
<TwilioResponse>
2
<SMSMessage>
3
<Sid>SM1f0e8ae6ade43cb3c0ce4525424e404f</Sid>
4
<DateCreated>Fri, 13 Aug 2010 01:16:24 +0000</DateCreated>
5
<DateUpdated>Fri, 13 Aug 2010 01:16:24 +0000</DateUpdated>
6
<DateSent/>
7
<AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
8
<To>+13455431221</To>
9
<From>+15104564545</From>
10
<Body>A Test Message</Body>
11
<Status>queued</Status>
12
<Flags>
13
<Flag>outbound</Flag>
14
</Flags>
15
<ApiVersion>2010-04-01</ApiVersion>
16
<Price/>
17
<Uri>
18
/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM1f0e8ae6ade43cb3c0ce4525424e404f
19
</Uri>
20
</SMSMessage>
21
</TwilioResponse>

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
1
{
2
"sid": "SM1f0e8ae6ade43cb3c0ce4525424e404f",
3
"date_created": "Fri, 13 Aug 2010 01:16:24 +0000",
4
"date_updated": "Fri, 13 Aug 2010 01:16:24 +0000",
5
"date_sent": null,
6
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
7
"to": "+15305431221",
8
"from": "+15104564545",
9
"body": "A Test Message",
10
"status": "queued",
11
"flags":["outbound"],
12
"api_version": "2010-04-01",
13
"price": null,
14
"uri": "\/2010-04-01\/Accounts\/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\/Messages\/SM1f0e8ae6ade43cb3c0ce4525424e404f.json"
15
}

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


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.


Here is a simple 404:

Requested Resource Not FoundLink to code sample: Requested Resource Not Found
1
<TwilioResponse>
2
<RestException>
3
<Status>404</Status>
4
<Message>The requested resource was not found</Message>
5
</RestException>
6
</TwilioResponse>

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

1
<TwilioResponse>
2
<RestException>
3
<Status>400</Status>
4
<Message>No to number is specified</Message>
5
<Code>21201</Code>
6
<MoreInfo>http://www.twilio.com/docs/errors/21201</MoreInfo>
7
</RestException>
8
</TwilioResponse>

Hypermedia References

hypermedia page anchor

One of the core principles of REST is "Hypermedia As The Engine Of Application State(link takes you to an external page)." 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:

1
<TwilioResponse>
2
<OutgoingCallerId>
3
<Sid>PNe905d7e6b410746a0fb08c57e5a186f3</Sid>
4
<AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
5
<FriendlyName>(510) 555-5555</FriendlyName>
6
<PhoneNumber>+15105555555</PhoneNumber>
7
<DateCreated>Tue, 27 Jul 2010 20:21:11 +0000</DateCreated>
8
<DateUpdated>Tue, 27 Jul 2010 20:21:11 +0000</DateUpdated>
9
<Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/OutgoingCallerIds/PNe905d7e6b410746a0fb08c57e5a186f3</Uri>
10
</OutgoingCallerId>
11
</TwilioResponse>

Hypermedia in List Resources

hypermedia-in-list-resources page anchor

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

hypermedia-in-instance-resources page anchor

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:

1
<TwilioResponse>
2
<Call>
3
<Sid>CAe1644a7eed5088b159577c5802d8be38</Sid>
4
<DateCreated>Tue, 10 Aug 2010 08:02:17 +0000</DateCreated>
5
<DateUpdated>Tue, 10 Aug 2010 08:02:47 +0000</DateUpdated>
6
<ParentCallSid/>
7
<AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
8
<To>+14153855708</To>
9
<From>+14158141819</From>
10
<PhoneNumberSid></PhoneNumberSid>
11
<Status>completed</Status>
12
<StartTime>Tue, 10 Aug 2010 08:02:31 +0000</StartTime>
13
<EndTime>Tue, 10 Aug 2010 08:02:47 +0000</EndTime>
14
<Duration>16</Duration>
15
<Price>-0.03000</Price>
16
<Flags>
17
<Flag>outbound-api</Flag>
18
</Flags>
19
<ApiVersion>2008-08-01</ApiVersion>
20
<ForwardedFrom/>
21
<CallerName/>
22
<Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAe1644a7eed5088b159577c5802d8be38</Uri>
23
<SubresourceUris>
24
<Notifications>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAe1644a7eed5088b159577c5802d8be38/Notifications</Notifications>
25
<Recordings>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAe1644a7eed5088b159577c5802d8be38/Recordings</Recordings>
26
</SubresourceUris>
27
</Call>
28
</TwilioResponse>

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.

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:

1
<TwilioResponse>
2
<Calls page="0" pagesize="50"
3
uri="/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls"
4
firstpageuri="/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls?Page=0&PageSize=50"
5
previouspageuri=""
6
nextpageuri="/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls?Page=1&PageSize=50&AfterSid=CA228399228abecca920de212121">
7
<Call>
8
<Sid>CA92d4405c9237c4ea04b56cbda88e128c</Sid>
9
<DateCreated>Fri, 13 Aug 2010 01:16:22 +0000</DateCreated>
10
<DateUpdated>Fri, 13 Aug 2010 01:16:22 +0000</DateUpdated>
11
<ParentCallSid/>
12
<AccountSid>ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</AccountSid>
13
<To>+15305431221</To>
14
<From>+15104563443</From>
15
<PhoneNumberSid>PNe2d8e63b37f46f2adb16f228afdb9058</PhoneNumberSid>
16
<Status>queued</Status>
17
<StartTime/>
18
<EndTime/>
19
<Duration/>
20
<Price/>
21
<Flags>
22
<Flag>outbound-api</Flag>
23
</Flags>
24
<ApiVersion>2010-04-01</ApiVersion> <ForwardedFrom/> <CallerName/>
25
<Uri>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CA92d4405c9237c4ea04b56cbda88e128c</Uri>
26
<SubresourceUris>
27
<Notifications>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CA92d4405c9237c4ea04b56cbda88e128c/Notifications</Notifications>
28
<Recordings>/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CA92d4405c9237c4ea04b56cbda88e128c/Recordings</Recordings>
29
</SubresourceUris>
30
</Call>
31
...
32
</Calls>
33
</TwilioResponse>

Paging Through API Resources

response-formats-list-filters page anchor

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


All phone numbers in requests from Twilio are in E.164(link takes you to an external page) 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.

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

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.