Phone ID - Tutorial: Cleanse a number with Telesign C# 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 C# 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.
- dotnet CLI - This tool isn't required to use this SDK, but it is required for this tutorial.
NOTE:
This tutorial uses the following.
- MacOS
- .NET SDK v6.0.428
Please modify accordingly if your developer environment differs from these details.
Set up your project
-
Follow the Telesign Self-service C# SDK install instructions here on GitHub incorporating these details:
-
Use
phoneidTest
as the project directory name. -
Use
PhoneId
as the name of the solution.
You should end up in the top-level directory ("PhoneId") for your solution in the Terminal.
-
Cleanse a number with Phone ID
- Open the file "Program.cs".
- Follow the steps below or copy the sample code into the file and update it accordingly.
- Add the imports below. The
Telesign
import refers to the Telesign self-service SDK.
using System;
using Telesign;
- Add the following basic structure below the imports.
namespace PhoneId
{
class PhoneId
{
public static void Main(string[] args)
{
}
}
}
In the next step, you will begin adding statements to the Main
function in the PhoneId
class.
- Define variables in the
Main
function to store your authentication credentials. For testing purposes, you can just overwrite the default values below or use environment variables (see step 7).
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
- Define a variable to hold the phone number you want to cleanse. For this tutorial, hardcode your testing device's phone number or pull it from an environment variable (see step 7).
string phoneNumber = "11234567890";
NOTE:
In your production integration, pull the phone number from your recipient database instead of hardcoding it.
- (Optional) Pull authentication credentials from environment variables. Although you might not choose to do so for this tutorial, in your production integration it's a good practice to reference your authentication credentials using environment variables.
if (System.Environment.GetEnvironmentVariable("CUSTOMER_ID") != null) {
customerId = System.Environment.GetEnvironmentVariable("CUSTOMER_ID");
}
if (System.Environment.GetEnvironmentVariable("API_KEY") != null) {
apiKey = System.Environment.GetEnvironmentVariable("API_KEY");
}
if (System.Environment.GetEnvironmentVariable("PHONE_NUMBER") != null) {
phoneNumber = System.Environment.GetEnvironmentVariable("PHONE_NUMBER");
}
- This code adds an extra digit to the number to make it invalid.
string extraDigit = "0";
string incorrectPhoneNumber = string.Format("{0}{1}", phoneNumber, extraDigit);
- Create a try-catch structure.
try
{
}
catch (Exception e)
{
}
- In the
try
block, instantiate a PhoneIdClient object and pass it your customer ID and your API key.
PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, apiKey);
- Next in the
try
block, use Phone ID to check and cleanse the incorrect phone number you created earlier.
RestClient.TelesignResponse telesignResponse = phoneIdClient.PhoneId(incorrectPhoneNumber);
- Next in the
try
block, 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 (telesignResponse.OK)
{
Console.WriteLine(string.Format("Cleansed phone number has country code {0} and phone number is {1}.",
telesignResponse.Json["numbering"]["cleansing"]["call"]["country_code"],
telesignResponse.Json["numbering"]["cleansing"]["call"]["phone_number"]));
Console.WriteLine(string.Format("Original phone number was {0}.",
telesignResponse.Json["numbering"]["original"]["complete_phone_number"]));
}
- In the
catch
block, add code to print any error that might occur.
Console.WriteLine(e);
- After the try-catch structure, add code to allow you to exit the program.
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
- 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:
dotnet build
dotnet run
Sample code
using System;
using Telesign;
namespace PhoneId
{
class PhoneId
{
static void Main(string[] args)
{
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "EXAMPLEABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
string phoneNumber = "Your test phone number with no special characters or spaces.";
if (System.Environment.GetEnvironmentVariable("CUSTOMER_ID") != null) {
customerId = System.Environment.GetEnvironmentVariable("CUSTOMER_ID");
}
if (System.Environment.GetEnvironmentVariable("API_KEY") != null) {
apiKey = System.Environment.GetEnvironmentVariable("API_KEY");
}
if (System.Environment.GetEnvironmentVariable("PHONE_NUMBER") != null) {
phoneNumber = System.Environment.GetEnvironmentVariable("PHONE_NUMBER");
}
string extraDigit = "0";
string incorrectPhoneNumber = string.Format("{0}{1}", phoneNumber, extraDigit);
try
{
PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, apiKey);
RestClient.TelesignResponse telesignResponse = phoneIdClient.PhoneId(incorrectPhoneNumber);
if (telesignResponse.OK)
{
Console.WriteLine(string.Format("Cleansed phone number has country code {0} and phone number is {1}.",
telesignResponse.Json["numbering"]["cleansing"]["call"]["country_code"],
telesignResponse.Json["numbering"]["cleansing"]["call"]["phone_number"]));
Console.WriteLine(string.Format("Original phone number was {0}.",
telesignResponse.Json["numbering"]["original"]["complete_phone_number"]));
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
}
Updated about 22 hours ago