Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Twilio Verify Node.js Phone Verification Quickstart


(warning)

Warning

Verify v1 API has reached End of Sale. It is now closed to new customers and will be fully deprecated in the future.

For new development, we encourage you to use the Verify v2 API. v2 has an improved developer experience and new features, including:

  • Twilio helper libraries in multiple languages
  • PSD2 Secure Customer Authentication Support
  • Improved Visibility and Insights

Existing customers will not be impacted at this time until Verify v1 API has reached End of Life. For more information about migration, see Migrating from 1.x to 2.x.

Phone verification is an important, high-confidence step in a registration flow to verify that a user has the device they claim to have. Adding phone verification to your application will greatly reduce your number of fraudulent registrations and protect future application users from having their numbers registered by scammers.

This quickstart guides you through creating a Node.js(link takes you to an external page), AngularJS(link takes you to an external page), and MongoDB(link takes you to an external page) app that requires a phone verification step to create an account. Two channels of phone verification are demoed: SMS and voice.

Ready to add phone verification to a demo app and keep the bad actors away? Enter stage left!


Sign Into - or Create- a Twilio Account

sign-into---or-create--a-twilio-account page anchor

Either sign up for a free Twilio trial, or sign into an existing Twilio account(link takes you to an external page).

Create a New Verify Application

create-a-new-verify-application page anchor

Once logged in, visit the Authy Console(link takes you to an external page). Click on the red 'Create New Application' (or big red plus ('+') if you already created one) to create a new Authy application then name it something memorable.

Authy create new application.

Twilio will redirect you to the Settings page next:

Account Security API Key.

Click the eyeball icon to reveal your Production API Key, and copy it somewhere safe. You will use the API Key during the application setup step below.


Install and Launch MongoDB

install-and-launch-mongodb page anchor

While Twilio's Verify API doesn't return any user information you'll need to store, to continue working on the app after this Quickstart you'll want a database. For this demo, we built our user database on top of MongoDB.

Instructions for installing MongoDB vary depending on platform. Please follow the appropriate link for instructions on how to install MongoDB for your platform.

After you install MongoDB, launch it. On *NIX and OSX, this command may be as simple as:


_10
mongod


Clone and Setup the Verification Application

clone-and-setup-the-verification-application page anchor

Start by cloning our Node.js repository.(link takes you to an external page) Enter the directory and use npm to install all of our dependencies:


_10
npm install

  1. Open the file .env.example
  2. Change ACCOUNT_SECURITY_API_KEY to the API Key from the above step
  3. Now, save the file as .env

Depending on your system, you need to set the environmental variables before you continue. On NIX, you can run:


_10
source .env

On Windows, depending on your shell, you will have to use SET.

Alternatively, you could use a package such as autoenv(link takes you to an external page) to load it at startup.

Enter an Application API Key

enter-an-application-api-key page anchor

Enter the API Key from the Account Security console and optionally change the port.


_10
export ACCOUNT_SECURITY_API_KEY='ENTER_SECRET_HERE'
_10
export PORT=1337

And then... actually, that's all the setup you'll need.

Now, launch Node with:


_10
node .

Assuming your API Key is correctly entered and MongoDB is running, you'll soon get a message that the app is up!


Use the Node.js Phone Verification Demo

use-the-nodejs-phone-verification-demo page anchor

Keeping your phone at your side, visit the phone verification page of the demo at http://localhost:1337/verification/(link takes you to an external page)

Enter a Country Code and Phone Number, then choose which channel to request verification over, 'SMS' or 'CALL' (Voice). Finally, hit the blue 'Request Verification' button and wait.

Phone Verification by SMS or Voice.

You won't be waiting long - you'll either receive a phone call or an SMS with the verification token. If you requested a phone call, as an additional security feature you may need to interact to proceed (by entering a number on the phone keypad).

Send a Phone Verification via SMS or Voice

send-a-phone-verification-via-sms-or-voice page anchor

This function allows you to send the verification code over SMS or Voice depending on the 'via' variable.


_103
var request = require('request');
_103
var VERSION = "0.1";
_103
_103
_103
module.exports = function (apiKey, apiUrl) {
_103
return new PhoneVerification(apiKey, apiUrl);
_103
};
_103
_103
function PhoneVerification(apiKey, apiUrl) {
_103
this.apiKey = apiKey;
_103
this.apiURL = apiUrl || "https://api.authy.com";
_103
this.user_agent = "PhoneVerificationRegNode/" + VERSION + " (node " + process.version + ")";
_103
this.headers = {};
_103
_103
this.init();
_103
}
_103
_103
PhoneVerification.prototype.init = function () {
_103
this.headers = {
_103
"User-Agent": this.user_agent
_103
};
_103
};
_103
_103
/**
_103
* Verify a phone number
_103
*
_103
* @param {!string} phone_number
_103
* @param {!string} country_code
_103
* @param {!string} token
_103
* @param {!function} callback
_103
*/
_103
PhoneVerification.prototype.verifyPhoneToken = function (phone_number, country_code, token, callback) {
_103
_103
console.log('in verify phone');
_103
this._request("get", "/protected/json/phones/verification/check", {
_103
"api_key": this.apiKey,
_103
"verification_code": token,
_103
"phone_number": phone_number,
_103
"country_code": country_code
_103
},
_103
callback
_103
);
_103
};
_103
_103
/**
_103
* Request a phone verification
_103
*
_103
* @param {!string} phone_number
_103
* @param {!string} country_code
_103
* @param {!string} via
_103
* @param {!function} callback
_103
*/
_103
PhoneVerification.prototype.requestPhoneVerification = function (phone_number, country_code, via, callback) {
_103
_103
this._request("post", "/protected/json/phones/verification/start", {
_103
"api_key": this.apiKey,
_103
"phone_number": phone_number,
_103
"via": via,
_103
"country_code": country_code,
_103
"code_length": 4
_103
},
_103
callback
_103
);
_103
};
_103
_103
PhoneVerification.prototype._request = function (type, path, params, callback, qs) {
_103
qs = qs || {};
_103
qs['api_key'] = this.apiKey;
_103
_103
options = {
_103
url: this.apiURL + path,
_103
form: params,
_103
headers: this.headers,
_103
qs: qs,
_103
json: true,
_103
jar: false,
_103
strictSSL: true
_103
};
_103
_103
console.log(options.url);
_103
_103
var callback_check = function (err, res, body) {
_103
if (!err) {
_103
if (res.statusCode === 200) {
_103
callback(null, body);
_103
} else {
_103
callback(body);
_103
}
_103
} else {
_103
callback(err);
_103
}
_103
};
_103
_103
switch (type) {
_103
case "post":
_103
request.post(options, callback_check);
_103
break;
_103
_103
case "get":
_103
request.get(options, callback_check);
_103
break;
_103
}
_103
};

Either way you requested the passcode, enter the token into the Verification entry form and click 'Verify Phone':

Phone Verification Entry Box.

This function verifies the token for a user delivered over the Voice or SMS channel.


_103
var request = require('request');
_103
var VERSION = "0.1";
_103
_103
_103
module.exports = function (apiKey, apiUrl) {
_103
return new PhoneVerification(apiKey, apiUrl);
_103
};
_103
_103
function PhoneVerification(apiKey, apiUrl) {
_103
this.apiKey = apiKey;
_103
this.apiURL = apiUrl || "https://api.authy.com";
_103
this.user_agent = "PhoneVerificationRegNode/" + VERSION + " (node " + process.version + ")";
_103
this.headers = {};
_103
_103
this.init();
_103
}
_103
_103
PhoneVerification.prototype.init = function () {
_103
this.headers = {
_103
"User-Agent": this.user_agent
_103
};
_103
};
_103
_103
/**
_103
* Verify a phone number
_103
*
_103
* @param {!string} phone_number
_103
* @param {!string} country_code
_103
* @param {!string} token
_103
* @param {!function} callback
_103
*/
_103
PhoneVerification.prototype.verifyPhoneToken = function (phone_number, country_code, token, callback) {
_103
_103
console.log('in verify phone');
_103
this._request("get", "/protected/json/phones/verification/check", {
_103
"api_key": this.apiKey,
_103
"verification_code": token,
_103
"phone_number": phone_number,
_103
"country_code": country_code
_103
},
_103
callback
_103
);
_103
};
_103
_103
/**
_103
* Request a phone verification
_103
*
_103
* @param {!string} phone_number
_103
* @param {!string} country_code
_103
* @param {!string} via
_103
* @param {!function} callback
_103
*/
_103
PhoneVerification.prototype.requestPhoneVerification = function (phone_number, country_code, via, callback) {
_103
_103
this._request("post", "/protected/json/phones/verification/start", {
_103
"api_key": this.apiKey,
_103
"phone_number": phone_number,
_103
"via": via,
_103
"country_code": country_code,
_103
"code_length": 4
_103
},
_103
callback
_103
);
_103
};
_103
_103
PhoneVerification.prototype._request = function (type, path, params, callback, qs) {
_103
qs = qs || {};
_103
qs['api_key'] = this.apiKey;
_103
_103
options = {
_103
url: this.apiURL + path,
_103
form: params,
_103
headers: this.headers,
_103
qs: qs,
_103
json: true,
_103
jar: false,
_103
strictSSL: true
_103
};
_103
_103
console.log(options.url);
_103
_103
var callback_check = function (err, res, body) {
_103
if (!err) {
_103
if (res.statusCode === 200) {
_103
callback(null, body);
_103
} else {
_103
callback(body);
_103
}
_103
} else {
_103
callback(err);
_103
}
_103
};
_103
_103
switch (type) {
_103
case "post":
_103
request.post(options, callback_check);
_103
break;
_103
_103
case "get":
_103
request.get(options, callback_check);
_103
break;
_103
}
_103
};

And with that, your demo app is protected with Twilio Verify! You can now log out to try the other channel.


Your demo app is now keeping hordes of fraudulent users from registering with your business and polluting the database. Next, you should check out all of the variables and options available to you in the Verify API Reference. Also, for protecting your customers in an ongoing manner (with this same codebase) try the Node.js Authy Two-Factor Authentication Quickstart.

After that, take a stroll through the Docs for more Account Security demos and tutorials - as well as sample web applications using all of Twilio's products. Encore!


Rate this page: