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

Send an MMS with Twilio using a media URL

See more Twilio Examples

Send an image and an optional caption from an MMS-capable Twilio number. Supply an MMS-supported destination in E.164 format and replace the example media URL with an HTTPS URL Twilio can fetch. The host must handle HEAD and GET and return the correct Content-Type for the file.

Live send. With live credentials and valid inputs, this request submits a billable message. Save the Message SID to check delivery later. Supported media types and size limits.

Twilio Messages API reference

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sAccountSid
    Handle hoUrl
    Variant vReq
    Handle hoReq
    Variant vResp
    Handle hoResp
    Handle hoReply
    Boolean iParsed
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    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
    //  Load the owning Account SID and an API key from application configuration.
    Move "YOUR_ACCOUNT_SID" To sAccountSid
    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 sAccountSid To iSuccess
    Get ComAppend Of hoUrl "/Messages.json" To iSuccess

    //  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Set ComHttpVerb Of hoReq To "POST"
    Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
    Send ComAddParam To hoReq "From" "YOUR_MMS_ENABLED_NUMBER"
    Send ComAddParam To hoReq "To" "RECIPIENT_E164_NUMBER"
    //  Supply a URL for the file itself so Twilio can retrieve the media.
    Send ComAddParam To hoReq "MediaUrl" "https://media.example.com/photo.jpg"
    Send ComAddParam To hoReq "Body" "Photo attached."

    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
    Get ComGetAsString Of hoUrl To sTemp1
    Get pvComObject of hoReq to vReq
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp sTemp1 vReq vResp To iSuccess
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 = 0) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        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 <> 201)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "HTTP " iTemp1
        Get ComHasMember Of hoReply "code" To bTemp1
        If ((iParsed = True) And (bTemp1 = True)) 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."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoReply "sid" To iTemp1
    Get ComJsonTypeOf Of hoReply "status" To iTemp2
    If ((iTemp1 <> 1) Or (iTemp2 <> 1)) Begin
        Showln "The create response is missing the message SID or status."
        Procedure_Return
    End

    Get ComStringOf Of hoReply "sid" To sTemp1
    Showln "Message SID: " sTemp1
    Get ComStringOf Of hoReply "status" To sTemp1
    Showln "Initial status: " sTemp1


End_Procedure