# Send a verification code

A one-time code over WhatsApp.

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

```bash
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 title="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];
```
```js title="Node.js"
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.

> **Warning**

> The message goes out under the *authentication* category: put nothing but the code in it. Meta refuses or requalifies messages that mix a code with a promotion.

## If the message does not arrive

Follow the status through a [webhook](/en/docs/webhooks). A `failed` with [131026](/en/docs/errors/131026) means the number is not on WhatsApp: offer another channel (SMS, email).
