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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
;  Load an API key for the Twilio account from private application configuration.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.BasicAuth = True
$oHttp.Login = "YOUR_API_KEY_SID"
$oHttp.Password = "YOUR_API_KEY_SECRET"

$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://api.twilio.com/2010-04-01/Accounts/")
$oUrl.Append("YOUR_ACCOUNT_SID")
$oUrl.Append("/Messages")
$oUrl.Append("/")
$oUrl.Append("YOUR_MESSAGE_SID")
$oUrl.Append("/Media")
$oMediaBase = ObjCreate("Chilkat.StringBuilder")
$oMediaBase.AppendSb($oUrl)
$oUrl.Append(".json")
;  Handle the API redirect explicitly so a signed download uses no Twilio credentials.
$oHttp.FollowRedirects = False
$oDownload = ObjCreate("Chilkat.Http")
$oDownload.RequireSslCertVerify = True
$oFileResp = ObjCreate("Chilkat.HttpResponse")
$oFileUrl = ObjCreate("Chilkat.StringBuilder")
$oFilename = ObjCreate("Chilkat.StringBuilder")
$oMediaSid = ObjCreate("Chilkat.StringBuilder")
Local $iSaved = 0
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")

Local $bMore = True
While ($bMore = True)
    $bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
    If ($oResp.StatusCode = 0) Then
        ConsoleWrite("No HTTP response from Twilio." & @CRLF)
        Exit
    EndIf

Local $bParsed = $oReply.Load($oResp.BodyStr)
    If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
        ConsoleWrite("Twilio returned HTTP " & $oResp.StatusCode & @CRLF)
        If (($bParsed = True) And ($oReply.JsonTypeOf("code") = 2)) Then
            ConsoleWrite("Twilio error: " & $oReply.IntOf("code") & "  " & $oReply.StringOf("message") & @CRLF)
        EndIf

        Exit
    EndIf

    If ($bParsed <> True) Then
        ConsoleWrite("Expected a JSON response from Twilio." & @CRLF)
        Exit
    EndIf

    If ($oReply.JsonTypeOf("media_list") <> 4) Then
        ConsoleWrite("Expected an array of media items." & @CRLF)
        Exit
    EndIf

Local $iCount = $oReply.SizeOfArray("media_list")
Local $i = 0
    While ($i < $iCount)
        $oReply.I = $i
        $oMediaSid.SetString($oReply.StringOf("media_list[i].sid"))
        If ($oMediaSid.Length = 0) Then
            ConsoleWrite("Media item is missing its SID." & @CRLF)
            Exit
        EndIf

        $oFileUrl.SetString($oMediaBase.GetAsString())
        $oFileUrl.Append("/")
        $oFileUrl.AppendSb($oMediaSid)
        $bSuccess = $oHttp.HttpNoBody("GET",$oFileUrl.GetAsString(),$oFileResp)
        If (($oFileResp.StatusCode >= 300) And ($oFileResp.StatusCode < 400)) Then
            $oFileUrl.SetString($oFileResp.GetHeaderField("Location"))
            If ($oFileUrl.StartsWith("https://",False) <> True) Then
                ConsoleWrite("Expected an HTTPS media download URL." & @CRLF)
                Exit
            EndIf

            $bSuccess = $oDownload.HttpNoBody("GET",$oFileUrl.GetAsString(),$oFileResp)
        EndIf

        If (($bSuccess <> True) Or ($oFileResp.StatusCode <> 200)) Then
            ConsoleWrite("Media download failed for " & $oMediaSid.GetAsString() & ". HTTP " & $oFileResp.StatusCode & @CRLF)
            Exit
        EndIf

        $oFilename.SetString("mms-media-")
        $oFilename.AppendInt($iSaved + 1)
        $oFilename.Append(".bin")
        $bSuccess = $oFileResp.SaveBodyBinary($oFilename.GetAsString())
        If ($bSuccess = False) Then
            ConsoleWrite($oFileResp.LastErrorText & @CRLF)
            Exit
        EndIf

        ConsoleWrite($oFilename.GetAsString() & "  " & $oMediaSid.GetAsString() & "  " & $oReply.StringOf("media_list[i].content_type") & @CRLF)
        $iSaved = $iSaved + 1
        $i = $i + 1
    Wend
    If ($oReply.IsNullOf("next_page_uri") = True) Then
        $bMore = False
    Else
        $oUrl.SetString($oReply.StringOf("next_page_uri"))
        If ($oUrl.StartsWith("/2010-04-01/Accounts/",True) <> True) Then
            ConsoleWrite("Expected a Twilio relative next-page URI." & @CRLF)
            Exit
        EndIf

        $oUrl.Prepend("https://api.twilio.com")
    EndIf

Wend
ConsoleWrite("Media files saved: " & $iSaved & @CRLF)