Go Requires Chilkat v11.0.0+
Go
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 Go Downloads
success := false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
http := chilkat.NewHttp()
http.SetRequireSslCertVerify(true)
http.SetBasicAuth(true)
http.SetLogin("YOUR_API_KEY_SID")
http.SetPassword("YOUR_API_KEY_SECRET")
// The phone number is one URL path segment. Percent-encode spaces and any leading +.
number := chilkat.NewStringBuilder()
number.Append("020 7946 0018")
encoded := number.Encode("url_rfc3986","utf-8")
if encoded != true {
fmt.Println("Could not encode the phone number.")
http.DisposeHttp()
number.DisposeStringBuilder()
return
}
url := chilkat.NewStringBuilder()
url.Append("https://lookups.twilio.com/v2/PhoneNumbers/")
url.Append(*number.GetAsString())
url.Append("?CountryCode=GB")
resp := chilkat.NewHttpResponse()
reply := chilkat.NewJsonObject()
success = http.HttpNoBody("GET",*url.GetAsString(),resp)
if resp.StatusCode() == 0 {
fmt.Println("No HTTP response from Twilio.")
http.DisposeHttp()
number.DisposeStringBuilder()
url.DisposeStringBuilder()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
return
}
parsed := reply.Load(resp.BodyStr())
if (success != true) || (resp.StatusCode() != 200) {
fmt.Println("Twilio returned HTTP ", resp.StatusCode())
if (parsed == true) && (reply.JsonTypeOf("code") == 2) {
fmt.Println("Twilio error: ", reply.IntOf("code"), " ", *reply.StringOf("message"))
}
http.DisposeHttp()
number.DisposeStringBuilder()
url.DisposeStringBuilder()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
return
}
if parsed != true {
fmt.Println("Expected a JSON response from Twilio.")
http.DisposeHttp()
number.DisposeStringBuilder()
url.DisposeStringBuilder()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
return
}
if reply.JsonTypeOf("valid") != 5 {
fmt.Println("The lookup response is missing the validity flag.")
http.DisposeHttp()
number.DisposeStringBuilder()
url.DisposeStringBuilder()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
return
}
if reply.BoolOf("valid") != true {
fmt.Println("The number is invalid for the specified country.")
i := 0
for (i < reply.SizeOfArray("validation_errors")) {
reply.SetI(i)
fmt.Println(*reply.StringOf("validation_errors[i]"))
i = i + 1
}
http.DisposeHttp()
number.DisposeStringBuilder()
url.DisposeStringBuilder()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
return
}
if reply.JsonTypeOf("phone_number") != 1 {
fmt.Println("The lookup response is missing the E.164 number.")
http.DisposeHttp()
number.DisposeStringBuilder()
url.DisposeStringBuilder()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
return
}
fmt.Println("E.164: ", *reply.StringOf("phone_number"))
fmt.Println("National format: ", *reply.StringOf("national_format"))
http.DisposeHttp()
number.DisposeStringBuilder()
url.DisposeStringBuilder()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()