Make an SMS PIN verification request with Node.js

You can quickly see how the Verification API works by sending yourself an SMS PIN verification request and responding to it.

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:

  1. Create a folder called “pin-verification-init”
  2. Navigate into the folder you created and run the following command.
    Copy
    Copied
    npm init
    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.

Install your dependencies

We will be using the axios package to make HTTP requests.

Use the following command to install the axios package:

Copy
Copied
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.

This code makes a POST request to the Verification API /verifications endpoint which sends an SMS PIN verification request out to the TO_NUMBER parameter in the body of the request.

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:

ParameterYour value
APPLICATION_KEYThe application key found on your Sinch dashboard.
APPLICATION_SECRETThe application secret found on your Sinch dashboard.
TO_NUMBERThis should be the number of the mobile handset you are using for this guide.

Save the file.

Initiate your verification request

Now you can execute the code and initiate your verification request. Run the following command:

Copy
Copied
node index.js

You should receive a text message to your mobile handset with a verification code. In a production scenario, this is the code that a user would enter into your app to verify their account.

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.

Next steps

Now it's time to take the one time PIN code you just received and use it to verify the identity of the user by reporting the PIN code.

Additional resources

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.