Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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.
Chilkat Delphi DLL Downloads
var
success: Boolean;
http: HCkHttp;
number: HCkStringBuilder;
encoded: Boolean;
url: HCkStringBuilder;
resp: HCkHttpResponse;
reply: HCkJsonObject;
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 := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putBasicAuth(http,True);
CkHttp_putLogin(http,'YOUR_API_KEY_SID');
CkHttp_putPassword(http,'YOUR_API_KEY_SECRET');
// The phone number is one URL path segment. Percent-encode spaces and any leading +.
number := CkStringBuilder_Create();
CkStringBuilder_Append(number,'020 7946 0018');
encoded := CkStringBuilder_Encode(number,'url_rfc3986','utf-8');
if (encoded <> True) then
begin
Memo1.Lines.Add('Could not encode the phone number.');
Exit;
end;
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://lookups.twilio.com/v2/PhoneNumbers/');
CkStringBuilder_Append(url,CkStringBuilder__getAsString(number));
CkStringBuilder_Append(url,'?CountryCode=GB');
resp := CkHttpResponse_Create();
reply := CkJsonObject_Create();
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('No HTTP response from Twilio.');
Exit;
end;
parsed := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
Memo1.Lines.Add('Twilio returned HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
if ((parsed = True) and (CkJsonObject_JsonTypeOf(reply,'code') = 2)) then
begin
Memo1.Lines.Add('Twilio error: ' + IntToStr(CkJsonObject_IntOf(reply,'code')) + ' ' + CkJsonObject__stringOf(reply,'message'));
end;
Exit;
end;
if (parsed <> True) then
begin
Memo1.Lines.Add('Expected a JSON response from Twilio.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(reply,'valid') <> 5) then
begin
Memo1.Lines.Add('The lookup response is missing the validity flag.');
Exit;
end;
if (CkJsonObject_BoolOf(reply,'valid') <> True) then
begin
Memo1.Lines.Add('The number is invalid for the specified country.');
i := 0;
while (i < CkJsonObject_SizeOfArray(reply,'validation_errors')) do
begin
CkJsonObject_putI(reply,i);
Memo1.Lines.Add(CkJsonObject__stringOf(reply,'validation_errors[i]'));
i := i + 1;
end;
Exit;
end;
if (CkJsonObject_JsonTypeOf(reply,'phone_number') <> 1) then
begin
Memo1.Lines.Add('The lookup response is missing the E.164 number.');
Exit;
end;
Memo1.Lines.Add('E.164: ' + CkJsonObject__stringOf(reply,'phone_number'));
Memo1.Lines.Add('National format: ' + CkJsonObject__stringOf(reply,'national_format'));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(number);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);