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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Number
long li_Encoded
oleobject loo_Url
oleobject loo_Resp
oleobject loo_Reply
long li_Parsed
long i

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
loo_Http.Login = "YOUR_API_KEY_SID"
loo_Http.Password = "YOUR_API_KEY_SECRET"

//  The phone number is one URL path segment. Percent-encode spaces and any leading +.
loo_Number = create oleobject
li_rc = loo_Number.ConnectToNewObject("Chilkat.StringBuilder")

loo_Number.Append("020 7946 0018")
li_Encoded = loo_Number.Encode("url_rfc3986","utf-8")
if li_Encoded <> 1 then
    Write-Debug "Could not encode the phone number."
    destroy loo_Http
    destroy loo_Number
    return
end if

loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://lookups.twilio.com/v2/PhoneNumbers/")
loo_Url.Append(loo_Number.GetAsString())
loo_Url.Append("?CountryCode=GB")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "No HTTP response from Twilio."
    destroy loo_Http
    destroy loo_Number
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Reply
    return
end if

li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
    Write-Debug "Twilio returned HTTP " + string(loo_Resp.StatusCode)
    if (li_Parsed = 1) AND (loo_Reply.JsonTypeOf("code") = 2) then
        Write-Debug "Twilio error: " + string(loo_Reply.IntOf("code")) + "  " + loo_Reply.StringOf("message")
    end if

    destroy loo_Http
    destroy loo_Number
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if li_Parsed <> 1 then
    Write-Debug "Expected a JSON response from Twilio."
    destroy loo_Http
    destroy loo_Number
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.JsonTypeOf("valid") <> 5 then
    Write-Debug "The lookup response is missing the validity flag."
    destroy loo_Http
    destroy loo_Number
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.BoolOf("valid") <> 1 then
    Write-Debug "The number is invalid for the specified country."
    i = 0
    do while (i < loo_Reply.SizeOfArray("validation_errors"))
        loo_Reply.I = i
        Write-Debug loo_Reply.StringOf("validation_errors[i]")
        i = i + 1
    loop
    destroy loo_Http
    destroy loo_Number
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.JsonTypeOf("phone_number") <> 1 then
    Write-Debug "The lookup response is missing the E.164 number."
    destroy loo_Http
    destroy loo_Number
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Reply
    return
end if

Write-Debug "E.164: " + loo_Reply.StringOf("phone_number")
Write-Debug "National format: " + loo_Reply.StringOf("national_format")


destroy loo_Http
destroy loo_Number
destroy loo_Url
destroy loo_Resp
destroy loo_Reply