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

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpRequest.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkBasicAuth(http, 1)
    ;  Load the owning Account SID and an API key from application configuration.
    accountSid.s = "YOUR_ACCOUNT_SID"
    CkHttp::setCkLogin(http, "YOUR_API_KEY_SID")
    CkHttp::setCkPassword(http, "YOUR_API_KEY_SECRET")
    url.i = CkStringBuilder::ckCreate()
    If url.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(url,"https://api.twilio.com/2010-04-01/Accounts/")
    CkStringBuilder::ckAppend(url,accountSid)
    CkStringBuilder::ckAppend(url,"/Messages.json")

    ;  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::setCkHttpVerb(req, "POST")
    CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded")
    CkHttpRequest::ckAddParam(req,"From","YOUR_MMS_ENABLED_NUMBER")
    CkHttpRequest::ckAddParam(req,"To","RECIPIENT_E164_NUMBER")
    ;  Supply a URL for the file itself so Twilio can retrieve the media.
    CkHttpRequest::ckAddParam(req,"MediaUrl","https://media.example.com/photo.jpg")
    CkHttpRequest::ckAddParam(req,"Body","Photo attached.")

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    reply.i = CkJsonObject::ckCreate()
    If reply.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpReq(http,CkStringBuilder::ckGetAsString(url),req,resp)
    If CkHttpResponse::ckStatusCode(resp) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpRequest::ckDispose(req)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    parsed.i = CkJsonObject::ckLoad(reply,CkHttpResponse::ckBodyStr(resp))
    If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) <> 201)
        Debug "HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
        If (parsed = 1) AND (CkJsonObject::ckHasMember(reply,"code") = 1)
            Debug "Twilio error " + Str(CkJsonObject::ckIntOf(reply,"code")) + ": " + CkJsonObject::ckStringOf(reply,"message")
        EndIf

        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpRequest::ckDispose(req)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    If parsed <> 1
        Debug "Expected a JSON response."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpRequest::ckDispose(req)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(reply,"sid") <> 1) OR (CkJsonObject::ckJsonTypeOf(reply,"status") <> 1)
        Debug "The create response is missing the message SID or status."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpRequest::ckDispose(req)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    Debug "Message SID: " + CkJsonObject::ckStringOf(reply,"sid")
    Debug "Initial status: " + CkJsonObject::ckStringOf(reply,"status")


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(url)
    CkHttpRequest::ckDispose(req)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(reply)


    ProcedureReturn
EndProcedure