Verify v1 API has reached End of Sale. It is now closed to new customers and will be fully deprecated in the future.
For new development, we encourage you to use the Verify v2 API. v2 has an improved developer experience and new features, including:
Existing customers will not be impacted at this time until Verify v1 API has reached End of Life. For more information about migration, see Migrating from 1.x to 2.x.
Phone Verification is an important, high-confidence step in a registration flow to verify that a user has the device they claim to have. Adding Twilio Verify to your application to validate new accounts will greatly reduce your number of fraudulent registrations and protect future application users from having their numbers registered by scammers.
This quickstart guides you through creating a Java, JDK, Servlets and AngularJS app that requires a Phone Verification step to create an account. Two channels of Phone Verification are demoed: SMS and Voice.
Ready to add Twilio Verify to a demo app and keep the bad actors away?
Either sign up for a free Twilio trial, or sign into an existing Twilio account.
Once logged in, visit the Authy Console. Click on the red 'Create New Application' (or big red plus ('+') if you already created one) to create a new Authy application then name it something memorable.
Twilio will redirect you to the Settings page next:
Click the eyeball icon to reveal your Production API Key, and copy it somewhere safe. You will use the API Key during the application setup step below.
Start by cloning our Servlets repository. Enter the directory and use gradle to install all of our dependencies:
gradle build
.env.example
ACCOUNT_SECURITY_API_KEY
to the API Key from the above step.env
source .env
In Windows, set the ACCOUNT_SECURITY_API_KEY
variable manually.
Enter the API Key from the Authy console and optionally change the port.
1# You can get/create one here :2# https://www.twilio.com/console/authy/applications3ACCOUNT_SECURITY_API_KEY=ENTER_SECRET_HERE
That's all the setup you'll need.
Now, launch the application with:
gradle appRun
Assuming your API Key is correctly entered, you'll soon get a message that the app is up!
Keeping your phone at your side, visit the Phone Verification page of the demo at http://localhost:8080/verification/
Enter a Country Code
and Phone Number
, then choose which channel to request verification over, 'SMS' or 'CALL' (Voice). Finally, hit the blue 'Request Verification' button and wait.
You'll either receive a phone call or an SMS with the verification token. If you requested a phone call, as an additional security feature you may need to interact to proceed (entering a number on the phone keypad).
This function allows you to send the verification code over SMS or Voice depending on the 'via' variable.
1package com.twilio.accountsecurity.services;23import com.authy.AuthyApiClient;4import com.authy.api.Params;5import com.authy.api.Verification;6import com.twilio.accountsecurity.exceptions.TokenVerificationException;7import com.twilio.accountsecurity.servlets.requests.CheckPhoneVerificationRequest;8import com.twilio.accountsecurity.servlets.requests.StartPhoneVerificationRequest;9import org.slf4j.Logger;10import org.slf4j.LoggerFactory;1112import static com.twilio.accountsecurity.config.Settings.authyId;1314public class PhoneVerificationService {1516private static final Logger LOGGER = LoggerFactory.getLogger(TokenService.class);1718private AuthyApiClient authyApiClient;1920public PhoneVerificationService(AuthyApiClient authyApiClient) {21this.authyApiClient = authyApiClient;22}2324public PhoneVerificationService() {25this.authyApiClient = new AuthyApiClient(authyId());26}2728public void start(StartPhoneVerificationRequest request) {29Params params = new Params();30params.setAttribute("code_length", "4");31Verification verification = authyApiClient32.getPhoneVerification()33.start(request.getPhoneNumber(),34request.getCountryCode(),35request.getVia(),36params);3738if(!verification.isOk()) {39logAndThrow("Error requesting phone verification. " +40verification.getMessage());41}42}4344public void check(CheckPhoneVerificationRequest request) {45Verification verification = authyApiClient46.getPhoneVerification()47.check(request.getPhoneNumber(),48request.getCountryCode(),49request.getToken());5051if(!verification.isOk()) {52logAndThrow("Error verifying token. " + verification.getMessage());53}54}5556private void logAndThrow(String message) {57LOGGER.warn(message);58throw new TokenVerificationException(message);59}60}
Either way you requested the passcode, enter the token into the Verification entry form and click 'Verify Phone':
This function verifies the token for a user delivered over the Voice or SMS channel.
1package com.twilio.accountsecurity.services;23import com.authy.AuthyApiClient;4import com.authy.api.Params;5import com.authy.api.Verification;6import com.twilio.accountsecurity.exceptions.TokenVerificationException;7import com.twilio.accountsecurity.servlets.requests.CheckPhoneVerificationRequest;8import com.twilio.accountsecurity.servlets.requests.StartPhoneVerificationRequest;9import org.slf4j.Logger;10import org.slf4j.LoggerFactory;1112import static com.twilio.accountsecurity.config.Settings.authyId;1314public class PhoneVerificationService {1516private static final Logger LOGGER = LoggerFactory.getLogger(TokenService.class);1718private AuthyApiClient authyApiClient;1920public PhoneVerificationService(AuthyApiClient authyApiClient) {21this.authyApiClient = authyApiClient;22}2324public PhoneVerificationService() {25this.authyApiClient = new AuthyApiClient(authyId());26}2728public void start(StartPhoneVerificationRequest request) {29Params params = new Params();30params.setAttribute("code_length", "4");31Verification verification = authyApiClient32.getPhoneVerification()33.start(request.getPhoneNumber(),34request.getCountryCode(),35request.getVia(),36params);3738if(!verification.isOk()) {39logAndThrow("Error requesting phone verification. " +40verification.getMessage());41}42}4344public void check(CheckPhoneVerificationRequest request) {45Verification verification = authyApiClient46.getPhoneVerification()47.check(request.getPhoneNumber(),48request.getCountryCode(),49request.getToken());5051if(!verification.isOk()) {52logAndThrow("Error verifying token. " + verification.getMessage());53}54}5556private void logAndThrow(String message) {57LOGGER.warn(message);58throw new TokenVerificationException(message);59}60}
And with that, your demo app is protected with Twilio's Phone Verification! You can now log out to try the untried channel.
Your demo app is now keeping fraudulent users from registering with your business and polluting your database. Next, check out all of the variables and options available to you in the Phone Verification API Reference. Also, to protect your customers in an ongoing manner (with this same codebase) try the Java Servlets Authy Two-Factor Authentication Quickstart.
After that, visit the Docs for more Account Security demos and tutorials and web applications using all of Twilio's products.