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

Xojo Plugin
Dim success As Boolean
success = False

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
Dim http As New Chilkat.Http
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 +.
Dim number As New Chilkat.StringBuilder
success = number.Append("020 7946 0018")
Dim encoded As Boolean
encoded = number.Encode("url_rfc3986","utf-8")
If (encoded <> True) Then
    System.DebugLog("Could not encode the phone number.")
    Return
End If

Dim url As New Chilkat.StringBuilder
success = url.Append("https://lookups.twilio.com/v2/PhoneNumbers/")
success = url.Append(number.GetAsString())
success = url.Append("?CountryCode=GB")
Dim resp As New Chilkat.HttpResponse
Dim reply As New Chilkat.JsonObject

success = http.HttpNoBody("GET",url.GetAsString(),resp)
If (resp.StatusCode = 0) Then
    System.DebugLog("No HTTP response from Twilio.")
    Return
End If

Dim parsed As Boolean
parsed = reply.Load(resp.BodyStr)
If ((success <> True) Or (resp.StatusCode <> 200)) Then
    System.DebugLog("Twilio returned HTTP " + Str(resp.StatusCode))
    If ((parsed = True) And (reply.JsonTypeOf("code") = 2)) Then
        System.DebugLog("Twilio error: " + Str(reply.IntOf("code")) + "  " + reply.StringOf("message"))
    End If

    Return
End If

If (parsed <> True) Then
    System.DebugLog("Expected a JSON response from Twilio.")
    Return
End If

If (reply.JsonTypeOf("valid") <> 5) Then
    System.DebugLog("The lookup response is missing the validity flag.")
    Return
End If

If (reply.BoolOf("valid") <> True) Then
    System.DebugLog("The number is invalid for the specified country.")
    Dim i As Int32
    i = 0
    While (i < reply.SizeOfArray("validation_errors"))
        reply.I = i
        System.DebugLog(reply.StringOf("validation_errors[i]"))
        i = i + 1
    Wend
    Return
End If

If (reply.JsonTypeOf("phone_number") <> 1) Then
    System.DebugLog("The lookup response is missing the E.164 number.")
    Return
End If

System.DebugLog("E.164: " + reply.StringOf("phone_number"))
System.DebugLog("National format: " + reply.StringOf("national_format"))