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

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
number: TChilkatStringBuilder;
encoded: Integer;
url: TChilkatStringBuilder;
resp: TChilkatHttpResponse;
reply: TChilkatJsonObject;
parsed: Integer;
i: Integer;

begin
success := 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.BasicAuth := 1;
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 := TChilkatStringBuilder.Create(Self);
number.Append('020 7946 0018');
encoded := number.Encode('url_rfc3986','utf-8');
if (encoded <> 1) then
  begin
    Memo1.Lines.Add('Could not encode the phone number.');
    Exit;
  end;
url := TChilkatStringBuilder.Create(Self);
url.Append('https://lookups.twilio.com/v2/PhoneNumbers/');
url.Append(number.GetAsString());
url.Append('?CountryCode=GB');
resp := TChilkatHttpResponse.Create(Self);
reply := TChilkatJsonObject.Create(Self);

success := http.HttpNoBody('GET',url.GetAsString(),resp.ControlInterface);
if (resp.StatusCode = 0) then
  begin
    Memo1.Lines.Add('No HTTP response from Twilio.');
    Exit;
  end;
parsed := reply.Load(resp.BodyStr);
if ((success <> 1) or (resp.StatusCode <> 200)) then
  begin
    Memo1.Lines.Add('Twilio returned HTTP ' + IntToStr(resp.StatusCode));
    if ((parsed = 1) and (reply.JsonTypeOf('code') = 2)) 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 from Twilio.');
    Exit;
  end;
if (reply.JsonTypeOf('valid') <> 5) then
  begin
    Memo1.Lines.Add('The lookup response is missing the validity flag.');
    Exit;
  end;
if (reply.BoolOf('valid') <> 1) then
  begin
    Memo1.Lines.Add('The number is invalid for the specified country.');
    i := 0;
    while (i < reply.SizeOfArray('validation_errors')) do
      begin
        reply.I := i;
        Memo1.Lines.Add(reply.StringOf('validation_errors[i]'));
        i := i + 1;
      end;

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