Guides

Validate phone numbers with Functions

Evervault Functions are secure, isolated serverless functions that can be used to process encrypted data.

In this guide, you will use Evervault Functions to validate encrypted phone numbers, normalize the formatting of the phone number, and then return the encrypted and normalized phone number alongside a validity check — something that would normally be impossible to do on encrypted data.


Setting up your Evervault Account

Before creating an Evervault Function, you’ll need to have an Evervault account. If you haven’t done so already you can sign up at app.evervault.com. Once your account is set up you’ll need to create an App, to do so, just click the Create App button in the Evervault dashboard.


Setting up the Evervault CLI

Functions are either managed through Github, or the Evervault CLI. In this guide, you will create a Function using the CLI, however, you can check out the docs for Evervault Functions to learn more about how to set one up through Github.

Run the command below in your terminal of choice to download and install the Evervault CLI.

lovelace:~$
sh <(curl https://cli.evervault.com/v3/install -sL)

Next, you’ll need to login and select your team and app. Just run the command below and follow the prompts to get signed in.

lovelace:~$
ev login

Create your Function

The Evervault CLI has a Function quickstart command. The command below will initialize a Function in your current directory.

lovelace:~$
ev function init

To follow this tutorial, be sure to select node as your Function’s language.

Next, you’ll need to install the phone library from npm.

lovelace:~$
# Using npm
npm install phone
# Using yarn
yarn add phone

Now open up index.js in your favourite code editor and replace it with the following:

1
const { phone } = require('phone');
2
3
exports.handler = async ({ country, number }, context) => {
4
if (country && number) {
5
const { isValid, phoneNumber } = phone(number, { country });
6
7
return {
8
isValid,
9
encryptedPhoneNumber: await context.encrypt(phoneNumber)
10
};
11
} else {
12
return {
13
isValid: false,
14
encryptedPhoneNumber: undefined,
15
};
16
}
17
};

The code above validates and formats a phone number for the country provided when it is invoked. The formatted phone number is then returned from the function as an Evervault-encrypted string within the encryptedPhoneNumber value.


Deploying your Function

To deploy the Function, run the following command:

lovelace:~$
ev function deploy

The deploy command is also used to make updates to an existing Function.


Running your Function

When you run your Function, any encrypted data present in the request is automatically decrypted before it reaches your code. You can also pass plaintext data to a Function alongside encrypted data.

To run your Function using the CLI, run the following command:

lovelace:~$
ev function run --data '{"number": "123456789", "country": "IE"}'

The CLI will attempt to run a Function that has been deployed from the directory that it is currently in. If you wish to invoke it from outside of that directory, you will need to pass an additional --name parameter.

If the phone number and country code combination you provided is valid, then isValid will equal true, and there will be an encrypted phone number returned.

You can pass this encrypted phone number back into the Function to see how it works on encrypted data.