JavaScript Requires Chilkat v11.0.0+
JavaScript
Download the media files from a Twilio MMS
See more Twilio Examples
Supply the Message SID of a received or sent MMS. List its media records, then request each media resource without the .json extension to download the file bytes. Twilio may return a temporary HTTPS download URL. The example uses a separate HTTP client for that URL so the Twilio API key stays with the API host.
Run in an empty output directory. Files are written as mms-media-1.bin, mms-media-2.bin, and so on. The code prints the media SID and MIME type alongside each filename. An empty media list produces a count of zero.
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.
// Load an API key for the Twilio account from private application configuration.
var http = new CkHttp();
http.RequireSslCertVerify = true;
http.BasicAuth = true;
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("YOUR_ACCOUNT_SID");
url.Append("/Messages");
url.Append("/");
url.Append("YOUR_MESSAGE_SID");
url.Append("/Media");
var mediaBase = new CkStringBuilder();
mediaBase.AppendSb(url);
url.Append(".json");
// Handle the API redirect explicitly so a signed download uses no Twilio credentials.
http.FollowRedirects = false;
var download = new CkHttp();
download.RequireSslCertVerify = true;
var fileResp = new CkHttpResponse();
var fileUrl = new CkStringBuilder();
var filename = new CkStringBuilder();
var mediaSid = new CkStringBuilder();
var saved = 0;
var resp = new CkHttpResponse();
var reply = new CkJsonObject();
var more = true;
while ((more == true)) {
success = http.HttpNoBody("GET",url.GetAsString(),resp);
if (resp.StatusCode == 0) {
console.log("No HTTP response from Twilio.");
return;
}
var parsed = reply.Load(resp.BodyStr);
if ((success !== true) || (resp.StatusCode !== 200)) {
console.log("Twilio returned HTTP " + resp.StatusCode);
if ((parsed == true) && (reply.JsonTypeOf("code") == 2)) {
console.log("Twilio error: " + reply.IntOf("code") + " " + reply.StringOf("message"));
}
return;
}
if (parsed !== true) {
console.log("Expected a JSON response from Twilio.");
return;
}
if (reply.JsonTypeOf("media_list") !== 4) {
console.log("Expected an array of media items.");
return;
}
var count = reply.SizeOfArray("media_list");
var i = 0;
while ((i < count)) {
reply.I = i;
mediaSid.SetString(reply.StringOf("media_list[i].sid"));
if (mediaSid.Length == 0) {
console.log("Media item is missing its SID.");
return;
}
fileUrl.SetString(mediaBase.GetAsString());
fileUrl.Append("/");
fileUrl.AppendSb(mediaSid);
success = http.HttpNoBody("GET",fileUrl.GetAsString(),fileResp);
if ((fileResp.StatusCode >= 300) && (fileResp.StatusCode < 400)) {
fileUrl.SetString(fileResp.GetHeaderField("Location"));
if (fileUrl.StartsWith("https://",false) !== true) {
console.log("Expected an HTTPS media download URL.");
return;
}
success = download.HttpNoBody("GET",fileUrl.GetAsString(),fileResp);
}
if ((success !== true) || (fileResp.StatusCode !== 200)) {
console.log("Media download failed for " + mediaSid.GetAsString() + ". HTTP " + fileResp.StatusCode);
return;
}
filename.SetString("mms-media-");
filename.AppendInt(saved+1);
filename.Append(".bin");
success = fileResp.SaveBodyBinary(filename.GetAsString());
if (success == false) {
console.log(fileResp.LastErrorText);
return;
}
console.log(filename.GetAsString() + " " + mediaSid.GetAsString() + " " + reply.StringOf("media_list[i].content_type"));
saved = saved+1;
i = i+1;
}
if (reply.IsNullOf("next_page_uri") == true) {
more = false;
}
else {
url.SetString(reply.StringOf("next_page_uri"));
if (url.StartsWith("/2010-04-01/Accounts/",true) !== true) {
console.log("Expected a Twilio relative next-page URI.");
return;
}
url.Prepend("https://api.twilio.com");
}
}
console.log("Media files saved: " + saved);