node.jstwiliotwilio-apitwilio-programmable-chat

twilio isn't sending sms nodejs


I use twilio to send sms after a order is placed, but it isn't sending any sms and also not console logging anything.

Code: npm i twilio

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require("twilio")(accountSid, authToken);

exports.createOrder = (req, res) => {
  const { orders, status, details } = req.body;

  const order = new Order({
    orders: orders,
    status: status,
    details: details,
  });

  order.save((error, order) => {
    if (error) return res.status(400).json(error);
    if (order) {
      // if (res.status === 201) {
      client.messages
        .create({
          body: "This is the ship that made the Kessel Run in fourteen parsecs?",
          from: "+xxxxxxxxx",
          to: "+xxxxxxxxxx",
        })
        .then((message) => console.log(message.sid));
      // }

      return res.status(201).json({ order });
    }
  });
};

LINK TO DOC (where I took the code) : https://www.twilio.com/docs/sms/quickstart/node


Solution

  • Twilio developer evangelist here.

    There is likely an issue in sending your message, but the code you have is dropping errors, so you can't see what is going on. Try updating it to this:

          client.messages
            .create({
              body: "This is the ship that made the Kessel Run in fourteen parsecs?",
              from: "+xxxxxxxxx",
              to: "+xxxxxxxxxx",
            })
            .then((message) => console.log(message.sid))
            .catch(error => console.error(error));
    

    Once you see what the error is, you will be able to fix it. My guess is that you have either configured your Account Sid and Auth Token incorrectly, or you are using a trial account and trying to send a message to a number you haven't verified yet, or you are trying to send a message to a country that isn't enabled.