Phone ID - Tutorial: Check phone type to block VoIP
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 an 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.
Set up your project
-
Follow the Telesign Self-service SDK install instructions in your preferred language. Select the language here, and you will be directed to GitHub where you can access the source code and installation instructions.
-
Create a new file called
phone_id_check_type.<file_extension>
(use the appropriate extension for your programming language) and open it in your editor.
touch phone_id_check_type.<file_extension>
- For Java only: 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
- Follow the steps below or copy the sample code into the file and update it accordingly.
- Include the appropriate part of the Telesign SDK and any additional language specific functions you may need.
const TeleSignSDK = require('telesignsdk');
package com.telesign.example.phoneid;
import com.telesign.PhoneIdClient;
import com.telesign.RestClient;
from __future__ import print_function
from telesign.phoneid import PhoneIdClient
#If you are working with a version of Python 3, you do not need the first import
#statement (`from __future__ import print_function`).
require 'telesign'
<?php
require __DIR__ . "/../../vendor/autoload.php";
use telesign\sdk\phoneid\PhoneIdClient;
using System;
- Insert values for each of the items from
customer_id
/customerId
throughphone_number
/phoneNumber
.
- customer_id/customerId - Your TeleSign assigned customer ID, available in your account information in the portal.
- api_key/apiKey - Your TeleSign assigned API key, available in your account information in the portal.
- phone_number / phoneNumber - The phone number purchased from TeleSign in TelePortal that you want to use. When you provide the number, it should be a string with no spaces or special characters. Include the complete number; for example, for a US phone number, you would include the country code and the area code with the number, like this:
16505551212
.
const customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
const rest_endpoint = "https://rest-api.telesign.com";
const timeout = 10*1000; // 10 secs
const client = new TeleSignSDK( customerId,
apiKey,
rest_endpoint,
timeout // optional
// userAgent
);
const phoneNumber = "Test phone number with no special characters or spaces goes here.";
public class CheckPhoneTypeToBlockVoip {
public static void main(String[] args) {
String customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
String apiKey = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
String phoneNumber = "Test phone number with no special characters or spaces goes here.";
String phoneTypeVoip = "5";
customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"
api_key = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="
phone_number = "Test phone number with no special characters or spaces goes here."
phone_type_voip = "5"
customer_id = 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = 'EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=='
phone_number = 'Test phone number with no special characters or spaces goes here.'
phone_type_voip = '5'
$customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
$api_key = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
$phone_number = "Test phone number with no special characters or spaces goes here.";
$phone_type_voip = "5";
namespace Telesign.Example.PhoneId
{
class CheckPhoneTypeToBlockVoip
{
static void Main(string[] args)
{
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "EXAMPLEABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
string phoneNumber = "phone_number";
string phoneTypeVoip = "5";
- Instantiate a PhoneIdClient object containing your customer ID and API key. For Node.js, open a console log here.
console.log("## PhoneIDClient.phoneID ##");
try {
PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, secretKey);
data = PhoneIdClient(customer_id, api_key)
phoneid_client = Telesign::PhoneIdClient.new(customer_id, api_key)
$data = new PhoneIdClient($customer_id, $api_key);
try
{
PhoneIdClient phoneIdClient = new PhoneIdClient(customerId, apiKey);
- Use Phone ID to check your phone number and store the results of your check in
response
/telesignResponse
. For Node.js, declare a function that will check and store the response that comes from Phone ID.
function messageCallback(error, responseBody) {
if (error === null) {
console.log(`PhoneID response for phone number: ${phoneNumber}`
+ ` => code: ${responseBody['status']['code']}`
+ `, description: ${responseBody['status']['description']}`);
if (responseBody['status']['code'] === 200) {
if (responseBody['phone_type']['code'] === phoneTypeVOIP) {
console.log("Phone type in request is VoIP");
} else {
console.log("Phone type in request is not VoIP");
}
}
} else {
console.error("Unable to get PhoneID. " + error);
}
}
RestClient.TelesignResponse telesignResponse = phoneIdClient.phoneid(phoneNumber, null);
response = data.phoneid(phone_number)
response = phoneid_client.phoneid(phone_number)
$response = $data->phoneid($phone_number);
RestClient.TelesignResponse telesignResponse = phoneIdClient.PhoneId(phoneNumber);
- 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.
For Node.js, this is where you create a PhoneID client. You pass in the function you defined in the last step.
client.phoneid.phoneID(messageCallback, phoneNumber);
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();
}
}
}
if response.ok:
if response.json['phone_type']['code'] == phone_type_voip:
print("Phone number {} is a VoIP phone.".format(
phone_number))
else:
print("Phone number {} is not a VoIP phone.".format(
phone_number))
if response.ok
if response.json['phone_type']['code'] == phone_type_voip
puts "Phone number #{phone_number} is a VoIP phone."
else
puts "Phone number #{phone_number} is not a VoIP phone."
end
end
if ($response->ok) {
if ($response->json['phone_type']['code'] == $phone_type_voip) {
echo "Phone number $phone_number is a VoIP phone.";
}
else {
echo "Phone number $phone_number 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));
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
}
NOTE:
In a production integration, you would instead implement logic here to block the VoIP number.
- 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
- Switch from your editor to the terminal and run:
node phone_id_check_type.js
./gradlew build
./gradlew run
python phone_id_check_type.py
ruby phone_id_check_type.rb
php src/phone_id_check_type.php
dotnet build
dotnet run
Sample code
const TeleSignSDK = require('telesignsdk');
const customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
const rest_endpoint = "https://rest-api.telesign.com";
const timeout = 10*1000; // 10 secs
const client = new TeleSignSDK( customerId,
apiKey,
rest_endpoint,
timeout // optional
// userAgent
);
const phoneNumber = "Test phone number with no special characters or spaces goes here.";
const phoneTypeVOIP = "5";
console.log("## PhoneIDClient.phoneID ##");
function messageCallback(error, responseBody) {
if (error === null) {
console.log(`PhoneID response for phone number: ${phoneNumber}`
+ ` => code: ${responseBody['status']['code']}`
+ `, description: ${responseBody['status']['description']}`);
if (responseBody['status']['code'] === 200) {
if (responseBody['phone_type']['code'] === phoneTypeVOIP) {
console.log("Phone type in request is VoIP");
} else {
console.log("Phone type in request is not VoIP");
}
}
} else {
console.error("Unable to get PhoneID. " + error);
}
}
client.phoneid.phoneID(messageCallback, phoneNumber);
package com.telesign.example.phoneid;
import com.telesign.PhoneIdClient;
import com.telesign.RestClient;
public class CheckPhoneTypeToBlockVoip {
public static void main(String[] args) {
String customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
String apiKey = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
String phoneNumber = "Test phone number with no special characters or spaces goes here.";
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();
}
}
}
from __future__ import print_function
from telesign.phoneid import PhoneIdClient
customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"
api_key = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=="
phone_number = "Test phone number with no special characters or spaces goes here."
phone_type_voip = "5"
data = PhoneIdClient(customer_id, api_key)
response = data.phoneid(phone_number)
if response.ok:
if response.json['phone_type']['code'] == phone_type_voip:
print("Phone number {} is a VoIP phone.".format(
phone_number))
else:
print("Phone number {} is not a VoIP phone.".format(
phone_number))
require 'telesign'
customer_id = 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = 'EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=='
phone_number = 'Test phone number with no special characters or spaces goes here.'
phone_type_voip = '5'
phoneid_client = Telesign::PhoneIdClient.new(customer_id, api_key)
response = phoneid_client.phoneid(phone_number)
if response.ok
if response.json['phone_type']['code'] == phone_type_voip
puts "Phone number #{phone_number} is a VoIP phone."
else
puts "Phone number #{phone_number} is not a VoIP phone."
end
end
<?php
require __DIR__ . "/../../vendor/autoload.php";
use telesign\sdk\phoneid\PhoneIdClient;
$customer_id = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
$api_key = "EXAMPLE----ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
$phone_number = "Test phone number with no special characters or spaces goes here.";
$phone_type_voip = "5";
$data = new PhoneIdClient($customer_id, $api_key);
$response = $data->phoneid($phone_number);
if ($response->ok) {
if ($response->json['phone_type']['code'] == $phone_type_voip) {
echo "Phone number $phone_number is a VoIP phone.";
}
else {
echo "Phone number $phone_number is not a VoIP phone.";
}
}
using System;
namespace Telesign.Example.PhoneId
{
class CheckPhoneTypeToBlockVoip
{
static void Main(string[] args)
{
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "EXAMPLEABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
string phoneNumber = "phone_number";
string phoneTypeVoip = "5";
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();
}
}
}
curl -X POST https://rest-api.telesign.com/v1/phoneid/<complete_phone_number> \
-d account_lifecycle_event="sign-in" \
-u "CUSTOMER_ID":"API_KEY"
Updated 17 days ago