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

Send an SMS verification code with Twilio Verify

See more Twilio Examples

Live SMS. When a user requests a code, pass their phone number in E.164 format (for example, +12025550123) and select the SMS channel. Create a Verify Service in the Twilio Console first. Supply its VA service SID and an API key with access to that service. For a live test, use a phone you control. Messaging and verification charges may apply.

Twilio generates the code and sends it to the phone. A pending response means the application can prompt for the code. Save the returned VE verification SID with the user and phone number that initiated the request, then use it to check the code entered by the user.

Twilio Verify API reference

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
url: TChilkatStringBuilder;
req: TChilkatHttpRequest;
resp: TChilkatHttpResponse;
result: TChilkatJsonObject;
parsed: Integer;
status: WideString;

begin
success := 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Obtain these credentials from the application's private configuration.
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.BasicAuth := 1;
http.Login := 'YOUR_API_KEY_SID';
http.Password := 'YOUR_API_KEY_SECRET';

//  Both requests use the same Verify Service SID (VA...).
url := TChilkatStringBuilder.Create(Self);
url.Append('https://verify.twilio.com/v2/Services/');
url.Append('YOUR_VERIFY_SERVICE_SID');
url.Append('/Verifications');

req := TChilkatHttpRequest.Create(Self);
req.HttpVerb := 'POST';
req.ContentType := 'application/x-www-form-urlencoded';
//  Supply the number the user asked to verify. AddParam encodes the leading plus sign.
req.AddParam('To','PHONE_NUMBER_IN_E164_FORMAT');
req.AddParam('Channel','sms');

resp := TChilkatHttpResponse.Create(Self);
success := http.HttpReq(url.GetAsString(),req.ControlInterface,resp.ControlInterface);
if (resp.StatusCode = 0) then
  begin
    Memo1.Lines.Add('No response from Twilio. Check connectivity and TLS settings.');
    Exit;
  end;
result := TChilkatJsonObject.Create(Self);
parsed := result.Load(resp.BodyStr);
if ((success <> 1) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
  begin
    Memo1.Lines.Add('Twilio returned HTTP ' + IntToStr(resp.StatusCode));
    if ((parsed = 1) and (result.JsonTypeOf('code') = 2)) then
      begin
        Memo1.Lines.Add('Twilio error code: ' + IntToStr(result.IntOf('code')));
      end;
    Exit;
  end;
if ((parsed <> 1) or (result.JsonTypeOf('status') <> 1)) then
  begin
    Memo1.Lines.Add('Verification status missing from the response.');
    Exit;
  end;
status := result.StringOf('status');
if (status <> 'pending') then
  begin
    Memo1.Lines.Add('Verification status: ' + status);
    Exit;
  end;
if ((result.JsonTypeOf('sid') <> 1) or (result.StringOf('sid') = '')) then
  begin
    Memo1.Lines.Add('The pending response did not include a verification SID.');
    Exit;
  end;
//  Save this SID with the pending attempt before prompting the user for the code.
Memo1.Lines.Add('Pending verification SID: ' + result.StringOf('sid'));