Phone ID - Tutorial: Check phone type to block VoIP with Telesign Java 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 Java 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.
  • Gradle: This package manager isn't required to use the SDK, but it is required for this tutorial.

📘

NOTE:

This tutorial uses the following:

  • MacOS
  • Java OpenJDK v19.0.1
  • Gradle 7.6

Please modify accordingly if your developer environment differs from these details.

Set up your project

  1. Follow the Telesign Self-service Java SDK install instructions here on GitHub incorporating these details:

    • Use phoneidTest as the project directory name.
    • Use JUnit 4 as the test framework.
    • Use phoneid as the project name.
    • Use phoneid as the source package name.

    You should end up in the top-level directory ("phoneidTest") for your project in the Terminal. This directory should contain an initialized Gradle project with the Telesign Self-service SDK included in its dependencies.

  2. From the top level of your project, delete the folder "app/src/test/". No tests are included in this tutorial.

rm -r app/src/test/

Check phone type

  1. Open the file "app/src/main/java/phoneid/App.java".
  2. Follow the steps below or copy the sample code into the file and update it accordingly.
  3. Add the imports below between the package declaration. These imports reference selected functionality from the Telesign Self-service SDKs, as well as some other Java utilities.
package phoneid;

import com.telesign.PhoneIdClient;  
import com.telesign.RestClient;
  1. Add the App class declaration and with the following basic structure.
public class App {
    public static void main(String[] args) {
    
    }
}

In the next step, you will begin adding statements to the main function in the App 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.
String customerId = System.getenv().getOrDefault("CUSTOMER_ID", "FFFFFFFF-EEEE-DDDD-1234-AB1234567890");
String apiKey = System.getenv().getOrDefault("API_KEY", "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==");
  1. Define a variable in the main function 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.
String phoneNumber = System.getenv().getOrDefault("PHONE_NUMBER", "123456789"); 
String phoneTypeVoip = "5";

📘

NOTE:

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

  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, null);
  1. 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.getAsJsonObject("phone_type").get("code").getAsString().equals(phoneTypeVoip)) {
            System.out.println(String.format("Phone number %s is a VOIP phone.", phoneNumber));
         } else {
            System.out.println(String.format("Phone number %s 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 display any type of exception and associated error code that might occur. Again, this is for debugging purposes. In your production code, you would likely remove this.
 e.printStackTrace();
  1. If everything works, 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:
./gradlew build
  1. Run the project:
./gradlew run

Sample code

package phoneid;

import com.telesign.PhoneIdClient;
import com.telesign.RestClient;

public class App {

    public static void main(String[] args) {

        String customerId = System.getenv().getOrDefault("CUSTOMER_ID", "FFFFFFFF-EEEE-DDDD-1234-AB1234567890");
        String apiKey = System.getenv().getOrDefault("API_KEY", "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==");

        String phoneNumber = System.getenv().getOrDefault("PHONE_NUMBER", "123456789");
        String phoneTypeVoip = "5";

        try {

            PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, apiKey);
            RestClient.TelesignResponse telesignResponse = phoneIdClient.phoneid(phoneNumber, null);

            if (telesignResponse.ok) {
                if (telesignResponse.json.getAsJsonObject("phone_type").get("code").getAsString().equals(phoneTypeVoip)) {
                    System.out.println(String.format("Phone number %s is a VoIP phone.", phoneNumber));
                } else {
                    System.out.println(String.format("Phone number %s is not a VoIP phone.", phoneNumber));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}