SendOtp-Promise - Node.js SDK
This SDK is the promise wrapper for SentOtp by MSG91
Set-up:
- Download the NPM module
// using npmnpm install sendotp -S // using yarnyarn add sendotp-promise
- Require the package in your code.
const SendOTP = ; / ;
- Initialize with your MSG91 auth key
const sendOtp = 'AuthKey';
That's all, your SDK is set up!
Requests
You now have the send, retry and verify otp via following methods.
sendOtp; //otp is optional if not sent it'll be generated automaticallysendOtp;sendOtp;
Usage:
To send OTP, without optional parameters
// normal callbacksendOtp;
Using async-await
// ES6 import ; // using commonJS pattern// const SendOTP = require('sendotp-promise'); const MSG91_AUTH_KEY = 'your auth key here';const MSG91_SENDER_ID = 'your sender id of 6 characters'; // new instance of SendOTPconst sendOtp = MSG91_AUTH_KEY; // set the expiry for your OTPsendOtp; const sendOtpToMobile = async { try // call the send() method const response = await sendOtp; console; if responsetype === 'success' return console; return console; catch err console; return console; }; // the mobile numberconst countryCode = 'your country code';const mobileNumber = 'any mobile number';const completeMobileNumber = ``; // call ;
To send OTP, with optional parameters
sendOtp;
If you want to set custom expiry of OTP verification
sendOtp; //in minutes
To retry OTP
sendOtp;
Note: In sendOtp.retry() set retryVoice false if you want to retry otp via text, default value is true
To verify OTP
sendOtp;
Options:
By default sendotp uses default message template, but custom message template can also set in constructor like
const SendOtp = ;const sendOtp = 'AuthKey' 'Otp for your order is {{otp}}, please do not share it with anybody';
{{otp}}
expression is used to inject generated otp in message.