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 phone verification to your application 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, Spring 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? Enter stage left!
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 Spring repository. Enter the directory and use npm to install all of our dependencies:
_10gradle build
.env.example
ACCOUNT_SECURITY_API_KEY
to the API Key from the above step
.env
Enter the API Key from the Authy console and optionally change the port.
_10# You can get/create one here :_10# https://www.twilio.com/console/authy/applications_10export ACCOUNT_SECURITY_API_KEY=ENTER_SECRET_HERE
And that's all the setup you'll need!
Now, launch the application with:
_10gradle 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 won't be waiting long - 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 (by 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.
_51package com.twilio.accountsecurity.services;_51_51import com.authy.AuthyApiClient;_51import com.authy.api.Params;_51import com.authy.api.Verification;_51import com.twilio.accountsecurity.exceptions.TokenVerificationException;_51import org.slf4j.Logger;_51import org.slf4j.LoggerFactory;_51import org.springframework.beans.factory.annotation.Autowired;_51import org.springframework.stereotype.Service;_51_51@Service_51public class PhoneVerificationService {_51_51 private static final Logger LOGGER = LoggerFactory.getLogger(TokenService.class);_51_51 private AuthyApiClient authyApiClient;_51_51 @Autowired_51 public PhoneVerificationService(AuthyApiClient authyApiClient) {_51 this.authyApiClient = authyApiClient;_51 }_51_51 public void start(String countryCode, String phoneNumber, String via) {_51 Params params = new Params();_51 params.setAttribute("code_length", "4");_51 Verification verification = authyApiClient_51 .getPhoneVerification()_51 .start(phoneNumber, countryCode, via, params);_51_51 if(!verification.isOk()) {_51 logAndThrow("Error requesting phone verification. " +_51 verification.getMessage());_51 }_51 }_51_51 public void verify(String countryCode, String phoneNumber, String token) {_51 Verification verification = authyApiClient_51 .getPhoneVerification()_51 .check(phoneNumber, countryCode, token);_51_51 if(!verification.isOk()) {_51 logAndThrow("Error verifying token. " + verification.getMessage());_51 }_51 }_51_51 private void logAndThrow(String message) {_51 LOGGER.warn(message);_51 throw new TokenVerificationException(message);_51 }_51}
Either way you requested the passcode, enter the token into the Verification entry form and click 'Verify Phone':
This function verifies the token delivered over the Voice or SMS channel.
_51package com.twilio.accountsecurity.services;_51_51import com.authy.AuthyApiClient;_51import com.authy.api.Params;_51import com.authy.api.Verification;_51import com.twilio.accountsecurity.exceptions.TokenVerificationException;_51import org.slf4j.Logger;_51import org.slf4j.LoggerFactory;_51import org.springframework.beans.factory.annotation.Autowired;_51import org.springframework.stereotype.Service;_51_51@Service_51public class PhoneVerificationService {_51_51 private static final Logger LOGGER = LoggerFactory.getLogger(TokenService.class);_51_51 private AuthyApiClient authyApiClient;_51_51 @Autowired_51 public PhoneVerificationService(AuthyApiClient authyApiClient) {_51 this.authyApiClient = authyApiClient;_51 }_51_51 public void start(String countryCode, String phoneNumber, String via) {_51 Params params = new Params();_51 params.setAttribute("code_length", "4");_51 Verification verification = authyApiClient_51 .getPhoneVerification()_51 .start(phoneNumber, countryCode, via, params);_51_51 if(!verification.isOk()) {_51 logAndThrow("Error requesting phone verification. " +_51 verification.getMessage());_51 }_51 }_51_51 public void verify(String countryCode, String phoneNumber, String token) {_51 Verification verification = authyApiClient_51 .getPhoneVerification()_51 .check(phoneNumber, countryCode, token);_51_51 if(!verification.isOk()) {_51 logAndThrow("Error verifying token. " + verification.getMessage());_51 }_51 }_51_51 private void logAndThrow(String message) {_51 LOGGER.warn(message);_51 throw new TokenVerificationException(message);_51 }_51}
And with that, your demo app is protected with Twilio's Verify! You can now log out to try the other channel.
Your demo app is now keeping hordes of fraudulent users from registering with your business and polluting the database. Next, check out all of the variables and options available to you in the Verify API Reference. Also, for protecting your customers in an ongoing manner (with this same codebase) try the Java Spring Authy Two-Factor Authentication Quickstart.
After that, take a stroll through the Docs for more Account Security demos and tutorials - as well as sample web applications using all of Twilio's products.