Hands-On Guide to Twilio Verify

Hands-On Guide to Twilio Verify

Hello, this is Charu from Classmethod.

In today's digital age, securing user accounts and sensitive information is more important than ever. One effective way to enhance security is through multi-factor authentication (MFA). Twilio Verify is a powerful service that simplifies the process of adding SMS and voice verification to your applications. Not only these, it also offers many other alternate verification options. In this hands-on guide, I'll walk you through the steps to integrate Twilio Verify into your application.

In this blog, we will see how to setup SMS Verify using Python language.

Twilio Verify offers several key advantages:

  • Speed and reliability: Delivers verification codes quickly and reliably.
  • Scalability: Capable of sending thousands of SMS 2FA messages to customers without sacrificing speed.
  • Security: Ensures that mobile SMS verification messages are secure, preventing attackers from intercepting unprotected messages. Twilio Verify is SOC 2 compliant, the gold standard for data security.
  • Excellent support: Provides immediate assistance when something goes wrong.
  • Alternate verification options: Supports other 2FA options such as email, WhatsApp, voice, push, or TOTP, for users who might not want to use their mobile phone for verification.

Step 1: Setting Up Your Twilio Account

Sign Up and Get Your API Key:

After signing up for Twilio, navigate to the Twilio Console. Here, you'll find your Account SID and Auth Token. Keep these handy as you'll need them later.

Screenshot 2024-10-23 at 17.53.48

Create a Verify Service:

In the Twilio Console, go to the Verify section and create a new Verify service. This service will handle the verification process for your application.

Screenshot 2024-10-23 at 17.54.20

Step 2: Twilio Verify Settings

You can change the message settings like adding a 'Do Not Share OTP' message or change your OTP code length.

Screenshot 2024-10-23 at 18.11.32

Screenshot 2024-10-23 at 18.11.46

Step 3: Setting Up Your Python Project

Create Project Directory:

Open your terminal and create a new directory for your project.

Install twilio,

pip install twilio

Write the following code in your main.py:

import os
from twilio.rest import Client

# Find your Account SID and Auth Token at twilio.com/console
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)

verification = client.verify.v2.services(
"VAxxxxxxxxxxxxxxxxxxx").verifications.create(
to="TO_PHONE_NUMBER", channel="sms")

print(verification.sid)

To access your secret variables, run the following command in your terminal,

% echo "export TWILIO_ACCOUNT_SID='YOUR_TWILIO_ACCOUNT_SID'" > twilio.env
% echo "export TWILIO_AUTH_TOKEN='YOUR_TWILIO_AUTH_TOKEN'" >> twilio.env
% source ./twilio.env
% echo "twilio.env" >> .gitignore

Make another file to check your OTP code. I named it check_verify.py

import os
from twilio.rest import Client

account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)

phone_number = "TO_PHONE_NUMBER"
verification_code = input("Enter the verification code you received: ")

verification_check = client.verify.v2.services(
"VAxxxxxxxxxxxxxxxxxxx"
).verification_checks.create(
to=phone_number, code=verification_code)

if verification_check.status == "approved":
    print("Phone number verified successfully!")
else:
    print("Invalid verification code.")

Run your application,

python main.py

Fetch the OTP code and run another file,

python check_verify.py

You can check your phone number for the message. The output will be like this,

Enter the verification code you received: 487562
Phone number verified successfully!

Conclusion:

Congratulations! You've successfully integrated Twilio Verify using Python. With just a few steps, you can now send and verify SMS codes, enhancing the security of your application. To know more about verification best practices, checkout this link.

Happy Learning!

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.