Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Account Verification with Authy, Java and Servlets


(warning)

Warning

As of November 2022, Twilio no longer provides support for Authy SMS/Voice-only customers. Customers who were also using Authy TOTP or Push prior to March 1, 2023 are still supported. The Authy API 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.

Existing customers will not be impacted at this time until Authy API has reached End of Life. For more information about migration, see Migrating from Authy to Verify for SMS(link takes you to an external page).

Ready to implement user account verification in your application? Here's how it works at a high level:

  1. The users begin the registration process by entering their data, including a phone number, into a signup form.
  2. The authentication system sends a one-time password to the user's mobile phone to verify the possession of that phone number.
  3. The user enters the one-time password into a form before completing registration.
  4. The user opens a success page and receives an SMS indicating that their account has been created.

Building Blocks

building-blocks page anchor

To get this done, you'll be working with the following Twilio-powered APIs:

Authy REST API

Twilio REST API

  • Messages Resource : We will use Twilio directly to send our user a confirmation message after they create an account.

Deployment descriptor

deployment-descriptor page anchor
src/main/webapp/WEB-INF/web.xml

_75
<?xml version="1.0" encoding="UTF-8"?>
_75
_75
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
_75
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
_75
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
_75
version="3.0">
_75
<welcome-file-list>
_75
<welcome-file>home.html</welcome-file>
_75
</welcome-file-list>
_75
<servlet>
_75
<servlet-name>registration</servlet-name>
_75
<servlet-class>com.twilio.verification.servlet.RegistrationServlet</servlet-class>
_75
</servlet>
_75
<servlet>
_75
<servlet-name>verify-code</servlet-name>
_75
<servlet-class>com.twilio.verification.servlet.VerifyCodeServlet</servlet-class>
_75
</servlet>
_75
<servlet>
_75
<servlet-name>account</servlet-name>
_75
<servlet-class>com.twilio.verification.servlet.AccountServlet</servlet-class>
_75
</servlet>
_75
<servlet>
_75
<servlet-name>resend-token</servlet-name>
_75
<servlet-class>com.twilio.verification.servlet.ResendTokenServlet</servlet-class>
_75
</servlet>
_75
_75
<servlet-mapping>
_75
<servlet-name>registration</servlet-name>
_75
<url-pattern>/registration</url-pattern>
_75
</servlet-mapping>
_75
<servlet-mapping>
_75
<servlet-name>verify-code</servlet-name>
_75
<url-pattern>/verify-code</url-pattern>
_75
</servlet-mapping>
_75
<servlet-mapping>
_75
<servlet-name>account</servlet-name>
_75
<url-pattern>/account</url-pattern>
_75
</servlet-mapping>
_75
<servlet-mapping>
_75
<servlet-name>resend-token</servlet-name>
_75
<url-pattern>/resend-token</url-pattern>
_75
</servlet-mapping>
_75
_75
<filter>
_75
<filter-name>authentication</filter-name>
_75
<filter-class>com.twilio.verification.filter.AuthenticationFilter</filter-class>
_75
</filter>
_75
_75
<filter-mapping>
_75
<filter-name>authentication</filter-name>
_75
<url-pattern>/account</url-pattern>
_75
<url-pattern>/verify-code</url-pattern>
_75
<url-pattern>/resend-token</url-pattern>
_75
</filter-mapping>
_75
_75
<filter>
_75
<filter-name>TeeFilter</filter-name>
_75
<filter-class>ch.qos.logback.access.servlet.TeeFilter</filter-class>
_75
</filter>
_75
_75
<filter-mapping>
_75
<filter-name>TeeFilter</filter-name>
_75
<url-pattern>/*</url-pattern>
_75
</filter-mapping>
_75
_75
<filter>
_75
<filter-name>LoggingFilter</filter-name>
_75
<filter-class>com.twilio.verification.logging.LoggingFilter</filter-class>
_75
</filter>
_75
_75
<filter-mapping>
_75
<filter-name>LoggingFilter</filter-name>
_75
<url-pattern>/*</url-pattern>
_75
</filter-mapping>
_75
</web-app>

Let's get started!


The User Model for this use-case is pretty straightforward and JPA offers us some tools to make it even simpler. If you have already read through the 2FA tutorial this one probably looks very similar. We need to make sure that our User model contains a phone number, country code so that the user can be verified with Authy.

User model definition and JPA mappings

user-model-definition-and-jpa-mappings page anchor
src/main/java/com/twilio/verification/model/User.java

_128
package com.twilio.verification.model;
_128
_128
import javax.persistence.*;
_128
import java.util.Date;
_128
_128
@SuppressWarnings("unused")
_128
@Entity
_128
@Table(name = "users")
_128
public class User {
_128
@Id
_128
@GeneratedValue(strategy = GenerationType.IDENTITY)
_128
@Column(name = "id")
_128
private int id;
_128
_128
@Column(name = "name")
_128
private String name;
_128
_128
@Column(name = "email")
_128
private String email;
_128
_128
@Column(name = "password")
_128
private String password;
_128
_128
@Column(name = "country_code")
_128
private String countryCode;
_128
_128
@Column(name = "phone_number")
_128
private String phoneNumber;
_128
_128
@Column(name = "authy_id")
_128
private int authyId;
_128
_128
@Column(name = "verified")
_128
private boolean verified;
_128
_128
@Column(name = "date")
_128
private Date date;
_128
_128
public User() {
_128
}
_128
_128
public User(String name, String email, String password, String countryCode, String phoneNumber, int authyId) {
_128
this.name = name;
_128
this.email = email;
_128
this.password = password;
_128
this.countryCode = countryCode;
_128
this.phoneNumber = phoneNumber;
_128
this.authyId = authyId;
_128
this.verified = false;
_128
this.date = new Date();
_128
}
_128
_128
public int getId() {
_128
return id;
_128
}
_128
_128
public void setId(int id) {
_128
this.id = id;
_128
}
_128
_128
public String getName() {
_128
return name;
_128
}
_128
_128
public void setName(String name) {
_128
this.name = name;
_128
}
_128
_128
public String getEmail() {
_128
return email;
_128
}
_128
_128
public void setEmail(String email) {
_128
this.email = email;
_128
}
_128
_128
public String getPassword() {
_128
return password;
_128
}
_128
_128
public void setPassword(String password) {
_128
this.password = password;
_128
}
_128
_128
public String getCountryCode() {
_128
return countryCode;
_128
}
_128
_128
public void setCountryCode(String countryCode) {
_128
this.countryCode = countryCode;
_128
}
_128
_128
public String getPhoneNumber() {
_128
return phoneNumber;
_128
}
_128
_128
public void setPhoneNumber(String phoneNumber) {
_128
this.phoneNumber = phoneNumber;
_128
}
_128
_128
public String getFullPhoneNumber() {
_128
return countryCode + phoneNumber;
_128
}
_128
_128
public int getAuthyId() {
_128
return authyId;
_128
}
_128
_128
public void setAuthyId(int authyId) {
_128
this.authyId = authyId;
_128
}
_128
_128
public boolean isVerified() {
_128
return verified;
_128
}
_128
_128
public void setVerified(boolean verified) {
_128
this.verified = verified;
_128
}
_128
_128
public Date getDate() {
_128
return date;
_128
}
_128
_128
public void setDate(Date date) {
_128
this.date = date;
_128
}
_128
}

Next we will see how to handle the new user form.


When we create a new user, we ask for a name, email address, and a password. In order to validate a new account we also ask the user for a mobile number with a country code. We will use Authy to send a one-time password via SMS to this phone number.

It is now the servlet's responsibility to verify that the user provides the necessary information to create a new user. If the user is created successfully, they will be logged into the system automatically.

Handle registration of new accounts

handle-registration-of-new-accounts page anchor
src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

_85
package com.twilio.verification.servlet;
_85
_85
import com.authy.AuthyApiClient;
_85
import com.twilio.verification.lib.RequestParametersValidator;
_85
import com.twilio.verification.lib.SessionManager;
_85
import com.twilio.verification.model.User;
_85
import com.twilio.verification.repository.UsersRepository;
_85
_85
import javax.servlet.ServletException;
_85
import javax.servlet.http.HttpServlet;
_85
import javax.servlet.http.HttpServletRequest;
_85
import javax.servlet.http.HttpServletResponse;
_85
import java.io.IOException;
_85
_85
public class RegistrationServlet extends HttpServlet {
_85
_85
private final UsersRepository usersRepository;
_85
private final SessionManager sessionManager;
_85
private final AuthyApiClient authyClient;
_85
_85
@SuppressWarnings("unused")
_85
public RegistrationServlet() {
_85
this(
_85
new UsersRepository(),
_85
new SessionManager(),
_85
new AuthyApiClient(System.getenv("AUTHY_API_KEY"))
_85
);
_85
}
_85
_85
public RegistrationServlet(UsersRepository usersRepository, SessionManager sessionManager, AuthyApiClient authyClient) {
_85
this.usersRepository = usersRepository;
_85
this.sessionManager = sessionManager;
_85
this.authyClient = authyClient;
_85
}
_85
_85
protected void doPost(HttpServletRequest request, HttpServletResponse response)
_85
throws ServletException, IOException {
_85
_85
String name = request.getParameter("name");
_85
String email = request.getParameter("email");
_85
String password = request.getParameter("password");
_85
String countryCode = request.getParameter("countryCode");
_85
String phoneNumber = request.getParameter("phoneNumber");
_85
_85
if (validateRequest(request)) {
_85
_85
com.authy.api.User authyUser = authyClient.getUsers().createUser(email, phoneNumber, countryCode);
_85
_85
// Create user remotely
_85
if (authyUser.isOk()) {
_85
int authyUserId = authyUser.getId();
_85
// Create user locally
_85
User user = usersRepository.create(new User(name, email, password, countryCode, phoneNumber, authyUserId));
_85
_85
// Request SMS authentication
_85
authyClient.getUsers().requestSms(authyUserId);
_85
_85
sessionManager.partialLogIn(request, user.getId());
_85
response.sendRedirect("/verify-code");
_85
}
_85
} else {
_85
preserveStatusRequest(request, name, email, countryCode, phoneNumber);
_85
request.getRequestDispatcher("/registration.jsp").forward(request, response);
_85
}
_85
}
_85
_85
private boolean validateRequest(HttpServletRequest request) {
_85
RequestParametersValidator validator = new RequestParametersValidator(request);
_85
_85
return validator.validatePresence("name", "email", "password", "countryCode", "phoneNumber")
_85
&& validator.validateEmail("email");
_85
}
_85
_85
private void preserveStatusRequest(
_85
HttpServletRequest request,
_85
String name,
_85
String email,
_85
String countryCode,
_85
String phoneNumber) {
_85
request.setAttribute("name", name);
_85
request.setAttribute("email", email);
_85
request.setAttribute("countryCode", countryCode);
_85
request.setAttribute("phoneNumber", phoneNumber);
_85
}
_85
}

Now the user is logged in but not verified. In the next steps we'll learn how to verify the user using Authy.


In .environment we list configuration parameters for the application. These are pulled from system environment variables, which is a helpful way to access sensitive values (like API keys). This prevents us from accidentally checking them into source control. We use the System.getenv method to load the key and inject the AuthyApiClient into the RegistrationServlet class.

Now we need our Authy production key (sign up for Authy here(link takes you to an external page)). When you create an Authy application the production key is found on the dashboard.

Authy dashboard.

Register your AuthyAPIClient with your Authy API key

register-your-authyapiclient-with-your-authy-api-key page anchor
src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

_85
package com.twilio.verification.servlet;
_85
_85
import com.authy.AuthyApiClient;
_85
import com.twilio.verification.lib.RequestParametersValidator;
_85
import com.twilio.verification.lib.SessionManager;
_85
import com.twilio.verification.model.User;
_85
import com.twilio.verification.repository.UsersRepository;
_85
_85
import javax.servlet.ServletException;
_85
import javax.servlet.http.HttpServlet;
_85
import javax.servlet.http.HttpServletRequest;
_85
import javax.servlet.http.HttpServletResponse;
_85
import java.io.IOException;
_85
_85
public class RegistrationServlet extends HttpServlet {
_85
_85
private final UsersRepository usersRepository;
_85
private final SessionManager sessionManager;
_85
private final AuthyApiClient authyClient;
_85
_85
@SuppressWarnings("unused")
_85
public RegistrationServlet() {
_85
this(
_85
new UsersRepository(),
_85
new SessionManager(),
_85
new AuthyApiClient(System.getenv("AUTHY_API_KEY"))
_85
);
_85
}
_85
_85
public RegistrationServlet(UsersRepository usersRepository, SessionManager sessionManager, AuthyApiClient authyClient) {
_85
this.usersRepository = usersRepository;
_85
this.sessionManager = sessionManager;
_85
this.authyClient = authyClient;
_85
}
_85
_85
protected void doPost(HttpServletRequest request, HttpServletResponse response)
_85
throws ServletException, IOException {
_85
_85
String name = request.getParameter("name");
_85
String email = request.getParameter("email");
_85
String password = request.getParameter("password");
_85
String countryCode = request.getParameter("countryCode");
_85
String phoneNumber = request.getParameter("phoneNumber");
_85
_85
if (validateRequest(request)) {
_85
_85
com.authy.api.User authyUser = authyClient.getUsers().createUser(email, phoneNumber, countryCode);
_85
_85
// Create user remotely
_85
if (authyUser.isOk()) {
_85
int authyUserId = authyUser.getId();
_85
// Create user locally
_85
User user = usersRepository.create(new User(name, email, password, countryCode, phoneNumber, authyUserId));
_85
_85
// Request SMS authentication
_85
authyClient.getUsers().requestSms(authyUserId);
_85
_85
sessionManager.partialLogIn(request, user.getId());
_85
response.sendRedirect("/verify-code");
_85
}
_85
} else {
_85
preserveStatusRequest(request, name, email, countryCode, phoneNumber);
_85
request.getRequestDispatcher("/registration.jsp").forward(request, response);
_85
}
_85
}
_85
_85
private boolean validateRequest(HttpServletRequest request) {
_85
RequestParametersValidator validator = new RequestParametersValidator(request);
_85
_85
return validator.validatePresence("name", "email", "password", "countryCode", "phoneNumber")
_85
&& validator.validateEmail("email");
_85
}
_85
_85
private void preserveStatusRequest(
_85
HttpServletRequest request,
_85
String name,
_85
String email,
_85
String countryCode,
_85
String phoneNumber) {
_85
request.setAttribute("name", name);
_85
request.setAttribute("email", email);
_85
request.setAttribute("countryCode", countryCode);
_85
request.setAttribute("phoneNumber", phoneNumber);
_85
}
_85
}

Now let's check out the Servlet handling a new user registration and see how it sends a token upon account creation.


Sending a Token on Account Creation

sending-a-token-on-account-creation page anchor

Once the user has an authyId we can actually send a verification code to that user's mobile phone using the Java Client for Authy(link takes you to an external page).

When our user is created successfully via the form we implemented, we send a token to the user's mobile phone to verify their account in our servlet.

Handle registration of new accounts

handle-registration-of-new-accounts-1 page anchor
src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

_85
package com.twilio.verification.servlet;
_85
_85
import com.authy.AuthyApiClient;
_85
import com.twilio.verification.lib.RequestParametersValidator;
_85
import com.twilio.verification.lib.SessionManager;
_85
import com.twilio.verification.model.User;
_85
import com.twilio.verification.repository.UsersRepository;
_85
_85
import javax.servlet.ServletException;
_85
import javax.servlet.http.HttpServlet;
_85
import javax.servlet.http.HttpServletRequest;
_85
import javax.servlet.http.HttpServletResponse;
_85
import java.io.IOException;
_85
_85
public class RegistrationServlet extends HttpServlet {
_85
_85
private final UsersRepository usersRepository;
_85
private final SessionManager sessionManager;
_85
private final AuthyApiClient authyClient;
_85
_85
@SuppressWarnings("unused")
_85
public RegistrationServlet() {
_85
this(
_85
new UsersRepository(),
_85
new SessionManager(),
_85
new AuthyApiClient(System.getenv("AUTHY_API_KEY"))
_85
);
_85
}
_85
_85
public RegistrationServlet(UsersRepository usersRepository, SessionManager sessionManager, AuthyApiClient authyClient) {
_85
this.usersRepository = usersRepository;
_85
this.sessionManager = sessionManager;
_85
this.authyClient = authyClient;
_85
}
_85
_85
protected void doPost(HttpServletRequest request, HttpServletResponse response)
_85
throws ServletException, IOException {
_85
_85
String name = request.getParameter("name");
_85
String email = request.getParameter("email");
_85
String password = request.getParameter("password");
_85
String countryCode = request.getParameter("countryCode");
_85
String phoneNumber = request.getParameter("phoneNumber");
_85
_85
if (validateRequest(request)) {
_85
_85
com.authy.api.User authyUser = authyClient.getUsers().createUser(email, phoneNumber, countryCode);
_85
_85
// Create user remotely
_85
if (authyUser.isOk()) {
_85
int authyUserId = authyUser.getId();
_85
// Create user locally
_85
User user = usersRepository.create(new User(name, email, password, countryCode, phoneNumber, authyUserId));
_85
_85
// Request SMS authentication
_85
authyClient.getUsers().requestSms(authyUserId);
_85
_85
sessionManager.partialLogIn(request, user.getId());
_85
response.sendRedirect("/verify-code");
_85
}
_85
} else {
_85
preserveStatusRequest(request, name, email, countryCode, phoneNumber);
_85
request.getRequestDispatcher("/registration.jsp").forward(request, response);
_85
}
_85
}
_85
_85
private boolean validateRequest(HttpServletRequest request) {
_85
RequestParametersValidator validator = new RequestParametersValidator(request);
_85
_85
return validator.validatePresence("name", "email", "password", "countryCode", "phoneNumber")
_85
&& validator.validateEmail("email");
_85
}
_85
_85
private void preserveStatusRequest(
_85
HttpServletRequest request,
_85
String name,
_85
String email,
_85
String countryCode,
_85
String phoneNumber) {
_85
request.setAttribute("name", name);
_85
request.setAttribute("email", email);
_85
request.setAttribute("countryCode", countryCode);
_85
request.setAttribute("phoneNumber", phoneNumber);
_85
}
_85
}

When the code is sent we redirect to another page where the user can enter the token they received, therefore completing the verification process.


This servlet method handles the submission form. It needs to:

  • Get the current user.
  • Verify the code that was entered by the user.
  • If the code entered was valid, flip a boolean flag on the user model to indicate the account was verified.

Verify an Authy code

verify-an-authy-code page anchor

The Authy client(link takes you to an external page) provides us with a verify() method that allows us to pass a user id and a token. In this case we just need to check that the API request was successful and, if so, set the User's verified field to true.

src/main/java/com/twilio/verification/servlet/VerifyCodeServlet.java

_81
package com.twilio.verification.servlet;
_81
_81
import com.authy.AuthyApiClient;
_81
import com.authy.api.Token;
_81
import com.twilio.verification.lib.RequestParametersValidator;
_81
import com.twilio.verification.lib.Sender;
_81
import com.twilio.verification.lib.SessionManager;
_81
import com.twilio.verification.model.User;
_81
import com.twilio.verification.repository.UsersRepository;
_81
_81
import javax.servlet.ServletException;
_81
import javax.servlet.http.HttpServlet;
_81
import javax.servlet.http.HttpServletRequest;
_81
import javax.servlet.http.HttpServletResponse;
_81
import java.io.IOException;
_81
_81
public class VerifyCodeServlet extends HttpServlet {
_81
_81
private final UsersRepository usersRepository;
_81
private final SessionManager sessionManager;
_81
private final AuthyApiClient authyClient;
_81
private final Sender sender;
_81
_81
@SuppressWarnings("unused")
_81
public VerifyCodeServlet() {
_81
this(
_81
new UsersRepository(),
_81
new SessionManager(),
_81
new AuthyApiClient(System.getenv("AUTHY_API_KEY")),
_81
new Sender()
_81
);
_81
}
_81
_81
public VerifyCodeServlet(
_81
UsersRepository usersRepository, SessionManager sessionManager, AuthyApiClient authyClient, Sender sender) {
_81
this.usersRepository = usersRepository;
_81
this.authyClient = authyClient;
_81
this.sessionManager = sessionManager;
_81
this.sender = sender;
_81
}
_81
_81
protected void doGet(HttpServletRequest request, HttpServletResponse response)
_81
throws ServletException, IOException {
_81
_81
request.getRequestDispatcher("/verifyCode.jsp").forward(request, response);
_81
}
_81
_81
protected void doPost(HttpServletRequest request, HttpServletResponse response)
_81
throws ServletException, IOException {
_81
_81
String code = request.getParameter("code");
_81
_81
if (validateRequest(request)) {
_81
int userId = sessionManager.getLoggedUserId(request);
_81
User user = usersRepository.find(userId);
_81
_81
Token token = authyClient.getTokens().verify(user.getAuthyId(), code);
_81
if (token.isOk()) {
_81
user.setVerified(true);
_81
usersRepository.update(user);
_81
// Send SMS confirmation
_81
sender.send(user.getFullPhoneNumber(), "You did it! Sign up complete :3");
_81
_81
sessionManager.logIn(request, userId);
_81
response.sendRedirect("/account");
_81
} else {
_81
request.setAttribute("codeError", "Incorrect code, please try again");
_81
request.getRequestDispatcher("/verifyCode.jsp").forward(request, response);
_81
}
_81
} else {
_81
request.getRequestDispatcher("/verifyCode.jsp").forward(request, response);
_81
}
_81
}
_81
_81
_81
private boolean validateRequest(HttpServletRequest request) {
_81
RequestParametersValidator validator = new RequestParametersValidator(request);
_81
_81
return validator.validatePresence("code");
_81
}
_81
}

That's all for token verification! However, our verification form wouldn't be very usable if there wasn't a way to resend a verification code if the message didn't arrive at the end user's handset.


Since the form for re-sending the code(link takes you to an external page) is very simple, we're going to skip that for this tutorial. Let's just look at the servlet.

This method loads the user associated with the request and then uses the same Authy API method we used earlier to resend the code.

Re-send an Authy verification code

re-send-an-authy-verification-code page anchor
src/main/java/com/twilio/verification/servlet/ResendTokenServlet.java

_47
package com.twilio.verification.servlet;
_47
_47
import com.authy.AuthyApiClient;
_47
import com.twilio.verification.lib.SessionManager;
_47
import com.twilio.verification.model.User;
_47
import com.twilio.verification.repository.UsersRepository;
_47
_47
import javax.servlet.ServletException;
_47
import javax.servlet.http.HttpServlet;
_47
import javax.servlet.http.HttpServletRequest;
_47
import javax.servlet.http.HttpServletResponse;
_47
import java.io.IOException;
_47
_47
public class ResendTokenServlet extends HttpServlet {
_47
_47
private final UsersRepository usersRepository;
_47
private final SessionManager sessionManager;
_47
private final AuthyApiClient authyClient;
_47
_47
@SuppressWarnings("unused")
_47
public ResendTokenServlet() {
_47
this(
_47
new UsersRepository(),
_47
new SessionManager(),
_47
new AuthyApiClient(System.getenv("AUTHY_API_KEY"))
_47
);
_47
}
_47
_47
public ResendTokenServlet(
_47
UsersRepository usersRepository, SessionManager sessionManager, AuthyApiClient authyClient) {
_47
this.usersRepository = usersRepository;
_47
this.sessionManager = sessionManager;
_47
this.authyClient = authyClient;
_47
}
_47
_47
protected void doPost(HttpServletRequest request, HttpServletResponse response)
_47
throws ServletException, IOException {
_47
_47
int userId = sessionManager.getLoggedUserId(request);
_47
User user = usersRepository.find(userId);
_47
_47
// Request SMS authentication
_47
authyClient.getUsers().requestSms(user.getAuthyId());
_47
_47
response.sendRedirect("/verify-token");
_47
}
_47
}

To wrap things up, let's implement the last step where we confirm that the user's account has been verified with a text message.


Sending the Confirmation Message

sending-the-confirmation-message page anchor

In this example, we create a single instance of the Twilio REST API helper, called client.

Then we need to get the account, the messageFactory, and finally use its sendMessage method in order to send an SMS to the user's phone.

Send a confirmation with a TwilioRestClient wrapper

send-a-confirmation-with-a-twiliorestclient-wrapper page anchor
src/main/java/com/twilio/verification/lib/Sender.java

_28
package com.twilio.verification.lib;
_28
_28
import com.twilio.http.TwilioRestClient;
_28
import com.twilio.rest.api.v2010.account.MessageCreator;
_28
import com.twilio.type.PhoneNumber;
_28
import com.twilio.verification.lib.Twilio.Config;
_28
_28
public class Sender {
_28
_28
private final TwilioRestClient client;
_28
_28
public Sender() {
_28
client = new TwilioRestClient.Builder(Config.getAccountSid(), Config.getAuthToken()).build();
_28
}
_28
_28
public Sender(TwilioRestClient client) {
_28
this.client = client;
_28
}
_28
_28
public void send(String to, String message) {
_28
new MessageCreator(
_28
new PhoneNumber(to),
_28
new PhoneNumber(Config.getPhoneNumber()),
_28
message
_28
).create(client);
_28
}
_28
_28
}

Congratulations! You've successfully verified new user accounts with Authy. Where can we take it from here?


If you're a Java developer working with Twilio, you might want to check out these other tutorials:

Click-To-Call

Put a button on your web page that connects visitors to live support or salespeople via telephone.

Automated Survey

Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.

Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Reach out to us on Twitter(link takes you to an external page) and let us know what you build!


Rate this page: