With just a few lines of code, your Node.js/Express application can verify phone numbers and add an additional layer of security with Twilio Verify.
This Node.js Verify Quickstart will teach you how to do this using our Verify REST API, the Twilio Node.js helper library, and Express.js to ease development.
In this Quickstart, you will learn how to:
Short on time? Spin up a low-code, fully editable verification demo in less than 2 minutes using Twilio's Code Exchange and Quick Deploy here.
If you already have a Twilio account, you're all set here! Feel free to jump to the next step.
Before you can send an SMS from Node.js, you'll need to sign up for a Twilio account or sign into your existing account.
You can sign up for a free Twilio trial account here.
If you've sent SMS with Twilio in the past, you might remember needing to buy a phone number. With Twilio Verify, we take care of that for you! The Verify API selects the best routes for quickly and reliably delivering verification codes globally.
Verify uses Services for configuration. To send a Verify API request you will need both your Twilio Credentials and a Service SID. You can create and update a Service in two ways:
Services can be used to edit the name (which shows up in the message template), set the code length (4-10 characters), enable settings like the "do not share warning" and more.
Now that you have a Twilio account and a verification service, you can start writing some code!
To make things even easier, we'll next install Twilio's official helper library for Node.js applications.
If you've gone through one of our other Node.js Quickstarts already and have Node.js and the Twilio Node.js helper library installed, you can skip this step and get straight to sending your first verification.
To start a phone verification, you'll need to have Node.js and the Twilio Node.js helper library installed.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createVerification() {11const verification = await client.verify.v212.services("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.verifications.create({14channel: "sms",15to: "+15017122661",16});1718console.log(verification.status);19}2021createVerification();
1{2"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",5"to": "+15017122661",6"channel": "sms",7"status": "pending",8"valid": false,9"date_created": "2015-07-30T20:00:00Z",10"date_updated": "2015-07-30T20:00:00Z",11"lookup": {},12"amount": null,13"payee": null,14"send_code_attempts": [15{16"time": "2015-07-30T20:00:00Z",17"channel": "SMS",18"attempt_sid": "VLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"19}20],21"sna": null,22"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications/VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"23}
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createVerificationCheck() {11const verificationCheck = await client.verify.v212.services("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.verificationChecks.create({14code: "123456",15to: "+15017122661",16});1718console.log(verificationCheck.status);19}2021createVerificationCheck();
1{2"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",5"to": "+15017122661",6"channel": "sms",7"status": "approved",8"valid": true,9"amount": null,10"payee": null,11"sna_attempts_error_codes": [],12"date_created": "2015-07-30T20:00:00Z",13"date_updated": "2015-07-30T20:00:00Z"14}
Next we'll get this up and running in a full example project.
Start by cloning our Node.js repository.
git clone https://github.com/TwilioDevEd/verify-v2-quickstart-node.git
If you don't have git installed or prefer to download the source code you can grab a zip file of the project here.
1cd verify-v2-quickstart-node23yarn install
Copy .env.example
to .env
. This is where we'll store sensitive data in environment variables.
cp .env.example .env
Modify your new .env
file with your own TWILIO_ACCOUNT_SID
, TWILIO_AUTH_TOKEN
, and VERIFICATION_SID
that you can find in your Twilio Console.
Run the application
yarn start
If your credentials are set up correctly, you'll soon get a message that the app is up!
Navigate to http://localhost:3000/register. You should see a registration form that looks like this:
Enter your phone number and choose which channel to request verification over. Finally hit the green Sign Up button and wait. You'll either receive a phone call or an SMS with the verification token. If you requested a phone call, you may need to interact to proceed (entering a number on the phone keypad) as an additional security feature.
Enter the token into the Verification entry form and click Verify:
And with that, your demo app is protected with Twilio's Phone Verification!
Your demo app is now keeping fraudulent users from registering with your business and polluting your database. Next, check out all of the variables and options available to you in the Verify API Reference.
After that, check out adding additional verification channels supported by the Verify API like:
Lastly, to protect your service against fraud, view our guidance on Preventing Toll Fraud when using Verify.