Sample code for 30+ languages & platforms
C# Requires Chilkat v11.0.0+

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.

Twilio documentation

Chilkat C# Downloads

C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.BasicAuth = true;
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("YOUR_ACCOUNT_SID");
url.Append("/Messages");
url.Append("/");
url.Append("YOUR_MESSAGE_SID");
url.Append("/Media");
Chilkat.StringBuilder mediaBase = new Chilkat.StringBuilder();
mediaBase.AppendSb(url);
url.Append(".json");
//  Handle the API redirect explicitly so a signed download uses no Twilio credentials.
http.FollowRedirects = false;
Chilkat.Http download = new Chilkat.Http();
download.RequireSslCertVerify = true;
Chilkat.HttpResponse fileResp = new Chilkat.HttpResponse();
Chilkat.StringBuilder fileUrl = new Chilkat.StringBuilder();
Chilkat.StringBuilder filename = new Chilkat.StringBuilder();
Chilkat.StringBuilder mediaSid = new Chilkat.StringBuilder();
int saved = 0;
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
Chilkat.JsonObject reply = new Chilkat.JsonObject();

bool more = true;
while ((more == true)) {
    success = http.HttpNoBody("GET",url.GetAsString(),resp);
    if (resp.StatusCode == 0) {
        Debug.WriteLine("No HTTP response from Twilio.");
        return;
    }

    bool parsed = reply.Load(resp.BodyStr);
    if ((success != true) || (resp.StatusCode != 200)) {
        Debug.WriteLine("Twilio returned HTTP " + Convert.ToString(resp.StatusCode));
        if ((parsed == true) && (reply.JsonTypeOf("code") == 2)) {
            Debug.WriteLine("Twilio error: " + Convert.ToString(reply.IntOf("code")) + "  " + reply.StringOf("message"));
        }

        return;
    }

    if (parsed != true) {
        Debug.WriteLine("Expected a JSON response from Twilio.");
        return;
    }

    if (reply.JsonTypeOf("media_list") != 4) {
        Debug.WriteLine("Expected an array of media items.");
        return;
    }

    int count = reply.SizeOfArray("media_list");
    int i = 0;
    while ((i < count)) {
        reply.I = i;
        mediaSid.SetString(reply.StringOf("media_list[i].sid"));
        if (mediaSid.Length == 0) {
            Debug.WriteLine("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) {
                Debug.WriteLine("Expected an HTTPS media download URL.");
                return;
            }

            success = download.HttpNoBody("GET",fileUrl.GetAsString(),fileResp);
        }

        if ((success != true) || (fileResp.StatusCode != 200)) {
            Debug.WriteLine("Media download failed for " + mediaSid.GetAsString() + ". HTTP " + Convert.ToString(fileResp.StatusCode));
            return;
        }

        filename.SetString("mms-media-");
        filename.AppendInt(saved + 1);
        filename.Append(".bin");
        success = fileResp.SaveBodyBinary(filename.GetAsString());
        if (success == false) {
            Debug.WriteLine(fileResp.LastErrorText);
            return;
        }

        Debug.WriteLine(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) {
            Debug.WriteLine("Expected a Twilio relative next-page URI.");
            return;
        }

        url.Prepend("https://api.twilio.com");
    }

}

Debug.WriteLine("Media files saved: " + Convert.ToString(saved));