Introduction
This tutorial shows how to build a demonstration serverless solution using Firebase Callable Functions and the Vonage Identity Insights API, which automatically attempts to cleanse user input. You don't have to worry if your users submit numbers mixed with dashes, spaces, or alphanumeric characters. The API will do its best to parse and validate the phone number.
Validating phone numbers and making sure they're cleaned and correctly formatted is a must. In the demonstration we will build, you can input a phone number and use the Vonage Identity Insights API to return the format validity, international format, country code, current and original carrier name, and network type.
Phone Number Validation
Prerequisites
Make sure you have:
- A Google account with Firebase with Blaze billing enabled (required for Node.js functions).
A Google account with Firebase with Blaze billing enabled (required for functions).
- The Firebase CLI installed: npm install -g firebase-tools
The Firebase CLI installed: npm install -g firebase-tools
- A Vonage account.
A Vonage account.
File Structure
The sample code for this project follows the structure below.
Let's associate this project directory with a Firebase project. You can create multiple project aliases by running firebase use --add.
Initialize the Firebase Function
Initialize Firebase Functions in your project directory:
Select JavaScript when prompted. There's also the option to choose TypeScript and Python. Next, navigate into your functions folder and install the Vonage Server SDK:
- To create an application, go to the Create an Application page on the Vonage Dashboard, and define a Name for your Application.
To create an application, go to the Create an Application page on the Vonage Dashboard, and define a Name for your Application.
- If you intend to use an API that uses Webhooks, you will need a private key. Click “Generate public and private key”, your download should start automatically. Store it securely; this key cannot be re-downloaded if lost. It will follow the naming convention private_<your app id>.key. This key can now be used to authenticate API calls. Note: Your key will not work until your application is saved.
If you intend to use an API that uses Webhooks, you will need a private key. Click “Generate public and private key”, your download should start automatically. Store it securely; this key cannot be re-downloaded if lost. It will follow the naming convention private_<your app id>.key. This key can now be used to authenticate API calls. Note: Your key will not work until your application is saved.
- Choose the capabilities you need (e.g., Voice, Messages, RTC, etc.) and provide the required webhooks (e.g., event URLs, answer URLs, or inbound message URLs). These will be described in the tutorial.
Choose the capabilities you need (e.g., Voice, Messages, RTC, etc.) and provide the required webhooks (e.g., event URLs, answer URLs, or inbound message URLs). These will be described in the tutorial.
- To save and deploy, click "Generate new application" to finalize the setup. Your application is now ready to use with Vonage APIs.
To save and deploy, click "Generate new application" to finalize the setup. Your application is now ready to use with Vonage APIs.
The Vonage Identity Insights API uses JWTs, so we need to securely store our Application ID and Private Key. We'll use Firebase's built-in Secret Manager so these values never live in our source code:
It will then prompt you to add the Vonage Application ID.
It will then prompt you to add the Vonage Private Key associated with your application ID.
Understand the Backend Logic
We'll use the Vonage Identity Insights API. Firebase Callable Functions handle CORS and authentication automatically, allowing your frontend to call them. You can follow along with the full index.js file in the sample repository.
We initialize the Vonage client. Identity Insights requires JWT authentication. We're initializing the client using the secrets we stored earlier.
Note: The .replace(/\n/g, '\n') is a fix for handling newline formatting issues when using Firebase Secret Manager.
Note: The .replace(/\n/g, '\n') is a fix for handling newline formatting issues when using Firebase Secret Manager.
We request both the format and current_carrier Identity Insights API insights to know not just if the phone number is technically valid, but also whether it's a mobile line we can actually send an SMS to.
We package everything up and send a response object back to our frontend, so it doesn't have to parse raw API responses.
Deploy Your Function to the Cloud
Run the following command in your terminal from the root directory of your project to deploy your function to Firebase:
During the deployment process, the Firebase CLI will package your function source code, upload it to Cloud Functions, and set up the necessary Cloud Secret Manager permissions for your configured secrets. You will see progress logs in your terminal detailing each step, ending with a success message and the URL endpoint for your deployed function.
To verify that the deployment was successful, open the Firebase Console, select your project, and navigate to the Functions section. You should see validatePhone listed under the dashboard along with its status, runtime, and trigger type.
Connect the Frontend
Because we used a Callable Function, invoking this from your web app is clean. You don't need to mess with fetch requests, HTTP headers, or parsing JSON.
Create an index.html file and paste the code, remembering to swap in your Firebase project configuration.
Test It Out
Open the HTML file in your browser, type a phone number in the input field including the country code, and hit Validate Number. You'll get information about the phone number, just as shown in the image at the beginning of this tutorial.
Conclusion
The Identity Insights API helps your application accept only real, routable phone numbers while gathering valuable carrier intelligence. To learn more about what data you can extract from phone numbers, check out the Vonage Identity Insights API documentation.












