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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
#  Load an API key for the Twilio account from private application configuration.
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.BasicAuth = $true
$http.Login = "YOUR_API_KEY_SID"
$http.Password = "YOUR_API_KEY_SECRET"

$url = New-Object 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")
$mediaBase = New-Object Chilkat.StringBuilder
$mediaBase.AppendSb($url)
$url.Append(".json")
#  Handle the API redirect explicitly so a signed download uses no Twilio credentials.
$http.FollowRedirects = $false
$download = New-Object Chilkat.Http
$download.RequireSslCertVerify = $true
$fileResp = New-Object Chilkat.HttpResponse
$fileUrl = New-Object Chilkat.StringBuilder
$filename = New-Object Chilkat.StringBuilder
$mediaSid = New-Object Chilkat.StringBuilder
$saved = 0
$resp = New-Object Chilkat.HttpResponse
$reply = New-Object Chilkat.JsonObject

$more = $true
while (($more -eq $true)) {
    $success = $http.HttpNoBody("GET",$url.GetAsString(),$resp)
    if ($resp.StatusCode -eq 0) {
        $("No HTTP response from Twilio.")
        exit
    }

    $parsed = $reply.Load($resp.BodyStr)
    if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
        $("Twilio returned HTTP " + $resp.StatusCode)
        if (($parsed -eq $true) -and ($reply.JsonTypeOf("code") -eq 2)) {
            $("Twilio error: " + $reply.IntOf("code") + "  " + $reply.StringOf("message"))
        }

        exit
    }

    if ($parsed -ne $true) {
        $("Expected a JSON response from Twilio.")
        exit
    }

    if ($reply.JsonTypeOf("media_list") -ne 4) {
        $("Expected an array of media items.")
        exit
    }

    $count = $reply.SizeOfArray("media_list")
    $i = 0
    while (($i -lt $count)) {
        $reply.I = $i
        $mediaSid.SetString($reply.StringOf("media_list[i].sid"))
        if ($mediaSid.Length -eq 0) {
            $("Media item is missing its SID.")
            exit
        }

        $fileUrl.SetString($mediaBase.GetAsString())
        $fileUrl.Append("/")
        $fileUrl.AppendSb($mediaSid)
        $success = $http.HttpNoBody("GET",$fileUrl.GetAsString(),$fileResp)
        if (($fileResp.StatusCode -ge 300) -and ($fileResp.StatusCode -lt 400)) {
            $fileUrl.SetString($fileResp.GetHeaderField("Location"))
            if ($fileUrl.StartsWith("https://",$false) -ne $true) {
                $("Expected an HTTPS media download URL.")
                exit
            }

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

        if (($success -ne $true) -or ($fileResp.StatusCode -ne 200)) {
            $("Media download failed for " + $mediaSid.GetAsString() + ". HTTP " + $fileResp.StatusCode)
            exit
        }

        $filename.SetString("mms-media-")
        $filename.AppendInt($saved + 1)
        $filename.Append(".bin")
        $success = $fileResp.SaveBodyBinary($filename.GetAsString())
        if ($success -eq $false) {
            $($fileResp.LastErrorText)
            exit
        }

        $($filename.GetAsString() + "  " + $mediaSid.GetAsString() + "  " + $reply.StringOf("media_list[i].content_type"))
        $saved = $saved + 1
        $i = $i + 1
    }

    if ($reply.IsNullOf("next_page_uri") -eq $true) {
        $more = $false
    }
    else {
        $url.SetString($reply.StringOf("next_page_uri"))
        if ($url.StartsWith("/2010-04-01/Accounts/",$true) -ne $true) {
            $("Expected a Twilio relative next-page URI.")
            exit
        }

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

}

$("Media files saved: " + $saved)