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 your registration flow to verify that a user has the device they claim to have. Adding 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 PHP, Laravel and AngularJS app that requires a phone verification step with Twilio Verify to create an account. Two channels of Phone Verification are demoed: SMS and Voice.
Ready to add Phone Verification 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 to reveal your Production API Key and copy it somewhere safe. You will use the API Key during the application setup step below.
To complete the quickstart today we'll use PHP 7.0+, Composer, MySQL, and the Twilio PHP Helper Library. Let's walk through each one now - but feel free to skip if you have already installed one.
When doing web development in PHP, we strongly suggest using Composer for package management. This quickstart relies on Composer to install the PHP Helper library. You can find manual Twilio PHP installation instructions on the PHP Helper Library page.
While Twilio's Verify API doesn't return user information you'll need to store, to continue working on the app after this Quickstart you'll want a database. For this demo, we built our user database on top of MySQL 5.x.
If you haven't yet installed it, here are instructions for your platform:
When installed, start MySQL. If you're using the default MySQL credentials (as below), create a schema account_security
with user homestead
and password secret
.
Clone our PHP repository locally, then enter the directory:
1git clone git@github.com:TwilioDevEd/account-security-quickstart-php.git2cd account-security-quickstart-php3composer install
cp .env.example .env
Next, copy your Authy API Key from the Authy Dashboard and set the API_KEY
variable in your .env
file.
Enter the API Key from the Account Security console and optionally change the port.
1# Get your API key here: https://www.twilio.com/console/authy/getting-started2API_KEY=your-authy-api-key34APP_DEBUG=true5APP_ENV=development6APP_KEY=7APP_LOG=errorlog89# By default, if you'd like to match this install MySQL locally.10# host: localhost:330611# DB user: homestead12# DB pass: secret13# DB name: account_security1415DB_DATABASE=account_security16DB_HOST=localhost17DB_PASSWORD=secret18DB_PORT=330619DB_USERNAME=homestead20MYSQL_DATABASE=account_security21MYSQL_PASSWORD=secret22MYSQL_PORT=330623MYSQL_ROOT_PASSWORD=secret24MYSQL_USER=homestead25SECRET=your-account-secret
Now, launch the application with:
1php artisan key:generate2php artisan migrate3php artisan serve --port 8081
Assuming your API Key is correctly entered and the command above executed correctly, you'll soon get a message that the app is up!
Keeping your phone at your side, vist the Phone Verification page of the demo at http://localhost:8081/verify/
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).
1<?php23namespace App\Http\Controllers\Auth;45use \Exception;6use App\Http\Controllers\Controller;7use Authy\AuthyApi;8use Illuminate\Http\Request;9use Illuminate\Support\Facades\Validator;1011class PhoneVerificationController extends Controller12{13/*14|--------------------------------------------------------------------------15| Phone Verification Controller16|--------------------------------------------------------------------------17|18| Uses Authy to verify a users phone via voice or sms.19|20*/2122/**23* Create a new controller instance.24*25* @return void26*/27public function __construct()28{29$this->middleware('guest');30}3132/**33* Get a validator for an incoming verification request.34*35* @param array $data36* @return \Illuminate\Contracts\Validation\Validator37*/38protected function verificationRequestValidator(array $data)39{40return Validator::make($data, [41'country_code' => 'required|string|max:3',42'phone_number' => 'required|string|max:10',43'via' => 'required|string|max:4',44]);45}4647/**48* Get a validator for an code verification request.49*50* @param array $data51* @return \Illuminate\Contracts\Validation\Validator52*/53protected function verificationCodeValidator(array $data)54{55return Validator::make($data, [56'country_code' => 'required|string|max:3',57'phone_number' => 'required|string|max:10',58'token' => 'required|string|max:4'59]);60}6162/**63* Request phone verification via PhoneVerificationService.64*65* @param array $data66* @return Illuminate\Support\Facades\Response;67*/68protected function startVerification(69Request $request,70AuthyApi $authyApi71) {72$data = $request->all();73$validator = $this->verificationRequestValidator($data);74extract($data);7576if ($validator->passes()) {77return $authyApi->phoneVerificationStart($phone_number, $country_code, $via);78}7980return response()->json(['errors'=>$validator->errors()], 403);81}8283/**84* Request phone verification via PhoneVerificationService.85*86* @param array $data87* @return Illuminate\Support\Facades\Response;88*/89protected function verifyCode(90Request $request,91AuthyApi $authyApi92) {93$data = $request->all();94$validator = $this->verificationCodeValidator($data);95extract($data);9697if ($validator->passes()) {98try {99$result = $authyApi->phoneVerificationCheck($phone_number, $country_code, $token);100return response()->json($result, 200);101} catch (Exception $e) {102$response=[];103$response['exception'] = get_class($e);104$response['message'] = $e->getMessage();105$response['trace'] = $e->getTrace();106return response()->json($response, 403);107}108}109110return response()->json(['errors'=>$validator->errors()], 403);111}112}
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.
1<?php23namespace App\Http\Controllers\Auth;45use \Exception;6use App\Http\Controllers\Controller;7use Authy\AuthyApi;8use Illuminate\Http\Request;9use Illuminate\Support\Facades\Validator;1011class PhoneVerificationController extends Controller12{13/*14|--------------------------------------------------------------------------15| Phone Verification Controller16|--------------------------------------------------------------------------17|18| Uses Authy to verify a users phone via voice or sms.19|20*/2122/**23* Create a new controller instance.24*25* @return void26*/27public function __construct()28{29$this->middleware('guest');30}3132/**33* Get a validator for an incoming verification request.34*35* @param array $data36* @return \Illuminate\Contracts\Validation\Validator37*/38protected function verificationRequestValidator(array $data)39{40return Validator::make($data, [41'country_code' => 'required|string|max:3',42'phone_number' => 'required|string|max:10',43'via' => 'required|string|max:4',44]);45}4647/**48* Get a validator for an code verification request.49*50* @param array $data51* @return \Illuminate\Contracts\Validation\Validator52*/53protected function verificationCodeValidator(array $data)54{55return Validator::make($data, [56'country_code' => 'required|string|max:3',57'phone_number' => 'required|string|max:10',58'token' => 'required|string|max:4'59]);60}6162/**63* Request phone verification via PhoneVerificationService.64*65* @param array $data66* @return Illuminate\Support\Facades\Response;67*/68protected function startVerification(69Request $request,70AuthyApi $authyApi71) {72$data = $request->all();73$validator = $this->verificationRequestValidator($data);74extract($data);7576if ($validator->passes()) {77return $authyApi->phoneVerificationStart($phone_number, $country_code, $via);78}7980return response()->json(['errors'=>$validator->errors()], 403);81}8283/**84* Request phone verification via PhoneVerificationService.85*86* @param array $data87* @return Illuminate\Support\Facades\Response;88*/89protected function verifyCode(90Request $request,91AuthyApi $authyApi92) {93$data = $request->all();94$validator = $this->verificationCodeValidator($data);95extract($data);9697if ($validator->passes()) {98try {99$result = $authyApi->phoneVerificationCheck($phone_number, $country_code, $token);100return response()->json($result, 200);101} catch (Exception $e) {102$response=[];103$response['exception'] = get_class($e);104$response['message'] = $e->getMessage();105$response['trace'] = $e->getTrace();106return response()->json($response, 403);107}108}109110return response()->json(['errors'=>$validator->errors()], 403);111}112}
And with that, your demo app is protected with Twilio's Verify phone verification! 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, you should 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 PHP 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. Encore!