Phone ID - Tutorial: Perform a number deactivation check with Telesign Python SDK

📘

NOTE:

To add Number Deactivation to your account, contact a Telesign expert. This feature is available for full-service accounts only.

This tutorial explains how to check whether a phone number was deactivated using the Phone ID Number Deactivation identity attribute with the Telesign Python SDK. You can use the check to decide whether to send someone an SMS or Voice message. If someone's number is not active, you can save the cost of attempting to send a message. Skip to the end of this page to see the full sample code.

Before you begin

Make sure you have the following ready:

📘

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 Full-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_deactivation_check.py and open it in your editor.

    touch phone_id_deactivation_check.py
    

Make Number Deactivation request

  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 telesignenterprise.phoneid import PhoneIdClient
    import os
    
  3. Define variables to store your authentication credentials.

    customer_id = os.getenv('CUSTOMER_ID', 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890')
    api_key = os.getenv('API_KEY', 'EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==')
    
  4. Define a variable to hold the phone number you want to perform a number deactivation check on. This should be a string with no spaces or special characters. Use the complete number, including the country code. For example: 16505551212.

    phone_number = os.getenv('PHONE_NUMBER', '11234567890')
    

    📘

    NOTE:

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

  5. Define a variable to hold the ucid which is the use case code that provides information about what kind of transaction you are performing.

    ucid = "ATCK"
    
  6. Instantiate a PhoneIdClient object containing your Customer ID and API Key.

    phoneid = PhoneIdClient(customer_id, api_key)
    
  7. Perform a number deactivation check, and store the results in the response variable.

    response = phoneid.number_deactivation(phone_number, ucid)
    
  8. The results stored in the response variable are provided to you in this step as shown in the sample code.

    if response.ok:
        if response.json['number_deactivation']['last_deactivated']:
            print("Phone number {} was last deactivated {}.".format(
                response.json['number_deactivation']['number'],
                response.json['number_deactivation']['last_deactivated']))
        else:
            print("Phone number {} has not been deactivated.".format(response.json['number_deactivation']['number']))
    

    📘

    NOTE:

    Use printto display the response for debugging purposes. In your production code, you would likely remove this from your code.

Test your integration

  1. Switch from your editor to the terminal and build your project. Make sure you are in the top-level directory of your project.

    python phone_id_deactivation_check.py
    

You should receive a response containing details about whether your phone number is active or not. You can use these results to make decisions about what to do with the phone number. For example, if someone is trying to sign up for an account with an inactive number, you might require them to sign up using a different number. Or, if you needed to send the provided number a code via text or voice message, you might opt not to send the message since it will not reach a deactivated number.

Sample code

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

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

phone_number = os.getenv('PHONE_NUMBER', '11234567890')
ucid = "ATCK"

phoneid = PhoneIdClient(customer_id, api_key)
response = phoneid.number_deactivation(phone_number, ucid)

if response.ok:
    if response.json['number_deactivation']['last_deactivated']:
        print("Phone number {} was last deactivated {}.".format(
            response.json['number_deactivation']['number'],
            response.json['number_deactivation']['last_deactivated']))
    else:
        print("Phone number {} has not been deactivated.".format(response.json['number_deactivation']['number']))