Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Check an SMS verification code with Twilio Verify

See more Twilio Examples

After the user enters the SMS code, retrieve the VE verification SID saved when you started the verification. Send that SID and the entered code to the same Verify Service. Keep the code as text so a leading zero survives.

Mark the phone number on that pending attempt as verified only when Twilio returns status=approved. A pending check leaves the number unverified. Twilio can return HTTP 404 after expiration, approval, or exhaustion of check attempts. Keep a successful result in your application so it can be used after Twilio removes the verification.

Twilio Verify API reference

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
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.
        // Obtain these credentials from the application's private configuration.
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setBasicAuth(true);
        http.setLogin("YOUR_API_KEY_SID");
        http.setPassword("YOUR_API_KEY_SECRET");

        // Both requests use the same Verify Service SID (VA...).
        StringBuilder url = new StringBuilder();
        url.append("https://verify.twilio.com/v2/Services/");
        url.append("YOUR_VERIFY_SERVICE_SID");
        url.append("/VerificationCheck");

        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setContentType("application/x-www-form-urlencoded");
        // Load the saved SID for this user's pending attempt. Take only the code from user input.
        String verificationSid = "SAVED_VERIFICATION_SID";
        String enteredCode = "CODE_ENTERED_BY_USER";
        req.addParam("VerificationSid", verificationSid);
        req.addParam("Code", enteredCode);

        HttpResponse resp = new HttpResponse();
        try {
            http.httpReq(url.getAsString(), req, resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (resp.getStatusCode() == 0) {
            System.out.println("No response from Twilio. Check connectivity and TLS settings.");
            return;
        }

        if (resp.getStatusCode() == 404) {
            System.out.println("No active verification found. Check the service and verification SIDs.");
            return;
        }

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

            return;
        }

        if ((!parsed) || (result.jsonTypeOf("status") != 1)) {
            System.out.println("Verification status missing from the response.");
            return;
        }

        String status = result.stringOf("status");
        if ("approved".equals(status)) {
            // Record success for the user and phone number associated with verificationSid.
            System.out.println("Phone number verified.");
        } else {
            if ("pending".equals(status)) {
                System.out.println("Code not accepted. Check the digits and enter the code again.");
            } else {
                System.out.println("Phone number unverified. Verification status: " + status);
            }
        }
    }
}