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

📘

NOTE:

This tutorial uses the following:

  • MacOS
  • Ruby 3.3.6

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

Set up your project

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

    • Use phoneidTest as the project name.

    You should end up in the top-level directory for your project in the Terminal.

  2. Create a new file called phone_id_check_type.rb and open it in your editor.

touch phone_id_check_type.rb

Check phone type

  1. Follow the steps below or copy the sample code into the file and update it accordingly.
  2. Import this dependency from the SDK.
require 'telesign'
  1. Define variables to store your authentication credentials.
customer_id = ENV['CUSTOMER_ID'] || 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = ENV['API_KEY'] || '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 the phone number, use the complete number, including the country code. This should be a string with no spaces or special characters. For example: 16505551212.
phone_number = ENV['PHONE_NUMBER'] || '11234567890'
phone_type_voip = '5'

📘

NOTE:

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

  1. Instantiate a PhoneIdClient object and pass it your customer ID and your API key.
phoneid_client = Telesign::PhoneIdClient.new(customer_id, api_key)
  1. Use Phone ID to check your phone number and store the results.
response = phoneid_client.phoneid(phone_number)
  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 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

📘

NOTE:

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

  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

Switch from your editor to the terminal and run:

ruby phone_id_check_type.rb

Sample code

require 'telesign'

customer_id = ENV['CUSTOMER_ID'] || 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890'
api_key = ENV['API_KEY'] || 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw=='
phone_number = ENV['PHONE_NUMBER'] || '11234567890'

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