Phone ID - Tutorial: Cleanse a number with Telesign Node.js 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 Node.js SDK to cleanse a phone number. Phone number cleansing corrects common formatting issues in submitted phone numbers. Skip to the end of this page to see the full sample code.

This code will take your test phone number, add an extra digit to make it invalid, and send it to Phone ID to cleanse. It then takes the cleansed phone number from the response and prints it to the console.

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 cleanse.
  • 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
  • Node v16.9.0

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

Set up your project

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

    • Use phone_id as the project name.
  2. Create a new file called phone_id_cleanse_number.js and open it in your editor.

    touch phone_id_cleanse_number.js
    

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

Cleanse a number with Phone ID

  1. Follow the steps below or copy the sample code into the file and update it accordingly.
  2. Add the imports below. The telesignsdk import refers to the Telesign Self-service SDK.
const TelesignSDK = require('telesignsdk');
  1. Define variables to store your authentication credentials. For testing purposes, you can just overwrite the default values below or use environment variables.
const customerId = process.env.CUSTOMER_ID || "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = process.env.API_KEY || "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
  1. Define a variable to hold the phone number you want to cleanse. This should be a string with no spaces or special characters. Use the complete number, including the country code. For example: 16505551212.
const phoneNumber = process.env.PHONE_NUMBER || "11234567890";
  1. Define rest_endpoint and timeout.
const rest_endpoint = "https://rest-api.telesign.com";
const timeout = 10*1000; // 10 secs
  1. Instantiate a client object containing Customer ID, API key, REST endpoint, and timeout.
const client = new TeleSignSDK( customerId,
    apiKey,
    rest_endpoint,
    timeout // optional
    // userAgent
);
  1. Set up a function that will log, check, and cleanse the phone number it pulls from responseBody.
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) {
            const cc = responseBody['numbering']['cleansing']['call']['country_code'];
            const pn = responseBody['numbering']['cleansing']['call']['phone_number'];
            console.log("Cleansed phone number has country code $cc and phone number is $pn.")
        }
    } else {
        console.error("Unable to get PhoneID. $error");
    }
}
  1. Set up your phoneID client using the function you created in the last step. Everything should run and you will get back a response containing your cleansed phone number and the original phone number. The messageCallback function will print details about what happened.
client.phoneid.phoneID(messageCallback, phoneNumber);
  1. If everything works, you send an incorrect phone number to Phone ID for cleansing, and you get back a response containing your cleansed phone number and the original phone number.

Test your integration

  1. Switch from your editor to the terminal and run:
node phone_id_cleanse_number.js

Sample code

// note change this to the following if using npm package: require('telesignsdk);
const TeleSignSDK = require('telesignsdk');

const customerId = process.env.CUSTOMER_ID || "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = process.env.API_KEY || "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";

const phoneNumber = process.env.PHONE_NUMBER || "11234567890";

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
);

console.log("## PhoneIDClient.phoneID ##");

function messageCallback(error, responseBody) {
    if (error === null) {
        console.log(`Phone ID response for phone number: ${phoneNumber} ` +
            `=> code: ${responseBody['status']['code']}, ` +
            `description: ${responseBody['status']['description']}`);

        if (responseBody['status']['code'] === 200) {
            const cc = responseBody['numbering']['cleansing']['call']['country_code'];
            const pn = responseBody['numbering']['cleansing']['call']['phone_number'];
            console.log("Cleansed phone number has country code $cc and phone number is $pn.")
        }
    } else {
        console.error("Unable to get PhoneID. $error");
    }
}

client.phoneid.phoneID(messageCallback, phoneNumber);