node.jstwilioone-time-password

[RestException [Error]: The 'To' number 9977428080 is not a valid phone number


I am trying to send sms to number using Twilio API's, but I am getting error like [RestException [Error]: The 'To' number 9977428080 is not a valid phone number.] { status: 400, message: "The 'To' number 9977428080 is not a valid phone number.", code: 21211, moreInfo: 'https://www.twilio.com/docs/errors/21211', detail: undefined }

before this I was getting the from number is not valid then I switched my API's credentials testing to Live credentials, the I got this another issue. These are the code below

const client = require('twilio')(config.SId, config.AccesToekn);
// SEND OTP TO USER ACCOUNT
router.post('/sendsms',(req, res, next)=>{
client.messages.create({
     from:'+15204770942',
     to:"9977428080",
     body:'Please user this OPT to verify your number 74536',
 }, function(error, data){
     if(error){
   console.log(error,'error')
    console.log(data,"data")
     }
 })
})

and My credential are correct. What might be cause of occurring this issue, any your help would be highly appreciated

Thanks in Advance


Solution

  • error.moreInfo: 'https://www.twilio.com/docs/errors/21211'

    Twilio accepts phone numbers in E164 format: [+] [country code] [subscriber number including area code]

    Further on the format:

    What is E.164?

    Example: +14155552671

    change to:

    const client = require('twilio')(config.SId, config.AccesToekn);
    // SEND OTP TO USER ACCOUNT
    router.post('/sendsms',(req, res, next)=>{
    client.messages.create({
         from:'+15204770942', 
         to:"+9199778080", //this must be a verified phone number for twilio trial accounts
         body:'Please user this OPT to verify your number 74536',
     }, function(error, data){
         if(error){
       console.log(error,'error')
        console.log(data,"data")
         }
     })
    })