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.
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:
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)
);
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.
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
APPKEY="YOUR_application_key"
APPSECRET="YOUR_application_secret"
app.js
File
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:
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:
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 operation | SDK method |
---|---|
Start an SMS PIN verification request | startSms() |
Start a FlashCall verification request | startFlashCall() |
Start a Phone Call verification request | startCallout() |
Start a Data verification request | startSeamless() |
Report an SMS verification PIN code by the identity of the recipient | reportSmsByIdentity() |
Report a FlashCall verification code (CLI) by the identity of the recipient | reportFlashCallByIdentity() |
Report a Phone Call verification code by the identity of the recipient | reportByCalloutIdentity() |
Report an SMS verification PIN code by the ID of the verification request | reportSmsById() |
Report a FlashCall verification code (CLI) by the ID of the verification request | reportFlashCallById() |
Report a Phone Call verification code by the ID of the verification request | reportCalloutById() |
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 operation | SDK method |
---|---|
Get the status of a verification by the ID of the verification request | getById() |
Get the status of a verification by the identity of the recipient | getByIdentity() |
Get the status of a verification by a reference value | getByReference() |
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.
const requestData = verificationHelper.buildStartSmsVerificationRequest(
"YOUR_phone_number");
Request body | Helper method name | Parameters |
---|---|---|
Start SMS Verification | buildStartSmsVerificationRequest() |
|
Start FlashCall Verification | buildStartFlashCallVerificationRequest() |
|
Start Callout Verification | buildStartCalloutVerificationRequest() |
|
Start Data Verification | buildStartSeamlessVerificationRequest() |
|
Report SMS Verification by ID | buildReportSmsVerificationByIdRequest() |
|
Report FlashCall Verification by ID | buildReportFlashCallVerificationByIdRequest() |
|
Report Phone Call Verification by ID | buildReportCalloutVerificationByIdRequest() |
|
Report SMS Verification by Identity | buildReportSmsVerificationByIdentityRequest() |
|
Report FlashCall Verification by Identity | buildReportFlashCallVerificationByIdentityRequest() |
|
Report Phone Call Verification by Identity | buildReportCalloutVerificationByIdentityRequest() |
|
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 thegetByIdentity()
method of the verification.verificationStatus
class is invoked:const response = await sinchClient.verification.verificationStatus.getByIdentity({
endpoint: 'PHONE_NUMBER',
method: 'sms'
});
url = "https://verification.api.sinch.com/verification/v1/verifications/" + method + "/number/" + endpoint
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.