Sample code for 30+ languages & platforms
VB.NET 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 VB.NET Downloads

VB.NET
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Load an API key for the Twilio account from private application configuration.
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.BasicAuth = True
http.Login = "YOUR_API_KEY_SID"
http.Password = "YOUR_API_KEY_SECRET"

Dim url As 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")
Dim mediaBase As 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
Dim download As New Chilkat.Http
download.RequireSslCertVerify = True
Dim fileResp As New Chilkat.HttpResponse
Dim fileUrl As New Chilkat.StringBuilder
Dim filename As New Chilkat.StringBuilder
Dim mediaSid As New Chilkat.StringBuilder
Dim saved As Integer = 0
Dim resp As New Chilkat.HttpResponse
Dim reply As New Chilkat.JsonObject

Dim more As Boolean = True
While (more = True)
    success = http.HttpNoBody("GET",url.GetAsString(),resp)
    If (resp.StatusCode = 0) Then
        Debug.WriteLine("No HTTP response from Twilio.")
        Exit Sub
    End If

    Dim parsed As Boolean = reply.Load(resp.BodyStr)
    If ((success <> True) Or (resp.StatusCode <> 200)) Then
        Debug.WriteLine("Twilio returned HTTP " & resp.StatusCode)
        If ((parsed = True) And (reply.JsonTypeOf("code") = 2)) Then
            Debug.WriteLine("Twilio error: " & reply.IntOf("code") & "  " & reply.StringOf("message"))
        End If

        Exit Sub
    End If

    If (parsed <> True) Then
        Debug.WriteLine("Expected a JSON response from Twilio.")
        Exit Sub
    End If

    If (reply.JsonTypeOf("media_list") <> 4) Then
        Debug.WriteLine("Expected an array of media items.")
        Exit Sub
    End If

    Dim count As Integer = reply.SizeOfArray("media_list")
    Dim i As Integer = 0
    While (i < count)
        reply.I = i
        mediaSid.SetString(reply.StringOf("media_list[i].sid"))
        If (mediaSid.Length = 0) Then
            Debug.WriteLine("Media item is missing its SID.")
            Exit Sub
        End If

        fileUrl.SetString(mediaBase.GetAsString())
        fileUrl.Append("/")
        fileUrl.AppendSb(mediaSid)
        success = http.HttpNoBody("GET",fileUrl.GetAsString(),fileResp)
        If ((fileResp.StatusCode >= 300) And (fileResp.StatusCode < 400)) Then
            fileUrl.SetString(fileResp.GetHeaderField("Location"))
            If (fileUrl.StartsWith("https://",False) <> True) Then
                Debug.WriteLine("Expected an HTTPS media download URL.")
                Exit Sub
            End If

            success = download.HttpNoBody("GET",fileUrl.GetAsString(),fileResp)
        End If

        If ((success <> True) Or (fileResp.StatusCode <> 200)) Then
            Debug.WriteLine("Media download failed for " & mediaSid.GetAsString() & ". HTTP " & fileResp.StatusCode)
            Exit Sub
        End If

        filename.SetString("mms-media-")
        filename.AppendInt(saved + 1)
        filename.Append(".bin")
        success = fileResp.SaveBodyBinary(filename.GetAsString())
        If (success = False) Then
            Debug.WriteLine(fileResp.LastErrorText)
            Exit Sub
        End If


        Debug.WriteLine(filename.GetAsString() & "  " & mediaSid.GetAsString() & "  " & reply.StringOf("media_list[i].content_type"))
        saved = saved + 1
        i = i + 1
    End While
    If (reply.IsNullOf("next_page_uri") = True) Then
        more = False
    Else
        url.SetString(reply.StringOf("next_page_uri"))
        If (url.StartsWith("/2010-04-01/Accounts/",True) <> True) Then
            Debug.WriteLine("Expected a Twilio relative next-page URI.")
            Exit Sub
        End If

        url.Prepend("https://api.twilio.com")
    End If

End While
Debug.WriteLine("Media files saved: " & saved)