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.
In this Quickstart, you will learn how to:
- Sign up for Twilio
- Set up your development environment to send and receive messages
- Send your first SMS phone verification
- Check verification codes
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.
- When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test verification messages to your phone from your Twilio account while in trial mode. This phone verification step is exactly what you'll learn how to build in this tutorial!
- Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
- Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, create a verification service, and more.
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.
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.
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.
cd verify-v2-quickstart-node yarn install
.env. This is where we'll store sensitive data in environment variables.
cp .env.example .env
Modify your new
.env file with your own
VERIFICATION_SID that you can find in your Twilio Console.
Run the application
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! You can now log out to try the untried channel.
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 Phone Verification API Reference. Also, to protect your customers in an ongoing manner (with this same codebase) try the Node.js Authy Two-Factor Authentication Quickstart.
After that, visit the Docs for more Account Security demos and tutorials and web applications using all of Twilio's products.