5

I am using Twilio SDK for my Postman request. I have stored my credentials in an .env file for now.

exports.send = async (messageBody) => {
  client.messages.create({
    body: messageBody,
    To: twilioTo,
    From: twilioFrom
  }, (err, message) => { 

    message = message + " Goodbye World!";
    if (err) return cb(err);
    cb(null, message);
  });
};

But instead of a normal response I am getting:

{
"code": 500,
"message": "Required parameter \"opts.to\" missing.",
"stack": "Error: Required parameter \"opts.to\" missing.\n    at    Function.create (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/node_modules/twilio/lib/rest/api/v2010/account/message.js:95:13)\n    at Object.exports.send (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/src/api/services/twilioSMSVerficationService.js:14:21)\n    at exports.send (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/src/api/controllers/twilio.controller.js:20:42)\n    at Layer.handle [as handle_request] (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/node_modules/express/lib/router/layer.js:95:5)\n    at next (/Volumes/BOOTCAMP/knowrify/knowrify-node-backend/node_modules/express/lib/router/route.js:137:13)\n    at /Volumes/BOOTCAMP/knowrify/knowrify-node-backend/src/api/middlewares/auth.js:41:10"
}
karel
  • 5,489
  • 46
  • 45
  • 50
Sourabh Shah
  • 140
  • 2
  • 11

1 Answers1

0

Even I was getting the same error until I realized that I'm passing undefined for "to". Attached sample code for reference.

const destinationNumber = req.body.number;
const messageConfig = {
                body: 'OTP : ' + otp,
                from: process.env.TWILIO_NUMBER,
                to: destinationNumber
            };
const message = await client.messages.create(messageConfig);
if (message.sid) {
     context.res = {
      body: "OTP sent successfully"
  };
}