twiliowhatsappverification

How can I send a WhatsApp verification to users by Custom WhatsApp API Integration?


I am working on implementing a custom WhatsApp API integration to send verification codes to users on WhatsApp. I have already been approved for access to the WhatsApp Business API and obtained the necessary credentials.

Now, I want to send verification codes to users through WhatsApp using my custom integration. But I don't have any idea that how to do that?

I just wanna know How do I generate a random verification code in my application without getting any bugs?

And also I wanna know Which WhatsApp API endpoints should I use to send the verification code as a text message?

I was expecting a the most suitable way to do these things without getting any bugs


Solution

  • The easiest way to do this is by using Twilio Verify. The advantage here is that you don't need to generate and manage your own codes and can even use the one of Twilio's numbers:

    Step 1: Create a verify service

    Step 2: Send a code to a number

    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    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: 'whatsapp'})
                    .then(verification => console.log(verification.accountSid));
    

    Step 3: Verify the code the user entered

    client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
                    .verificationChecks
                    .create({to: '+15017122661', code: '123456'})
                    .then(check => console.log(check.valid));