Rate this page:

Account Verification with Authy, PHP and Laravel

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:

  • Twilio helper libraries in JavaScript, Java, C#, Python, Ruby, PHP, and Golang
  • Access via the Twilio CLI
  • Improved Visibility and Insights
  • Push authentication SDK embeddable in your own application

You are currently viewing the Authy API. New features and development will be on the Verify API. Check out the FAQ for more information and the migrating to Verify guide to get started.

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

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


  • Authy Docs: Find quick starts, documentation, and all about the helper libraries.


  • 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!

Click here to move on to the next step!

The User Model

The User Model for this use-case is pretty straight-forward, and Laravel offers us some tools to make it even simpler. If you have already read through the 2FA tutorial, this one probably looks very similar.


        Migration to create user table


        Pretty simple user model, right? Next, we're going to visit the registration form on the client side.

        Take a look at the user registration form

        Registration Form

        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.


              Create New Users


              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.

              Configure your application to work with Authy

              Configuring Authy

              In .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.

              Authy dashboard


                    Register the service provider for the Authy API


                    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.

                    Learn how to send a one-time token to a new user

                    Sending a Token on Account Creation

                    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.


                          Send verification token via SMS on user creation


                          Next, we'll take a look at verifying the code the user provides us.

                          Verify your user's code

                          Verifying the Code

                          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 $user->verified to true.


                                Verify a user-submitted token


                                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.

                                See how to resend verification codes

                                Re-Rending the Code

                                Since the form for re-sending the code is one line, we're going to skip that for this tutorial. Let's just look at the controller function.


                                      Re-send a verification code when initial SMS fails


                                      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.

                                      Let the user know that they have been verified

                                      Show User Details Page

                                      This simple blade template displays a user name and let's them know they've been verified.


                                            Render user information with a blade template


                                            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.

                                            Send a confirmation SMS!

                                            Sending the Confirmation Message

                                            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 country_code and phone_number that the user provided upon registration.


                                                  Send a confirmation message via SMS


                                                  We've just implemented account verification so your users can confirm their phone number. Where can we take it from here?

                                                  What's next?

                                                  Where to Next?

                                                  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.

                                                  Automated Survey

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

                                                  Did this help?

                                                  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!

                                                  Mario Celi Kelley Robinson Hector Ortega Kat King Samuel Mendes Paul Kamp
                                                  Rate this page:

                                                  Need some help?

                                                  We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.


                                                        Thank you for your feedback!

                                                        We are always striving to improve our documentation quality, and your feedback is valuable to us. Please select the reason(s) for your feedback or provide additional information about how we can improve:

                                                        Sending your feedback...
                                                        🎉 Thank you for your feedback!
                                                        Something went wrong. Please try again.

                                                        Thanks for your feedback!

                                                        Refer us and get $10 in 3 simple steps!

                                                        Step 1

                                                        Get link

                                                        Get a free personal referral link here

                                                        Step 2

                                                        Give $10

                                                        Your user signs up and upgrade using link

                                                        Step 3

                                                        Get $10

                                                        1,250 free SMSes
                                                        OR 1,000 free voice mins
                                                        OR 12,000 chats
                                                        OR more