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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vUrl
    Handle hoUrl
    Handle hoMediaBase
    Handle hoDownload
    Variant vFileResp
    Handle hoFileResp
    Handle hoFileUrl
    Handle hoFilename
    Variant vMediaSid
    Handle hoMediaSid
    Integer iSaved
    Variant vResp
    Handle hoResp
    Handle hoReply
    Boolean iMore
    Boolean iParsed
    Integer iCount
    Integer i
    String sTemp1
    String sTemp2
    String sTemp3
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Load an API key for the Twilio account from private application configuration.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComBasicAuth Of hoHttp To True
    Set ComLogin Of hoHttp To "YOUR_API_KEY_SID"
    Set ComPassword Of hoHttp To "YOUR_API_KEY_SECRET"

    Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
    If (Not(IsComObjectCreated(hoUrl))) Begin
        Send CreateComObject of hoUrl
    End
    Get ComAppend Of hoUrl "https://api.twilio.com/2010-04-01/Accounts/" To iSuccess
    Get ComAppend Of hoUrl "YOUR_ACCOUNT_SID" To iSuccess
    Get ComAppend Of hoUrl "/Messages" To iSuccess
    Get ComAppend Of hoUrl "/" To iSuccess
    Get ComAppend Of hoUrl "YOUR_MESSAGE_SID" To iSuccess
    Get ComAppend Of hoUrl "/Media" To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoMediaBase
    If (Not(IsComObjectCreated(hoMediaBase))) Begin
        Send CreateComObject of hoMediaBase
    End
    Get pvComObject of hoUrl to vUrl
    Get ComAppendSb Of hoMediaBase vUrl To iSuccess
    Get ComAppend Of hoUrl ".json" To iSuccess
    //  Handle the API redirect explicitly so a signed download uses no Twilio credentials.
    Set ComFollowRedirects Of hoHttp To False
    Get Create (RefClass(cComChilkatHttp)) To hoDownload
    If (Not(IsComObjectCreated(hoDownload))) Begin
        Send CreateComObject of hoDownload
    End
    Set ComRequireSslCertVerify Of hoDownload To True
    Get Create (RefClass(cComChilkatHttpResponse)) To hoFileResp
    If (Not(IsComObjectCreated(hoFileResp))) Begin
        Send CreateComObject of hoFileResp
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoFileUrl
    If (Not(IsComObjectCreated(hoFileUrl))) Begin
        Send CreateComObject of hoFileUrl
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoFilename
    If (Not(IsComObjectCreated(hoFilename))) Begin
        Send CreateComObject of hoFilename
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoMediaSid
    If (Not(IsComObjectCreated(hoMediaSid))) Begin
        Send CreateComObject of hoMediaSid
    End
    Move 0 To iSaved
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End

    Move True To iMore
    While (iMore = True)
        Get ComGetAsString Of hoUrl To sTemp1
        Get pvComObject of hoResp to vResp
        Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        If (iTemp1 = 0) Begin
            Showln "No HTTP response from Twilio."
            Procedure_Return
        End

        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoReply sTemp1 To iParsed
        Get ComStatusCode Of hoResp To iTemp1
        If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "Twilio returned HTTP " iTemp1
            Get ComJsonTypeOf Of hoReply "code" To iTemp1
            If ((iParsed = True) And (iTemp1 = 2)) Begin
                Get ComIntOf Of hoReply "code" To iTemp1
                Get ComStringOf Of hoReply "message" To sTemp1
                Showln "Twilio error: " iTemp1 "  " sTemp1
            End

            Procedure_Return
        End

        If (iParsed <> True) Begin
            Showln "Expected a JSON response from Twilio."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoReply "media_list" To iTemp1
        If (iTemp1 <> 4) Begin
            Showln "Expected an array of media items."
            Procedure_Return
        End

        Get ComSizeOfArray Of hoReply "media_list" To iCount
        Move 0 To i
        While (i < iCount)
            Set ComI Of hoReply To i
            Get ComStringOf Of hoReply "media_list[i].sid" To sTemp1
            Get ComSetString Of hoMediaSid sTemp1 To iSuccess
            Get ComLength Of hoMediaSid To iTemp1
            If (iTemp1 = 0) Begin
                Showln "Media item is missing its SID."
                Procedure_Return
            End

            Get ComGetAsString Of hoMediaBase To sTemp1
            Get ComSetString Of hoFileUrl sTemp1 To iSuccess
            Get ComAppend Of hoFileUrl "/" To iSuccess
            Get pvComObject of hoMediaSid to vMediaSid
            Get ComAppendSb Of hoFileUrl vMediaSid To iSuccess
            Get ComGetAsString Of hoFileUrl To sTemp1
            Get pvComObject of hoFileResp to vFileResp
            Get ComHttpNoBody Of hoHttp "GET" sTemp1 vFileResp To iSuccess
            Get ComStatusCode Of hoFileResp To iTemp1
            Get ComStatusCode Of hoFileResp To iTemp2
            If ((iTemp1 >= 300) And (iTemp2 < 400)) Begin
                Get ComGetHeaderField Of hoFileResp "Location" To sTemp1
                Get ComSetString Of hoFileUrl sTemp1 To iSuccess
                Get ComStartsWith Of hoFileUrl "https://" False To bTemp1
                If (bTemp1 <> True) Begin
                    Showln "Expected an HTTPS media download URL."
                    Procedure_Return
                End

                Get ComGetAsString Of hoFileUrl To sTemp1
                Get pvComObject of hoFileResp to vFileResp
                Get ComHttpNoBody Of hoDownload "GET" sTemp1 vFileResp To iSuccess
            End

            Get ComStatusCode Of hoFileResp To iTemp1
            If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
                Get ComGetAsString Of hoMediaSid To sTemp1
                Get ComStatusCode Of hoFileResp To iTemp1
                Showln "Media download failed for " sTemp1 ". HTTP " iTemp1
                Procedure_Return
            End

            Get ComSetString Of hoFilename "mms-media-" To iSuccess
            Get ComAppendInt Of hoFilename (iSaved + 1) To iSuccess
            Get ComAppend Of hoFilename ".bin" To iSuccess
            Get ComGetAsString Of hoFilename To sTemp1
            Get ComSaveBodyBinary Of hoFileResp sTemp1 To iSuccess
            If (iSuccess = False) Begin
                Get ComLastErrorText Of hoFileResp To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            Get ComGetAsString Of hoFilename To sTemp1
            Get ComGetAsString Of hoMediaSid To sTemp2
            Get ComStringOf Of hoReply "media_list[i].content_type" To sTemp3
            Showln sTemp1 "  " sTemp2 "  " sTemp3
            Move (iSaved + 1) To iSaved
            Move (i + 1) To i
        Loop

        Get ComIsNullOf Of hoReply "next_page_uri" To bTemp1
        If (bTemp1 = True) Begin
            Move False To iMore
        End
        Else Begin
            Get ComStringOf Of hoReply "next_page_uri" To sTemp1
            Get ComSetString Of hoUrl sTemp1 To iSuccess
            Get ComStartsWith Of hoUrl "/2010-04-01/Accounts/" True To bTemp1
            If (bTemp1 <> True) Begin
                Showln "Expected a Twilio relative next-page URI."
                Procedure_Return
            End

            Get ComPrepend Of hoUrl "https://api.twilio.com" To iSuccess
        End

    Loop

    Showln "Media files saved: " iSaved


End_Procedure