Twilio Proxy for masked phone numbers in Node.js


Twilio Proxy for masked phone numbers in Node.js

Twilio Proxy for masked phone numbers in Node.js

Twilio offers a service called Proxy to allow masked phone numbers. What that means? You know when you call an Uber and the number shown to you is not the phone number of your driver?! Exactly that means masked phone number.

The Proxy API connects two parties together, allowing them to communicate and keep personal information private.

masked phone numbers

How to make a call in Node.js

Let’s get to the point. I’ve made a twilio-service.js

import Twilio from 'twilio'

const TWILIO_SERVICE_ID = 'YOUR_SERVICE_ID' // create a service here: https://console.twilio.com/us1/develop/proxy/services
const TWILIO_ACCOUNT_SID= 'YOUR_ACCOUNT_ID'
const TWILIO_AUTH_TOKEN = 'YOUR_AUTH_TOKEN'

const twilioService = function () {
  let client
  if (!client) {
    client = new Twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, {
      logLevel: 'debug'
    })
    delete client.constructor
  }

  return {
    _client: client,

    /**
     * Creates new session to prepare a call
     * docs: https://www.twilio.com/docs/proxy/api/session
     * @returns
     */
    createSession: async function () {
      return client.proxy.services(TWILIO_SERVICE_ID).sessions.create({
        mode: 'voice-only'
      })
    },

    /**
     * Delete a call sesion
     * @param {string} sessionId The id of session
     * docs: https://www.twilio.com/docs/proxy/api/session#delete-a-session-resource
     * @returns
     */
    deleteSession: async function (sessionId) {
      return client
        .proxy
        .services(TWILIO_SERVICE_ID)
        .sessions(sessionId)
        .remove()
    },

    /**
     * Add new participant to the call
     * Maximum 2 participants per call are allowed
     * docs: https://www.twilio.com/docs/proxy/api/participant
     *
     * @param {string} sessionId The id of session
     * @param {string} phoneNumber The phone number of participant to call
     * @returns
     */
    addParticipantToSession: async function (sessionId, phoneNumber) {
      return client
        .proxy
        .services(TWILIO_SERVICE_ID)
        .sessions(sessionId)
        .participants
        .create({
          identifier: phoneNumber
        })
    }

  }
}

export default twilioService()

To make a call only need 3 steps:

  1. Create a new proxy session
  2. Add participants to the session created previously. We have two participants. Each of them has a property identifier (their real phone number) and has assigned a new property proxyIdentifier (the masked phone number).
  3. Now each of them can call their own proxyIdentifier, Twilio match their masked number with the masked number of the recipient, and call then the real number of the recipient.
  import TwilioService from '../services/twilio';

  const participant1Number = '+14156789012';
  const participant2Number = '+17012345678';

  const session = await TwilioService.createSession()
  // session = {
  //   ...
  //    "sid": "KCaa270143d7a1ef87f743313a07d4069d",
  //    ...
  // }
  const participantFrom = await TwilioService.addParticipantToSession(session.sid,participant1Number)
  // participantFrom = {
  //   "sid": "KPa4947c3d7b8ca7b0138dbc8181f12e16",
  //   "sessionSid": "KCaa270143d7a1ef87f743313a07d4069d",
  //   "identifier": "+14156789012",
  //   "proxyIdentifier": "+14153749231",
  // }
  const participantTo = await TwilioService.addParticipantToSession(session.sid, participant2Number)  
  participantTo = {
  //   "sid": "KP48f197284b3f50c5b31891ecc8377c20",
  //   "sessionSid": "KCaa270143d7a1ef87f743313a07d4069d",
  //   "identifier": "+17012345678",
  //   "proxyIdentifier": "+40371246711",
  // }

What is next?

Let’s say you are +14156789012 and you need to connect with +17012345678. You’ve run the previous code, now Twilio created a proxy between these two numbers. But you don’t know the number of +17012345678.

Take your phone, make a call to your proxyIdentifier, +14153749231, Twilio identifies your identity and match your masked phone number with the other paticipant, and call their real name +17012345678, but you never know which is the real number of your companion speaker. To make the call from the other side, do the same.

Do you wanna cancel the proxy between two of them?

 await TwilioService.deleteSession(session.sid);

That is all boys. For further questions send me an email or message on Twitter. Do you like the article? Share it!

Newsletter


Related Posts

A Week in the Life of an Invoice Wrangler: Navigating Ridesharing and Food Delivery Chaos

As an app founder in the ridesharing and food delivery industry, I found myself knee-deep in invoice reports from companies like Bolt, Uber, Glovo, and Bolt Food

Free HTML templates list for Startups

Free HTML templates list for startup. A complete list with free resources to build your next startup's website and gain the traction to the sky.

Deal with client requests in SaaS

How to deal with client requests in Saas which are seeing only their interests, not the product interest.

The first client of LoyalXpert is not answering anymore

Trying to implement LoyalXpert app, I lost my first customer, he's not answering anymore

Experiments with Tiktok Ads

Recently tried out TikTok ads for the first time and here are some of my learnings and challenges

People don’t care about you, until they know you care about them.

People don’t care about you, until they know you care about them. The same happens in business, you need to take care of your clients.

The One Word That Can Ruin Your SaaS Business Anyone

As a SaaS founder, you probably know how important it is to have a clear and specific target market for your product.

How I got my digital certificate connected it with ANAF

How I got my digital certificate from certSIGN and connected it with ANAF

The Ultimate List of Company Directories to Boost Your Networking

Discover a wide range of company directories to boost your business's visibility and connect with potential clients.

Discover the Best Free AI Art Tools for Your Next Masterpiece

Explore a curated collection of the finest free AI art tools, designed to help you bring your artistic vision to life.