Phone ID - Tutorial: Check phone type to block VoIP with Telesign Python SDK

📘

NOTE:

This tutorial applies only to self-service accounts, not full-service accounts.

This tutorial walks you step-by-step through how to write code that makes a request to Telesign Phone ID using a Python SDK to identify voice over internet protocol (VoIP) numbers, so you can block them. Skip to the end of this page to see the full sample code.

📘

Why block VoIP numbers?

VOIP numbers are cheap, easy to buy in bulk, and can easily be used by a fraudster to sign up for an account that requires a phone number as part of registration. After signup, they may abuse promotions and bonuses, spam other users, create fake likes, comments, or product reviews, sell fake/bulk accounts, and engage in other similar fraudulent activities. Blocking VOIP numbers can be a key part of securing your account registration process.

Before you begin

Make sure you have the following ready:

  • Authentication credentials - Your Telesign Customer ID and API Key. If you don't have these already, see How do I find my Customer ID and API Key?
  • Phone number - The phone number you want to get information about. If you are a self-service customer and have not added money to your account yet, you need to first get the phone number verified by Telesign by adding it to your list of Test Numbers.

📘

NOTE:

This tutorial uses the following:

  • MacOS
  • Python 3.9.6

Please modify accordingly if your developer environment differs from these details.

Set up your project

  1. Follow the Telesign Self-service Python SDK install instructions here on GitHub incorporating these details:

    • Use phoneidTest as the project name.

    You should end up in the top-level directory for your project in the Terminal.

  2. Create a new file called phone_id_check_type.py and open it in your editor.

touch phone_id_check_type.py

Check phone type

  1. Follow the steps below or copy the sample code into the file and update it accordingly.
  2. Import these dependencies from the SDK.
from __future__ import print_function
from telesign.phoneid import PhoneIdClient
import os

#If you are working with a version of Python 3, you do not need the first import
#statement (`from __future__ import print_function`).
  1. Define variables to store your authentication credentials.
customer_id = os.getenv('CUSTOMER_ID', 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890')
api_key = os.getenv('API_KEY', 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==')  
  1. Define a variable to hold the phone number you want to get information for and a variable to store the phone type, represented by a numerical identifier for a VoIP phone. For the phone number, use the complete number, including the country code. This should be a string with no spaces or special characters. For example: 16505551212.
phone_number = os.getenv('PHONE_NUMBER', '11234567890')
phone_type_voip = "5"

📘

NOTE:

In your production integration, pull the phone number from your recipient database instead of hardcoding it.

  1. Instantiate a PhoneIdClient object and pass it your customer ID and your API key.
data = PhoneIdClient(customer_id, api_key)
  1. Use Phone ID to check your phone number and store the results.
response = data.phoneid(incorrect_phone_number)
  1. Check phone_type. If it is "VOIP", print a message saying that the number is a VoIP phone. Otherwise, print a message saying it is not a VoIP phone.
if response.ok:
    if response.json['phone_type']['code'] == phone_type_voip:
        print("Phone number {} is a VoIP phone.".format(
            phone_number))
    else:
        print("Phone number {} is not a VoIP phone.".format(
            phone_number))

📘

NOTE:

In a production integration, you would instead implement logic here to block the VoIP number.

  1. If everything works, it should submit the phone number you specified to a check by Phone ID. Depending on whether the phone type is VoIP or not, the appropriate message is displayed.

Test your integration

Switch from your editor to the terminal and run:

python phone_id_check_type.py

Sample code

from __future__ import print_function
from telesign.phoneid import PhoneIdClient
import os

customer_id = os.getenv('CUSTOMER_ID', 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890')
api_key = os.getenv('API_KEY', 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==')  

phone_number = os.getenv('PHONE_NUMBER', '11234567890')
phone_type_voip = "5"

data = PhoneIdClient(customer_id, api_key)
response = data.phoneid(phone_number)

if response.ok:
    if response.json['phone_type']['code'] == phone_type_voip:
        print("Phone number {} is a VoIP phone.".format(
            phone_number))
    else:
        print("Phone number {} is not a VoIP phone.".format(
            phone_number))