Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Validate and format a phone number with Twilio Lookup

See more Twilio Examples

Use Basic Lookup v2 to check a national-format number and obtain its E.164 and national display formats. Supply the two-letter country code for a national number. The example requests only the basic fields.

Read the boolean valid field even when the HTTP request succeeds. When it is false, display any validation_errors that Twilio supplies. Numbering-plan validation does not establish reachability or control of a phone. To confirm control, send an SMS verification code.

Twilio documentation

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.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  number: TStringBuilder;
  encoded: Boolean;
  url: TStringBuilder;
  resp: THttpResponse;
  reply: TJsonObject;
  parsed: Boolean;
  i: Integer;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  //  Load an API key for the Twilio account from private application configuration.
  http := THttp.Create;
  http.RequireSslCertVerify := True;
  http.BasicAuth := True;
  http.Login := 'YOUR_API_KEY_SID';
  http.Password := 'YOUR_API_KEY_SECRET';

  //  The phone number is one URL path segment. Percent-encode spaces and any leading +.
  number := TStringBuilder.Create;
  number.Append('020 7946 0018');
  encoded := number.Encode('url_rfc3986','utf-8');
  if (encoded <> True) then
    begin
      WriteLn('Could not encode the phone number.');
      Exit;
    end;
  url := TStringBuilder.Create;
  url.Append('https://lookups.twilio.com/v2/PhoneNumbers/');
  url.Append(number.GetAsString());
  url.Append('?CountryCode=GB');
  resp := THttpResponse.Create;
  reply := TJsonObject.Create;

  success := http.HttpNoBody('GET',url.GetAsString(),resp);
  if (resp.StatusCode = 0) then
    begin
      WriteLn('No HTTP response from Twilio.');
      Exit;
    end;
  parsed := reply.Load(resp.BodyStr);
  if ((success <> True) or (resp.StatusCode <> 200)) then
    begin
      WriteLn('Twilio returned HTTP ' + resp.StatusCode);
      if ((parsed = True) and (reply.JsonTypeOf('code') = 2)) then
        begin
          WriteLn('Twilio error: ' + reply.IntOf('code') + '  ' + reply.StringOf('message'));
        end;
      Exit;
    end;
  if (parsed <> True) then
    begin
      WriteLn('Expected a JSON response from Twilio.');
      Exit;
    end;
  if (reply.JsonTypeOf('valid') <> 5) then
    begin
      WriteLn('The lookup response is missing the validity flag.');
      Exit;
    end;
  if (reply.BoolOf('valid') <> True) then
    begin
      WriteLn('The number is invalid for the specified country.');
      i := 0;
      while (i < reply.SizeOfArray('validation_errors')) do
        begin
          reply.I := i;
          WriteLn(reply.StringOf('validation_errors[i]'));
          i := i + 1;
        end;

      Exit;
    end;
  if (reply.JsonTypeOf('phone_number') <> 1) then
    begin
      WriteLn('The lookup response is missing the E.164 number.');
      Exit;
    end;
  WriteLn('E.164: ' + reply.StringOf('phone_number'));
  WriteLn('National format: ' + reply.StringOf('national_format'));


  http.Free;
  number.Free;
  url.Free;
  resp.Free;
  reply.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.