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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
string ls_AccountSid
oleobject loo_Url
oleobject loo_Req
oleobject loo_Resp
oleobject loo_Reply
long li_Parsed

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
//  Load the owning Account SID and an API key from application configuration.
ls_AccountSid = "YOUR_ACCOUNT_SID"
loo_Http.Login = "YOUR_API_KEY_SID"
loo_Http.Password = "YOUR_API_KEY_SECRET"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://api.twilio.com/2010-04-01/Accounts/")
loo_Url.Append(ls_AccountSid)
loo_Url.Append("/Messages.json")

//  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"
loo_Req.AddParam("From","YOUR_MMS_ENABLED_NUMBER")
loo_Req.AddParam("To","RECIPIENT_E164_NUMBER")
//  Supply a URL for the file itself so Twilio can retrieve the media.
loo_Req.AddParam("MediaUrl","https://media.example.com/photo.jpg")
loo_Req.AddParam("Body","Photo attached.")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Http.HttpReq(loo_Url.GetAsString(),loo_Req,loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 201) then
    Write-Debug "HTTP " + string(loo_Resp.StatusCode)
    if (li_Parsed = 1) AND (loo_Reply.HasMember("code") = 1) then
        Write-Debug "Twilio error " + string(loo_Reply.IntOf("code")) + ": " + loo_Reply.StringOf("message")
    end if

    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if li_Parsed <> 1 then
    Write-Debug "Expected a JSON response."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if (loo_Reply.JsonTypeOf("sid") <> 1) OR (loo_Reply.JsonTypeOf("status") <> 1) then
    Write-Debug "The create response is missing the message SID or status."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

Write-Debug "Message SID: " + loo_Reply.StringOf("sid")
Write-Debug "Initial status: " + loo_Reply.StringOf("status")


destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
destroy loo_Reply