apitwiliotwilio-api

How to fix Parameter missing error in Twilio OTP API?


I am trying to Send OPT using Twilio API with AIOX but I am getting missing parameter error in response. I tried with same with PHP but getting same error. Is any help help me on this?

Please check my below code:

import axios from 'axios';

const accountSid = 'your_account_sid';
const authToken = 'your_auth_token';
const serviceSid = 'your_verify_service_sid';

export default {
  methods: {
    async generateSmsOtp(phoneNumber) {
      const response = await axios.post(`https://verify.twilio.com/v2/Services/${serviceSid}/Verifications`, {
        To: phoneNumber,
        Channel: 'sms',
      }, {
        auth: {
          username: accountSid,
          password: authToken,
        },
      });

      return response.data.sid;
    },
  },
};

Getting Below Error:

{
  "code": 60200,
  "message": "Invalid parameter",
  "more_info": "https://www.twilio.com/docs/errors/60200",
  "status": 400
}

Solution

  • The problem is that you need to URL-encode the payload and cannot just send them as you did. You can use qs to fix this:

    import axios from 'axios';
    import qs from 'qs';
    
    const accountSid = 'your_account_sid';
    const authToken = 'your_auth_token';
    const serviceSid = 'your_verify_service_sid';
    
    export default {
      methods: {
        async generateSmsOtp(phoneNumber) {
          const response = await axios.post(`https://verify.twilio.com/v2/Services/${serviceSid}/Verifications`, qs.stringify({
            To: phoneNumber,
            Channel: 'sms',
          }), {
            auth: {
              username: accountSid,
              password: authToken,
            },
          });
    
          return response.data.sid;
        },
      },
    };
    

    But I'd recommend using the Twilio client instead of axios and qs. With that, the code should look as follows:

    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = require('twilio')(accountSid, authToken);
    
    client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
                    .verifications
                    .create({to: '+15017122661', channel: 'sms'})
                    .then(verification => console.log(verification.status));
    

    PS: You can find more examples in the docs.