.NET Core C# Requires Chilkat v11.0.0+
.NET Core C#
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.
Chilkat .NET Core C# Downloads
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.BasicAuth = true;
// Load the owning Account SID and an API key from application configuration.
string accountSid = "YOUR_ACCOUNT_SID";
http.Login = "YOUR_API_KEY_SID";
http.Password = "YOUR_API_KEY_SECRET";
Chilkat.StringBuilder url = new Chilkat.StringBuilder();
url.Append("https://api.twilio.com/2010-04-01/Accounts/");
url.Append(accountSid);
url.Append("/Messages.json");
url.Append("?PageSize=50");
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
Chilkat.JsonObject reply = new Chilkat.JsonObject();
int total = 0;
bool more = true;
while ((more == true)) {
success = http.HttpNoBody("GET",url.GetAsString(),resp);
if (resp.StatusCode == 0) {
Debug.WriteLine(http.LastErrorText);
return;
}
bool parsed = reply.Load(resp.BodyStr);
if ((success != true) || (resp.StatusCode != 200)) {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode));
if ((parsed == true) && (reply.HasMember("code") == true)) {
Debug.WriteLine("Twilio error " + Convert.ToString(reply.IntOf("code")) + ": " + reply.StringOf("message"));
}
return;
}
if (parsed != true) {
Debug.WriteLine("Expected a JSON response.");
return;
}
if (reply.JsonTypeOf("messages") != 4) {
Debug.WriteLine("Expected a messages array.");
return;
}
int count = reply.SizeOfArray("messages");
int i = 0;
while ((i < count)) {
reply.I = i;
Debug.WriteLine(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) {
Debug.WriteLine("Expected a next-page URI or null.");
return;
}
url.SetString("https://api.twilio.com");
url.Append(reply.StringOf("next_page_uri"));
}
}
Debug.WriteLine("Messages found: " + Convert.ToString(total));