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 to your application to validate new accounts 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 Python, Django 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?
Either sign up for a free Twilio trial, or sign into an existing Twilio account.
Once logged in, visit the Verify Console. Click on the red 'Create New Application' (or big red plus ('+') if you already created one) to create a new Verify 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 Django repository. Enter the directory and use pip
to install all of our dependencies:
pip install -r requirements.txt
.env.example
ACCOUNT_SECURITY_API_KEY
to the API Key from the above step.env
Enter the API Key from the Verify console and optionally change the port.
1# You can get/create one here :2# https://www.twilio.com/console/authy/applications3ACCOUNT_SECURITY_API_KEY='ENTER_SECRET_HERE'
Run the app migrations to get everything up to date:
./manage.py migrate
That's all the setup you'll need.
Now, launch the application with:
./manage.py runserver
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:8000/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'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 (enter a number on the phone keypad).
1from authy.api import AuthyApiClient2from django.conf import settings3from django.shortcuts import render, redirect45from .forms import VerificationForm, TokenForm678authy_api = AuthyApiClient(settings.ACCOUNT_SECURITY_API_KEY)91011def phone_verification(request):12if request.method == 'POST':13form = VerificationForm(request.POST)14if form.is_valid():15request.session['phone_number'] = form.cleaned_data['phone_number']16request.session['country_code'] = form.cleaned_data['country_code']17authy_api.phones.verification_start(18form.cleaned_data['phone_number'],19form.cleaned_data['country_code'],20via=form.cleaned_data['via']21)22return redirect('token_validation')23else:24form = VerificationForm()25return render(request, 'phone_verification.html', {'form': form})262728def token_validation(request):29if request.method == 'POST':30form = TokenForm(request.POST)31if form.is_valid():32verification = authy_api.phones.verification_check(33request.session['phone_number'],34request.session['country_code'],35form.cleaned_data['token']36)37if verification.ok():38request.session['is_verified'] = True39return redirect('verified')40else:41for error_msg in verification.errors().values():42form.add_error(None, error_msg)43else:44form = TokenForm()45return render(request, 'token_validation.html', {'form': form})464748def verified(request):49if not request.session.get('is_verified'):50return redirect('phone_verification')51return render(request, 'verified.html')
Either way you requested the passcode, enter the token into the Verification entry form and click 'Verify Phone':
1from authy.api import AuthyApiClient2from django.conf import settings3from django.shortcuts import render, redirect45from .forms import VerificationForm, TokenForm678authy_api = AuthyApiClient(settings.ACCOUNT_SECURITY_API_KEY)91011def phone_verification(request):12if request.method == 'POST':13form = VerificationForm(request.POST)14if form.is_valid():15request.session['phone_number'] = form.cleaned_data['phone_number']16request.session['country_code'] = form.cleaned_data['country_code']17authy_api.phones.verification_start(18form.cleaned_data['phone_number'],19form.cleaned_data['country_code'],20via=form.cleaned_data['via']21)22return redirect('token_validation')23else:24form = VerificationForm()25return render(request, 'phone_verification.html', {'form': form})262728def token_validation(request):29if request.method == 'POST':30form = TokenForm(request.POST)31if form.is_valid():32verification = authy_api.phones.verification_check(33request.session['phone_number'],34request.session['country_code'],35form.cleaned_data['token']36)37if verification.ok():38request.session['is_verified'] = True39return redirect('verified')40else:41for error_msg in verification.errors().values():42form.add_error(None, error_msg)43else:44form = TokenForm()45return render(request, 'token_validation.html', {'form': form})464748def verified(request):49if not request.session.get('is_verified'):50return redirect('phone_verification')51return render(request, 'verified.html')
And with that, your demo app is protected with Twilio's Phone Verification! You can now log out to try the untried channel.
Your demo app is now keeping fraudulent users from registering with your business and polluting your database. Next, check out all of the variables and options available to you in the Phone Verification API Reference. Also, to protect your customers in an ongoing manner (with this same codebase) try the Python Django Authy Two-Factor Authentication Quickstart.
After that, visit the Docs for more Account Security demos and tutorials and web applications using all of Twilio's products.