Phone ID - Tutorial: Perform a number deactivation check with Telesign C# 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 C# 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:

  • 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?
  • Identity attribute enabled - Make sure the Number Deactivation identity attribute is enabled by Telesign. If it is not yet enabled, contact a Telesign expert.
  • 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

  1. Follow the Telesign Full-service C# SDK install instructions here on GitHub incorporating these details:

    • Use phoneidTest as the project 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.

Make Number Deactivation request

  1. Open the file "Program.cs".

  2. Follow the steps below or copy the sample code into a file and update accordingly.

  3. Add the imports below. The Telesign import refers to the Telesign self-service SDK.

    using Newtonsoft.Json.Linq;
    using System;
    using Telesign;
    
  4. Add the following basic structure below the imports.

    namespace TelesignEnterprise.Example.PhoneNumberDeactivation
    {
        public class CheckPhoneNumberDeactivated
        {
            public static void Main(string[] args)
            {
     
            }
        }
    }
    

    In the next step, you will begin adding statements to the Main function in the PhoneId class.

  5. 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 8).

    string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
    string apiKey = "EXAMPLEABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
    
  6. 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.

    string phoneNumber = "11234567890";
    

    📘

    NOTE:

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

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

    string ucid = "ATCK";
    
  8. (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");
    }
    
  9. Create a try-catch structure.

    try
    {
    
    }
    catch (Exception e)
    {
     
    }
    
    
  10. In the try block, instantiate a PhoneIdClient object and pass it your customer ID and your API key.

    PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, apiKey);
    
  11. Next in the try block, use Phone ID to perform a number deactivation check, and store the results in the telesignResponse variable.

    RestClient.TelesignResponse telesignResponse = phoneIdClient.NumberDeactivation(phoneNumber, ucid);
    
  12. Next in the try block, use two print statements to check the results of the number deactivation check on the phone number. The first print statement explains when the phone number was last deactivated is if that occurred and displays the JSON response for the deactivated number. Otherwise, the second print statement indicates that the phone number was not deactivated and displays the JSON response for the phone number.

    if (telesignResponse.OK)
                    {
                        if (telesignResponse.Json["number_deactivation"]["last_deactivated"].Type != JTokenType.Null)
                        {
                            Console.WriteLine(string.Format("Phone number {0} was last deactivated {1}.",
                                    telesignResponse.Json["number_deactivation"]["number"],
                                    telesignResponse.Json["number_deactivation"]["last_deactivated"]));
                        }
                        else
                        {
                            Console.WriteLine(string.Format("Phone number {0} has not been deactivated.",
                                    telesignResponse.Json["number_deactivation"]["number"]));
                        }
                    }
                } 
    
  13. In the catch block, add code to print any error that might occur.

    Console.WriteLine(e);
    
  14. After the try-catch structure, add code to allow you to exit the program.

    Console.WriteLine("Press any key to quit.");
    Console.ReadKey();
    

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.

    dotnet build
    dotnet run
    

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

using Newtonsoft.Json.Linq;
using System;
using Telesign;

namespace TelesignEnterprise.Example.PhoneNumberDeactivation
{
    public class CheckPhoneNumberDeactivated
    {
        public 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("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 ucid = "ATCK";

            try
            {
                PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, apiKey);
                RestClient.TelesignResponse telesignResponse = phoneIdClient.NumberDeactivation(phoneNumber, ucid);

                if (telesignResponse.OK)
                {
                    if (telesignResponse.Json["number_deactivation"]["last_deactivated"].Type != JTokenType.Null)
                    {
                        Console.WriteLine(string.Format("Phone number {0} was last deactivated {1}.",
                                telesignResponse.Json["number_deactivation"]["number"],
                                telesignResponse.Json["number_deactivation"]["last_deactivated"]));
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Phone number {0} has not been deactivated.",
                                telesignResponse.Json["number_deactivation"]["number"]));
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.WriteLine("Press any key to quit.");
            Console.ReadKey();
        }
    }
}