Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
Send an SMS with Twilio
See more Twilio Examples
Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.
For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
http: TChilkatHttp;
accountSid: WideString;
url: TChilkatStringBuilder;
req: TChilkatHttpRequest;
resp: TChilkatHttpResponse;
reply: TChilkatJsonObject;
parsed: Integer;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.BasicAuth := 1;
// Use the test Account SID and test Auth Token from the Twilio Console.
accountSid := 'YOUR_TEST_ACCOUNT_SID';
http.Login := accountSid;
http.Password := 'YOUR_TEST_AUTH_TOKEN';
url := TChilkatStringBuilder.Create(Self);
url.Append('https://api.twilio.com/2010-04-01/Accounts/');
url.Append(accountSid);
url.Append('/Messages.json');
// Add raw field values. Chilkat form-encodes them, including the + in a phone number.
req := TChilkatHttpRequest.Create(Self);
req.HttpVerb := 'POST';
req.ContentType := 'application/x-www-form-urlencoded';
req.AddParam('From','+15005550006');
req.AddParam('To','+12025550123');
req.AddParam('Body','Hello from Twilio!');
resp := TChilkatHttpResponse.Create(Self);
reply := TChilkatJsonObject.Create(Self);
success := http.HttpReq(url.GetAsString(),req.ControlInterface,resp.ControlInterface);
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
parsed := reply.Load(resp.BodyStr);
if ((success <> 1) or (resp.StatusCode <> 201)) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode));
if ((parsed = 1) and (reply.HasMember('code') = 1)) then
begin
Memo1.Lines.Add('Twilio error ' + IntToStr(reply.IntOf('code')) + ': ' + reply.StringOf('message'));
end;
Exit;
end;
if (parsed <> 1) then
begin
Memo1.Lines.Add('Expected a JSON response.');
Exit;
end;
if ((reply.JsonTypeOf('sid') <> 1) or (reply.JsonTypeOf('status') <> 1)) then
begin
Memo1.Lines.Add('The create response is missing the message SID or status.');
Exit;
end;
Memo1.Lines.Add('Message SID: ' + reply.StringOf('sid'));
Memo1.Lines.Add('Initial status: ' + reply.StringOf('status'));