JavaScript Requires Chilkat v11.0.0+
JavaScript
List Twilio message history
See more Twilio Examples
List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.
Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
var http = new CkHttp();
http.RequireSslCertVerify = true;
http.BasicAuth = true;
// Load the owning Account SID and an API key from application configuration.
var accountSid = "YOUR_ACCOUNT_SID";
http.Login = "YOUR_API_KEY_SID";
http.Password = "YOUR_API_KEY_SECRET";
var url = new CkStringBuilder();
url.Append("https://api.twilio.com/2010-04-01/Accounts/");
url.Append(accountSid);
url.Append("/Messages.json");
url.Append("?PageSize=50");
var resp = new CkHttpResponse();
var reply = new CkJsonObject();
var total = 0;
var more = true;
while ((more == true)) {
success = http.HttpNoBody("GET",url.GetAsString(),resp);
if (resp.StatusCode == 0) {
console.log(http.LastErrorText);
return;
}
var parsed = reply.Load(resp.BodyStr);
if ((success !== true) || (resp.StatusCode !== 200)) {
console.log("HTTP " + resp.StatusCode);
if ((parsed == true) && (reply.HasMember("code") == true)) {
console.log("Twilio error " + reply.IntOf("code") + ": " + reply.StringOf("message"));
}
return;
}
if (parsed !== true) {
console.log("Expected a JSON response.");
return;
}
if (reply.JsonTypeOf("messages") !== 4) {
console.log("Expected a messages array.");
return;
}
var count = reply.SizeOfArray("messages");
var i = 0;
while ((i < count)) {
reply.I = i;
console.log(reply.StringOf("messages[i].sid") + " " + reply.StringOf("messages[i].direction") + " " + reply.StringOf("messages[i].status"));
i = i+1;
}
total = total+count;
// Twilio supplies a relative URI with the cursor and returns null on the last page.
if (reply.IsNullOf("next_page_uri") == true) {
more = false;
}
else {
if (reply.JsonTypeOf("next_page_uri") !== 1) {
console.log("Expected a next-page URI or null.");
return;
}
url.SetString("https://api.twilio.com");
url.Append(reply.StringOf("next_page_uri"));
}
}
console.log("Messages found: " + total);