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

Delphi DLL
var
success: Boolean;
http: HCkHttp;
accountSid: PWideChar;
url: HCkStringBuilder;
req: HCkHttpRequest;
resp: HCkHttpResponse;
reply: HCkJsonObject;
parsed: Boolean;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putBasicAuth(http,True);
//  Load the owning Account SID and an API key from application configuration.
accountSid := 'YOUR_ACCOUNT_SID';
CkHttp_putLogin(http,'YOUR_API_KEY_SID');
CkHttp_putPassword(http,'YOUR_API_KEY_SECRET');
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://api.twilio.com/2010-04-01/Accounts/');
CkStringBuilder_Append(url,accountSid);
CkStringBuilder_Append(url,'/Messages.json');

//  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
req := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,'POST');
CkHttpRequest_putContentType(req,'application/x-www-form-urlencoded');
CkHttpRequest_AddParam(req,'From','YOUR_MMS_ENABLED_NUMBER');
CkHttpRequest_AddParam(req,'To','RECIPIENT_E164_NUMBER');
//  Supply a URL for the file itself so Twilio can retrieve the media.
CkHttpRequest_AddParam(req,'MediaUrl','https://media.example.com/photo.jpg');
CkHttpRequest_AddParam(req,'Body','Photo attached.');

resp := CkHttpResponse_Create();
reply := CkJsonObject_Create();
success := CkHttp_HttpReq(http,CkStringBuilder__getAsString(url),req,resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;
parsed := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 201)) then
  begin
    Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    if ((parsed = True) and (CkJsonObject_HasMember(reply,'code') = True)) then
      begin
        Memo1.Lines.Add('Twilio error ' + IntToStr(CkJsonObject_IntOf(reply,'code')) + ': ' + CkJsonObject__stringOf(reply,'message'));
      end;
    Exit;
  end;
if (parsed <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(reply,'sid') <> 1) or (CkJsonObject_JsonTypeOf(reply,'status') <> 1)) then
  begin
    Memo1.Lines.Add('The create response is missing the message SID or status.');
    Exit;
  end;
Memo1.Lines.Add('Message SID: ' + CkJsonObject__stringOf(reply,'sid'));
Memo1.Lines.Add('Initial status: ' + CkJsonObject__stringOf(reply,'status'));

CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);