Phone ID - Tutorial: Check phone type to block VoIP 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 Telesign C# 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.
  • 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 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.

Check phone type

  1. Open the file "Program.cs".
  2. Follow the steps below or copy the sample code into the file and update it accordingly.
  3. Add the imports below. The Telesign import refers to the Telesign Self-service SDK.
using System;
using Telesign;
  1. 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.

  1. 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==";
  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 this tutorial, hardcode your testing device's phone number or pull it from an environment variable (see step 7).
string phoneNumber = "11234567890";
string phoneTypeVoip = "5";

📘

NOTE:

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

  1. (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");
}   
  1. Create a try-catch structure.
try
{

}
catch (Exception e)
{
 
}
  1. In the try block, instantiate a PhoneIdClient object and pass it your customer ID and your API key.
PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, apiKey);
  1. Next in the try block, use Phone ID to check your phone number and store the results.
RestClient.TelesignResponse telesignResponse = phoneIdClient.PhoneId(phoneNumber);
  1. Next in the try block, 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 (telesignResponse.OK)
   {
   	if (telesignResponse.Json["phone_type"]["code"].ToString() == phoneTypeVoip)
      	 {
           Console.WriteLine(string.Format("Phone number {0} is a VoIP phone.", phoneNumber));
         }
       else
         {
           Console.WriteLine(string.Format("Phone number {0} is not a VoIP phone.", phoneNumber));
         }
  }

📘

NOTE:

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

  1. In the catch block, add code to print any error that might occur.
Console.WriteLine(e);
  1. After the try-catch structure, add code to allow you to exit the program.
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
  1. Run the code. 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

  1. Switch from your editor to the terminal and build the project:
dotnet build
  1. Run the project:
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 = "phone_number";

            string phoneTypeVoip = "5";
          
            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");
	    }   

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

                if (telesignResponse.OK)
                {
                    if (telesignResponse.Json["phone_type"]["code"].ToString() == phoneTypeVoip)
                    {
                        Console.WriteLine(string.Format("Phone number {0} is a VoIP phone.", phoneNumber));
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Phone number {0} is not a VoIP phone.", phoneNumber));
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

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