How to integrate msg91 api with node

1.1k views Asked by At

I am trying mobile verification with msg91 ..the code run succesfully but msg notsending messagee...it gives the success message ... my code:

router.post('/mobile', async (req, res) =\> {
try {
const sendOtp = new SendOtp(process.env.AUTHKEY);
const mobile = await usermodel.findOne({ mobile: req.body.mobile })
if (!mobile) return res.status(403).send('user not found..')

    const otp =`${ Math.floor(1000 + Math.random() * 9000)} `
    console.log(otp);
    
    const  phonenumber  = req.body.mobile
    const newPhoneNumber = '+91' + phonenumber
    // console.log(newPhoneNumber);
    
    sendOtp.send(newPhoneNumber, otp, function (error, data) {
      console.log(data);
      console.log(error);
    });
    res.status(201).send({ status: 'TRUE', message: 'OTP SEND' })

} catch (error) {
res.status(200).send({ status: 'failed', message: 'Unable to Send OTP', error })
console.log(error)
}
})

responce: { message: '326b6774704a343230323437', type: 'success' } null

1

There are 1 answers

1
Ankit Kumar On
import msg91 from "msg91";
/**
*OR
*const msg91 = require('msg91').default;
*/

msg91.initialize({authKey: "Your-Auth-Key"});
let sms = msg91.getSMS();

// Send SMS
sms.send("flowId",'mobile':"MOBILE_NUMBER_WITH_COUNTRY_CODE","VAR1":"123"});

Use official MSG91 npm package