Sending a verification code over WhatsApp is often more reliable and cheaper than an SMS. The code goes in an otp message, in the format Meta requires: a Copy code button appears in the conversation.
Send the code#
Terminal
curl -X POST https://hira.localtechstudio.com/api/v1/messages \
-H "Authorization: Bearer pk_live_..." \
-H "Content-Type: application/json" \
-d '{"connectionId":"con_...","to":"+237600000000","otp":{"code":"482913","language":"en"}}'
Generate the code on the server with a safe random source, and keep only its hash and an expiry time.
PHP
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
$stored = ['hash' => password_hash($code, PASSWORD_DEFAULT), 'expires' => time() + 600, 'tries' => 0];JavaScript
import { randomInt } from 'node:crypto';
const code = String(randomInt(0, 1_000_000)).padStart(6, '0');The rules that matter#
- Short expiry, ten minutes at most.
- Five tries at most per code, then ask for a new one.
- A delay between two requests for the same number, so it cannot be used to harass someone.
- A code is never reused.
If the message does not arrive#
Follow the status through a webhook. A failed with 131026 means the number is not on WhatsApp: offer another channel (SMS, email).