Phone ID - Tutorial: Cleanse a number 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 cleanse a phone number. Phone number cleansing corrects common formatting issues in submitted phone numbers. Skip to the end of this page to see the full sample code.
This code will take your test phone number, add an extra digit to make it invalid, and send it to Phone ID to cleanse. It then takes the cleansed phone number from the response and prints it to the console.
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 cleanse.
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
-
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.
- Use
-
Create a new file called
phone_id_cleanse_number.py
and open it in your editor.touch phone_id_cleanse_number.py
Cleanse a number with Phone ID
-
Follow the steps below or copy the sample code into the file and update it accordingly.
-
Import these dependencies from the SDK.
from __future__ import print_function from telesignenterprise.phoneid import PhoneIdClient import os
-
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==')
-
Define a variable to hold the phone number you want to cleanse. 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')
NOTE:
In your production integration, pull the phone number from your recipient database instead of hardcoding it.
-
This code adds an extra digit to the number to make it invalid.
extra_digit = "0"
incorrect_phone_number = "{}{}".format(phone_number, extra_digit)
- Instantiate a PhoneIdClient object and pass it your customer ID and your API key.
data = PhoneIdClient(customer_id, api_key)
- Use Phone ID to check and cleanse the incorrect phone number you created earlier.
response = data.phoneid(incorrect_phone_number)
- Use two print statements to check the results of the cleansing procedure on the incorrect phone number. The first print statement explains what the cleansed phone number is and displays the JSON response for the cleansed number. The second print statement shows the original phone number and displays the JSON response for the original phone number.
if response.ok:
print("Cleansed phone number has country code {} and phone number is {}.".format(
response.json['numbering']['cleansing']['call']['country_code'],
response.json['numbering']['cleansing']['call']['phone_number']))
print("Original phone number was {}.".format(
response.json['numbering']['original']['complete_phone_number']))
- If everything works, you send an incorrect phone number to Phone ID for cleansing, and you get back a response containing your cleansed phone number and the original phone number.
Test your integration
- Switch from your editor to the terminal and run:
python phone_id_check_cleanse_number.py
Sample code
from __future__ import print_function
from telesign.phoneid import PhoneIdClient
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')
extra_digit = "0"
incorrect_phone_number = "{}{}".format(phone_number, extra_digit)
data = PhoneIdClient(customer_id, api_key)
response = data.phoneid(incorrect_phone_number)
if response.ok:
print("Cleansed phone number has country code {} and phone number is {}.".format(
response.json['numbering']['cleansing']['call']['country_code'],
response.json['numbering']['cleansing']['call']['phone_number']))
print("Original phone number was {}.".format(
response.json['numbering']['original']['complete_phone_number']))
Updated about 22 hours ago