For new development, we encourage you to use the Verify API instead of the Authy API. The Verify API is an evolution of the Authy API with continued support for SMS, voice, and email one-time passcodes, an improved developer experience and new features including:
- Access via the Twilio CLI
- Improved Visibility and Insights
- Push authentication SDK embeddable in your own application
You are currently viewing the Authy API. The Authy API will continue to be maintained, but any new features and development will be on the Verify API. Check out the FAQ for more information and Verify API Reference to get started.
Ready to implement user account verification in your application? Here's how it works at a high level:
- The users begin the registration process by entering their data, including a phone number, into a signup form.
- The authentication system sends a one-time password to the user's mobile phone to verify the possession of that phone number.
- The user enters the one-time password into a form before completing registration.
- The user opens a success page and receives an SMS indicating that their account has been created!
To get this done, you'll be working with the following Twilio-powered APIs:
Authy REST API
- Authy Docs: Find quick starts, documentation, and all about the helper libraries.
Twilio REST API
- Messages Resource: We will use Twilio directly to send our user a confirmation message after they create an account.
All of this can be done in under a half an hour with the simplicity and power of Authy and Twilio. Let's get started!
Pretty simple user model, right? Next, we're going to visit the registration form on the client side.
When we create a new user, we ask for a name, e-mail address, and a password. To validate their account, we also ask them for a mobile number with a country code. We use Authy to send a one-time password via SMS to this phone number.
It is now the controller'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.
Now the user is logged in but not verified. In the next step, we'll learn how to configure Authy to integrate with our application.
.env.example 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). Using environment variables prevents us from accidentally checking them into source control. We also use our Laravel configuration file to load the key and inject
Authy\AuthyApi into the application using a service provider.
Now we need our Authy production key (sign up for Authy here). When you create an Authy application, the production key is found on the dashboard.
Now that we've learned how to configure Authy, we need to jump over to the
UserController to configure the Authy client and create an instance method to send a one-time password.
Once the user has an
authy_id we can actually send a verification code to that user's mobile phone.
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 controller. Once the code is sent, we redirect to another page where the user can enter the token they received, completing the verification process.
Next, we'll take a look at verifying the code the user provides us.
This controller function 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.
The Authy PHP client provides us with a
verifyToken() 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
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.
This controller loads the
$user associated with the request and then uses the same Authy API method we used earlier to resend the code.
To wrap things up, let's implement the last step. We need to confirm that the user's account has been verified with a success page and a text message.
blade template displays a user name and let's them know they've been verified.
This should suffice for confirmation in the browser that the user has been verified. Let's see how we might send that text message next.
We create a single instance of the Twilio REST API helper, called
$client in this example.
Then all we need to do to send an SMS to the user's phone is use
messages->create() method. Notice that we are using the user's
fullNumber() to make sure we support international numbers. The
fullNumber() method in the
User model simply returns a combination of the
phone_number that the user provided upon registration.
We've just implemented account verification so your users can confirm their phone number. Where can we take it from here?
If you're a PHP developer working with Twilio, you might want to check out these other tutorials:
Put a button on your web page that connects visitors to live support or sales people via telephone.
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 and let us know what you've built!