Report an SMS PIN verification code with Node.js
Now that you've initiated a verification request and received a one time PIN code, it's time to report that code in order to verify the identity of the user.
In this guide you will learn:
What you need to know before you start
Before you can get started, you need the following already set up:
- Set all Verification API configuration settings.
- NPM and a familiarity with how to install packages.
- Node.js and a familiarity with how to create a new app.
- A mobile handset that can receive SMS messages.
Set up your Node.js application
First we'll create a Node project using npm. This creates a package.json and the core dependencies necessary to start coding.
To create the project, do the following steps:
- Create a folder called
pin-verification-report
- Navigate into the folder you created and run the following command.This command adds the node_modules folder and the package.json file. You will be prompted to provide values for the fields. For this tutorial, you can simply accept the default values and press enter at each stage.
npm init
Install your dependencies
We will be using the axios package to make HTTP requests.
Use the following command to install the axios package:
npm install axios
Create your file
In your project folder, create a new file named index.js in the project and paste the provided "index.js" code into the file.
Note:
This tutorial uses basic authentication for testing purposes. We recommend using a signed request for authentication in a production environment. You can follow the steps in this guide, but use the code samples from here to use request signing authentication instead.
toNumber
parameter to verify that a code sent to that number is valid.Fill in your parameters
Before you can run the code, you need to update some values so you can connect to your Sinch account. Update the following parameters with your own values:
Parameter | Your value |
---|---|
APPLICATION_KEY | The application key found on your Sinch dashboard. |
APPLICATION_SECRET | The application secret found on your Sinch dashboard. |
TO_NUMBER | This should be the number of the mobile handset you are using for this guide. |
CODE | This is the code which was received by the mobile handset in the initiate verification guide. These codes are only valid for 10 minutes, so ensure you have initiated a verification within the last ten minutes so you can successfully match the code! |
Save the file.
Report your verification request
Now you can execute the code and report your verification request. Run the following command:
node index.js
If the code was correct, your console will show a success result in the response from Sinch. You can test this by entering an incorrect code and seeing the failed result response.
Troubleshooting tip
If after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.