Get Started with the SMS Verify SDK

This tutorial teaches you how to use the SMS Verify SDK, by walking you step-by-step through the creation of an integration where you send your own verification code to an end-user. Skip to the end of this page to see the full, completed sample code.

NOTE:

This tutorial includes CLI examples using a bash shell in Mac Terminal, please modify accordingly if you are using a different system.

Before You Begin

Make sure you have the following before you start:

  • Authentication Credentials: Your Customer ID and API Key. If you need help finding these items, go to the support article How do I find my Customer ID and API Key.
  • Testing Device: A mobile phone on which you can receive SMS.
NOTE:

This tutorial uses Python 3.8.1. Please modify accordingly if you are using a different version of Python.

Step 1: Download the SDK

  1. Create a directory on your machine where you want to download the TeleSign Python SDK. This should not be in the same directory as where you build your integration.
Bash
cd ~/code/local
mkdir telesign_sdks
cd telesign_sdks
  1. In the new directory you created, clone the SDK from GitHub. A new directory should appear called python_telesign_enterprise:
Bash
git clone https://github.com/TeleSign/python_telesign_enterprise.git

Step 2: Create Code to Send the SMS

  1. Create another directory (must be outside of the SDK directory you just created) where you want your SMS Verify integration to go.
Bash
cd ~/code/local/telesign_integrations
mkdir sms_verify_integration
cd sms_verify_integration
  1. Install the SDK in this new directory using the CLI command below. It references the path to where you downloaded the SDK. Once the SDK is installed, you should see Successfully installed telesignenterprise in the terminal:
Bash
pip install -e ~/code/local/telesign_sdks/python_telesign_enterprise/
  1. Create a new file in your integration folder called verify_with_own_code.py and open it in your editor:
Bash
touch verify_with_own_code.py
  1. Add code to import dependencies from the TeleSign SDK:
Python
from telesignenterprise.verify import VerifyClient
from telesign.util import random_with_n_digits
  1. Define variables to store your authentication credentials:
Python
customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"
api_key = "TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="
  1. Define a variable to hold the end-user’s phone number you want to send verification to. For this tutorial, hardcode the phone number to one for a device you can use for testing:
Python
phone_number = "15558675309"
NOTE:

In your production integration, have phone_number pull from your recipient database instead of hardcoding it.

  1. Randomly generate your verification code. We will use a TeleSign SDK utility for this. The parameter value 5 specifies the number of digits generated:
Python
verify_code = random_with_n_digits(5)
  1. Instantiate a verification object with your authentication credentials.
Python
verify = VerifyClient(customer_id, api_key)
  1. Send a verification SMS to the end-user and store the response. Behind the scenes, this sends an HTTP request to the TeleSign SMS Verify API. TeleSign then sends an SMS with the code to the end-user:
Python
response = verify.sms(phone_number, verify_code=verify_code)
  1. Collect the potential verification code entered by the end-user in your application. In this tutorial, we will simulate this by prompting for input from the command line.
Python
user_entered_verify_code = input("Please enter the verification code you were sent: ")
NOTE:

In your production implementation, you will listen for input from your website or other application where the end-user is trying to log in.

  1. Determine if the user-entered code matches your verification code, and resolve the login attempt accordingly. In this tutorial, we just report whether the codes match.
Python
if verify_code == user_entered_verify_code.strip():
    print("Your code is correct.")
else:
    print("Your code is incorrect.")
NOTE:

In your production implementation, you will add code here to log the user in when the codes match.

Step 3: Test Your Integration

  1. Switch from your editor to the terminal and run verify_with_own_code.py.
Bash
python verify_with_own_code.py
  1. You should receive an SMS on your phone that looks like this:
  1. In the terminal, you should see a prompt to enter a code. Enter the code from your phone to test a successful verification:
Bash
Please enter the verification code you were sent: 82139
Your code is correct.
  1. Now let’s test an unsuccessful verification. Run verify_with_own_code.py again:
Bash
python verify_with_own_code.py
  1. You should receive a new and different SMS code on your phone. Enter something else though in the command prompt, and you should get a message that verification failed.
Bash
Please enter the verification code you were sent: 55555
Your code is incorrect.

Here is the complete sample code for this tutorial:

verify_with_own_code.py
Python
from telesignenterprise.verify import VerifyClient
from telesign.util import random_with_n_digits

customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"
api_key = "TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="

phone_number = "15558675309"
verify_code = random_with_n_digits(5)

verify = VerifyClient(customer_id, api_key)
response = verify.sms(phone_number, verify_code=verify_code)

user_entered_verify_code = input("Please enter the verification code you were sent: ")

if verify_code == user_entered_verify_code.strip():
    print("Your code is correct.")
else:
    print("Your code is incorrect.")