It’s easier than you might think to add two-factor authentication (
Sometimes the best way to learn how to code something is by looking at an example, so we’ve created a
To use it, you need a Plivo account; you can
If this is your first time using Plivo APIs, Plivo’s
The sample application uses Redis, an in-memory key–value database, cache, and message broker.
If it’s not already running (or redis-server
in a terminal window to start it.
Once you have the prerequisites out of the way, use git to clone the repository for the sample application into its own directory, or you can just
Install the sample application’s dependencies using the included requirements.txt
file by running pip install -r requirements.txt
in the application directory.
Edit config.py
and change a few items.
Replace the auth placeholders with your authentication credentials from the Plivo console.
Replace the phone number placeholder with an actual phone number in
Replace the PHLO ID with an empty string, since we’re doing our 2FA solely using code.
The application runs under the Flask framework. The file twofactor.py defines a function that uses the Time-Based OTP algorithm to generate a random six-digit one-time password (OTP), and other functions that send the OTP to the specified destination number.
The file routes.py uses the functions to generate an OTP, send it out, and verify the user’s entry.
When you’re ready to see it in action,
Our simple APIs work in tandem with our comprehensive global network. Plivo’s premium direct routes guarantee highest possible delivery rates and the shortest possible delivery times for your 2FA SMS and voice messages. See for yourself — sign up for a free trial account.