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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        // Load an API key for the Twilio account from private application configuration.
        Http http = new Http();
        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 +.
        StringBuilder number = new StringBuilder();
        number.append("020 7946 0018");
        try {
            number.encode("url_rfc3986", "utf-8");
        } catch (ChilkatException e) {
            System.out.println("Could not encode the phone number.");
            return;
        }

        StringBuilder url = new StringBuilder();
        url.append("https://lookups.twilio.com/v2/PhoneNumbers/");
        url.append(number.getAsString());
        url.append("?CountryCode=GB");
        HttpResponse resp = new HttpResponse();
        JsonObject reply = new JsonObject();

        try {
            http.httpNoBody("GET", url.getAsString(), resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (resp.getStatusCode() == 0) {
            System.out.println("No HTTP response from Twilio.");
            return;
        }

        boolean parsed;
        try {
            reply.load(resp.getBodyStr());
            parsed = true;
        } catch (ChilkatException e) {
            parsed = false;
        }
        if ((!success) || (resp.getStatusCode() != 200)) {
            System.out.println("Twilio returned HTTP " + resp.getStatusCode());
            if ((parsed) && (reply.jsonTypeOf("code") == 2)) {
                System.out.println("Twilio error: " + reply.intOf("code") + "  " + reply.stringOf("message"));
            }

            return;
        }

        if (!parsed) {
            System.out.println("Expected a JSON response from Twilio.");
            return;
        }

        if (reply.jsonTypeOf("valid") != 5) {
            System.out.println("The lookup response is missing the validity flag.");
            return;
        }

        if (!reply.boolOf("valid")) {
            System.out.println("The number is invalid for the specified country.");
            int i = 0;
            while (i < reply.sizeOfArray("validation_errors")) {
                reply.setI(i);
                System.out.println(reply.stringOf("validation_errors[i]"));
                i = i + 1;
            }

            return;
        }

        if (reply.jsonTypeOf("phone_number") != 1) {
            System.out.println("The lookup response is missing the E.164 number.");
            return;
        }

        System.out.println("E.164: " + reply.stringOf("phone_number"));
        System.out.println("National format: " + reply.stringOf("national_format"));
    }
}