Number Masking Using Node.js

    Overview

    Phone number masking hides the phone numbers of parties in a call from each other. Many businesses find it advantageous to anonymize communication between two parties — for example, between a customer and a delivery agent on a food delivery service platform or a driver and a rider using a ride-hailing application. Businesses can implement phone number masking by sending calls through an intermediate phone number that acts as a proxy between the two parties. A Plivo number can serve as the intermediate number to connect the two parties while keeping their contact information private.

    How it works

    As an example, we’ll build a number-masking application for a food delivery service that lets the company connect customers with delivery agents and vice versa without revealing any actual phone numbers. To do this, you

    1. Create a customer-to-agent phone number mapping in your application’s back end.
    2. Create the number masking application using Plivo.
    3. Assign the number masking application to a Plivo number.

    Prerequisites

    To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. You must have a voice-enabled Plivo phone number to receive incoming calls; you can rent numbers from the Numbers page of the Plivo console, or by using the Numbers API. If this is your first time using Plivo APIs, follow our instructions to set up a Node.js development environment and a web server and safely expose that server to the internet.

    Create a 1:1 map with actual numbers

    Create customer-to-agent phone number mapping for the application. Whenever a customer places an order, their phone number should be stored in a database for your application to access. A delivery agent will be assigned for the order, and the agent’s number will also be stored in your database, and will be mapped to the customer’s number:

    Customer’s Number		<->		Agent’s Number
      1-415-666-7777					1-415-666-7778
    

    We created sample mapping data in a config.js file:

        const config = {app: {port: 5000}}
    	config.customerAgentMap = {
    		'14156667777':'14156667778', 
    		'14156667779':'14156667780', 
    		'14156667781':'14156667782'
    	};
    	module.exports = config;
    

    Create an Express application for number masking

    Create a file called number_masking.js and paste into it this code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    
    const config = require('./config');
    const plivo = require('plivo');
    const express = require('express');
    const app = express();
    app.set('port', (process.env.PORT || 5000));
    
    // Handle incoming calls to a Plivo number, connect agent with customer and vice versa without revealing their actual phone numbers. 
    app.all('/handleincoming/', function(req, res) {
        const fromNumber = (req.query.From);
        const toNumber = (req.query.To);
        const response = plivo.Response();
        const customerPhoneMapping = config.customerAgentMap;
        const agentCustomerMapping = Object.fromEntries(Object.entries(customerPhoneMapping).map(v => v.reverse()));
        if(fromNumber in customerPhoneMapping){ // Check whether the customer's number is in the customer-agent mapping
            const number = customerPhoneMapping[fromNumber]; // Assign the value from the customer-agent array to number variable
            const params = {
                'callerId': toNumber, // Plivo number is used as the caller ID for the call toward the agent
            };
            const dial = response.addDial(params);
            const destNumber = number;
            dial.addNumber(destNumber);
            res.send(response.toXML());
        }
        else if(fromNumber in agentCustomerMapping){ // Check whether the agent's number is in the customer-agent mapping
            const number = agentCustomerMapping[fromNumber]; // Assign the key from the customer-agent array to number variable
            const params = {
                'callerId': toNumber, // Plivo number is used as the caller ID for the call toward the customer
            };
            const dial = response.addDial(params);
            const destNumber = number;
            dial.addNumber(destNumber);
            res.send(response.toXML());
        }
    });
    
    app.listen(app.get('port'), function () {
    	console.log('Node app is running on port', app.get('port'));
    });
    

    Save the file and run it.

    $ node number_masking.js
    

    You should see your basic server application in action at http://localhost:5000/handleincoming/.

    Set up ngrok to expose your local server to the internet.

    Now people can call your Plivo number. If an incoming call to your Plivo number is from one of the customer phone numbers in the customer-agent map — for example, if the caller number is 14156667777 — then Plivo will send the XML response to process the incoming call as below, and you can check the XML document in your browser.

    XML Document - Customer Calls Agent

    If the incoming call to your Plivo number is from one of the agent phone numbers in the customer-agent map — for example, if the caller number is 14156667778 — then Plivo will send the XML response to process the incoming call as below, and you can check the XML document in your browser.

    XML Document - Agent Calls Customer

    Create a Plivo application

    Associate the Express application you created with Plivo by creating a Plivo application. Visit Voice > Applications in the Plivo console and click on Add New Application, or use Plivo’s Application API.

    Give your application a name — we called ours Number Masking. Enter the server URL you want to use (for example, https://<ngrok_identifier>.ngrok.io/handleincoming/) in the Answer URL field and set the method as GET. Click Create Application to save your application.

    Create Application — Number Masking

    Assign a Plivo number to your application

    Navigate to the Numbers page and select the phone number you want to use for this application.

    From the Application Type drop-down, select XML Application.

    From the Plivo Application drop-down, select Number Masking (the name we gave the application).

    Click Update Number to save.

    Assign Application - Number Masking

    Test

    To test the application, you need two Plivo numbers. Set up one of your numbers as a customer and another as an agent in the customer-to-agent mapping data in the config file. Make a call from each of your mobile numbers to the Plivo number you mapped to the application. You should see that the call is forwarded to the other number, and that the incoming call has the Plivo number as the caller ID.