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

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.HttpRequest,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  url: TStringBuilder;
  req: THttpRequest;
  resp: THttpResponse;
  result: TJsonObject;
  parsed: Boolean;
  status: string;

begin
  success := False;

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

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

  req := THttpRequest.Create;
  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 := THttpResponse.Create;
  success := http.HttpReq(url.GetAsString(),req,resp);
  if (resp.StatusCode = 0) then
    begin
      WriteLn('No response from Twilio. Check connectivity and TLS settings.');
      Exit;
    end;
  result := TJsonObject.Create;
  parsed := result.Load(resp.BodyStr);
  if ((success <> True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
    begin
      WriteLn('Twilio returned HTTP ' + resp.StatusCode);
      if ((parsed = True) and (result.JsonTypeOf('code') = 2)) then
        begin
          WriteLn('Twilio error code: ' + result.IntOf('code'));
        end;
      Exit;
    end;
  if ((parsed <> True) or (result.JsonTypeOf('status') <> 1)) then
    begin
      WriteLn('Verification status missing from the response.');
      Exit;
    end;
  status := result.StringOf('status');
  if (status <> 'pending') then
    begin
      WriteLn('Verification status: ' + status);
      Exit;
    end;
  if ((result.JsonTypeOf('sid') <> 1) or (result.StringOf('sid') = '')) then
    begin
      WriteLn('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.
  WriteLn('Pending verification SID: ' + result.StringOf('sid'));


  http.Free;
  url.Free;
  req.Free;
  resp.Free;
  result.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.