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

Send an SMS verification code with Twilio Verify

See more Twilio Examples

Live SMS. When a user requests a code, pass their phone number in E.164 format (for example, +12025550123) and select the SMS channel. Create a Verify Service in the Twilio Console first. Supply its VA service SID and an API key with access to that service. For a live test, use a phone you control. Messaging and verification charges may apply.

Twilio generates the code and sends it to the phone. A pending response means the application can prompt for the code. Save the returned VE verification SID with the user and phone number that initiated the request, then use it to check the code entered by the user.

Twilio Verify API reference

Chilkat CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

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

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Obtain these credentials from the application's private configuration.
    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    http.put_BasicAuth(true);
    http.put_Login("YOUR_API_KEY_SID");
    http.put_Password("YOUR_API_KEY_SECRET");

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

    CkHttpRequest req = new CkHttpRequest();
    req.put_HttpVerb("POST");
    req.put_ContentType("application/x-www-form-urlencoded");
    //  Supply the number the user asked to verify. AddParam encodes the leading plus sign.
    req.AddParam("To","PHONE_NUMBER_IN_E164_FORMAT");
    req.AddParam("Channel","sms");

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpReq(url.getAsString(),req,resp);
    if (resp.get_StatusCode() == 0) {
        System.out.println("No response from Twilio. Check connectivity and TLS settings.");
        return;
        }

    CkJsonObject result = new CkJsonObject();
    boolean parsed = result.Load(resp.bodyStr());
    if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        System.out.println("Twilio returned HTTP " + resp.get_StatusCode());
        if ((parsed == true) && (result.JsonTypeOf("code") == 2)) {
            System.out.println("Twilio error code: " + result.IntOf("code"));
            }

        return;
        }

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

    String status = result.stringOf("status");
    if (status != "pending") {
        System.out.println("Verification status: " + status);
        return;
        }

    if ((result.JsonTypeOf("sid") != 1) || (result.stringOf("sid") == "")) {
        System.out.println("The pending response did not include a verification SID.");
        return;
        }

    //  Save this SID with the pending attempt before prompting the user for the code.
    System.out.println("Pending verification SID: " + result.stringOf("sid"));
  }
}