Sinch Node.js SDK for Verification API

The Sinch Verification API Node.js SDK allows you to quickly interact with the Verification API from inside your Node.js applications. The fastest way to get started with the SDK is to check out our getting started guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK.

Syntax

When using the Node.js SDK, the code representing requests and queries sent to and responses received from the Verification API are structured similarly to those that are sent and received using the Verification API itself.

Note:

This guide describes the syntactical structure of the Node.js SDK for the Verification API, including any differences that may exist between the API itself and the SDK. For a full reference on Verification API calls and responses, see the Verification API Reference.

index.mjs

This code makes an SMS verification request using the Node.js SDK.

const {SinchClient} = require('@sinch/sdk-core');
const {verificationsHelper} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    applicationKey: 'YOUR_application_key',
    applicationSecret: 'YOUR_application_secret'
});

const startRequestData = verificationsHelper.buildStartSmsVerificationRequest(
                                                number);
const smsVerification = await sinchClient.verification.verifications.startSms(
                                                startRequestData);

console.log(JSON.stringify(smsVerification));

The code sample on the side of this page is an example of how to use the Node.js SDK to initiate an Verification SMS PIN request. The code is also displayed below, along with a Verification API call that accomplishes the same task, for reference:

REST APISDK
Copy
Copied
const axios = require('axios');

const APPLICATION_KEY = "<REPLACE_WITH_APP_KEY>";
const APPLICATION_SECRET = "<REPLACE_WITH_APP_SECRET>";
const TO_NUMBER = "<REPLACE_WITH_YOUR_NUMBER>";

const SINCH_URL = "https://verification.api.sinch.com/verification/v1/verifications";

const basicAuthentication = APPLICATION_KEY + ":" + APPLICATION_SECRET;

const payload = {
    identity: {
        type: 'number',
        endpoint: TO_NUMBER
    },
    method: 'sms'
};

const headers = {
    'Authorization': 'Basic ' + Buffer.from(basicAuthentication).toString('base64'),
    'Content-Type': 'application/json; charset=utf-8'
};

axios.post(SINCH_URL, payload, { headers })
    .then(response =>
        console.log(response.data)
    ).catch(error =>
    console.error('There was an error!', error)
);
Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');
const {verificationsHelper} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    applicationKey: 'YOUR_application_key',
    applicationSecret: 'YOUR_application_secret'
});
const startRequestData = verificationsHelper.buildStartSmsVerificationRequest(
                                                        number);
const smsVerification = await sinchClient.verification.verifications.startSms(
                                                        startRequestData);

console.log(JSON.stringify(smsVerification));

This example highlights the following required to successfully make a Verification API call using the Sinch Node.js SDK:

Client

When using the Sinch Node.js SDK, you initialize communication with the Sinch backend by initializing the Node.js SDK's main client class. This client allows you to access the functionality of the Sinch Node.js SDK.

Initialization

To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.

Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    applicationKey: "YOUR_application_key",
    applicationSecret: "YOUR_application_secret"
});
Note:

For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example:

.env File

Copy
Copied
APPKEY="YOUR_application_key"
APPSECRET="YOUR_application_secret" 

app.js File

Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    applicationKey: process.env.APPKEY,
    applicationSecret: process.env.APPSECRET
});
Note:

If you are using the Node.js SDK for multiple products that use different sets of authentication credentials, you can include all of the relevant credentials in the same configuration object, as in the following example:

Copy
Copied
const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret",
    applicationKey: "YOUR_application_key",
    applicationSecret: "YOUR_application_secret"
});

Verification domain

The Sinch Node.js SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example, sinch.verification.[endpoint_category].[method()].

Endpoint categories

In the Sinch Node.js SDK, Verification API endpoints are accessible through the client. The naming convention of the endpoint's representation in the SDK matches the API:

  • verification.verifications
  • verification.verificationStatus

For example:

Copy
Copied
const startRequestData = verificationsHelper.buildStartSmsVerificationRequest(
                                                      "YOUR_phone_number");
const smsVerification = await sinchClient.verification.verifications.startSms(
                                                      startRequestData);

verification.verifications endpoint category

The verification.verifications category of the Node.js SDK corresponds to the verifications endpoint. The mapping between the API operations and corresponding Node.js methods are described below:
API operationSDK method
Start an SMS PIN verification requeststartSms()
Start a FlashCall verification requeststartFlashCall()
Start a Phone Call verification requeststartCallout()
Start a Data verification requeststartSeamless()
Report an SMS verification PIN code by the identity of the recipientreportSmsByIdentity()
Report a FlashCall verification code (CLI) by the identity of the recipientreportFlashCallByIdentity()
Report a Phone Call verification code by the identity of the recipientreportByCalloutIdentity()
Report an SMS verification PIN code by the ID of the verification requestreportSmsById()
Report a FlashCall verification code (CLI) by the ID of the verification requestreportFlashCallById()
Report a Phone Call verification code by the ID of the verification requestreportCalloutById()

verification.verificationStatus endpoint category

The verification.verificationStatus category of the Node.js SDK corresponds to the verifications endpoint. The mapping between the API operations and corresponding Node.js methods are described below:
API operationSDK method
Get the status of a verification by the ID of the verification requestgetById()
Get the status of a verification by the identity of the recipientgetByIdentity()
Get the status of a verification by a reference valuegetByReference()

Helper methods

The Node.js SDK has a number of helper methods designed to aid in quickly constructing the correct request bodies for the various methods. The helper methods available and their parameters are described in the example and table below.

Copy
Copied
const requestData = verificationHelper.buildStartSmsVerificationRequest(
                                          "YOUR_phone_number");
Request bodyHelper method nameParameters
Start SMS VerificationbuildStartSmsVerificationRequest()
  • phoneNumber
    *required
  • reference
Start FlashCall VerificationbuildStartFlashCallVerificationRequest()
  • phoneNumber
    *required
  • reference
  • dialTimeout
Start Callout VerificationbuildStartCalloutVerificationRequest()
  • phoneNumber
    *required
  • reference
Start Data VerificationbuildStartSeamlessVerificationRequest()
  • phoneNumber
    *required
  • reference
Report SMS Verification by IDbuildReportSmsVerificationByIdRequest()
  • id
    *required
  • code
    *required
  • cli
Report FlashCall Verification by IDbuildReportFlashCallVerificationByIdRequest()
  • id
    *required
  • cli
    *required
Report Phone Call Verification by IDbuildReportCalloutVerificationByIdRequest()
  • id
    *required
  • code
    *required
Report SMS Verification by IdentitybuildReportSmsVerificationByIdentityRequest()
  • identity
    *required
  • code
    *required
  • cli
Report FlashCall Verification by IdentitybuildReportFlashCallVerificationByIdentityRequest()
  • identity
    *required
  • cli
    *required
Report Phone Call Verification by IdentitybuildReportCalloutVerificationByIdentityRequest()
  • identity
    *required
  • code
    *required

Request and query parameters

Requests and queries made using the Node.js SDK are similar to those made using the Verification API. Path parameters, request body parameters, and query parameters that are used in the API are all passed as arguments to the corresponding Node.js method.

For example, consider this example in which the getByIdentity() method of the verification.verificationStatus class is invoked:
SDKREST API
Copy
Copied
const response = await sinchClient.verification.verificationStatus.getByIdentity({
    endpoint: 'PHONE_NUMBER',
    method: 'sms'
});
Copy
Copied
url = "https://verification.api.sinch.com/verification/v1/verifications/" + method + "/number/" + endpoint
When using the Verification API, method and endpoint would be included as path parameters in the request. With the Node.js SDK, the method and endpoint parameters are used in an object passed as an argument in the getByIdentity() method.

Responses

Response fields match the API responses. They are delivered as Javascript objects.

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

index.mjs

This code makes an SMS verification request using the Node.js SDK.

const {SinchClient} = require('@sinch/sdk-core');
const {verificationsHelper} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    applicationKey: 'YOUR_application_key',
    applicationSecret: 'YOUR_application_secret'
});

const startRequestData = verificationsHelper.buildStartSmsVerificationRequest(
                                                number);
const smsVerification = await sinchClient.verification.verifications.startSms(
                                                startRequestData);

console.log(JSON.stringify(smsVerification));