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

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oNumber
LOCAL nEncoded
LOCAL oUrl
LOCAL oResp
LOCAL oReply
LOCAL nParsed
LOCAL i

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:BasicAuth := 1
oHttp:Login := "YOUR_API_KEY_SID"
oHttp:Password := "YOUR_API_KEY_SECRET"

//  The phone number is one URL path segment. Percent-encode spaces and any leading +.
oNumber := CreateObject("Chilkat.StringBuilder")
oNumber:Append("020 7946 0018")
nEncoded := oNumber:Encode("url_rfc3986", "utf-8")
IF (nEncoded != 1)
    ? "Could not encode the phone number."
    oHttp:destroy()
    oNumber:destroy()
    RETURN
ENDIF

oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://lookups.twilio.com/v2/PhoneNumbers/")
oUrl:Append(oNumber:GetAsString())
oUrl:Append("?CountryCode=GB")
oResp := CreateObject("Chilkat.HttpResponse")
oReply := CreateObject("Chilkat.JsonObject")

nSuccess := oHttp:HttpNoBody("GET", oUrl:GetAsString(), oResp)
IF (oResp:StatusCode == 0)
    ? "No HTTP response from Twilio."
    oHttp:destroy()
    oNumber:destroy()
    oUrl:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

nParsed := oReply:Load(oResp:BodyStr)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
    ? "Twilio returned HTTP " + Str(oResp:StatusCode)
    IF ((nParsed == 1) .AND. (oReply:JsonTypeOf("code") == 2))
        ? "Twilio error: " + Str(oReply:IntOf("code")) + "  " + oReply:StringOf("message")
    ENDIF

    oHttp:destroy()
    oNumber:destroy()
    oUrl:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (nParsed != 1)
    ? "Expected a JSON response from Twilio."
    oHttp:destroy()
    oNumber:destroy()
    oUrl:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:JsonTypeOf("valid") != 5)
    ? "The lookup response is missing the validity flag."
    oHttp:destroy()
    oNumber:destroy()
    oUrl:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:BoolOf("valid") != 1)
    ? "The number is invalid for the specified country."
    i := 0
    DO WHILE (i < oReply:SizeOfArray("validation_errors"))
        oReply:I := i
        ? oReply:StringOf("validation_errors[i]")
        i := i + 1
    ENDDO
    oHttp:destroy()
    oNumber:destroy()
    oUrl:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:JsonTypeOf("phone_number") != 1)
    ? "The lookup response is missing the E.164 number."
    oHttp:destroy()
    oNumber:destroy()
    oUrl:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

? "E.164: " + oReply:StringOf("phone_number")
? "National format: " + oReply:StringOf("national_format")

oHttp:destroy()
oNumber:destroy()
oUrl:destroy()
oResp:destroy()
oReply:destroy()