Send an OTP
One-time codes on WhatsApp with an authentication template, safely retried.
A login or payment code is a template message: it can go to someone who has not written to you in the last 24
hours. In live mode, create an authentication template in Wapitick (Meta approves it, usually within minutes). In
test mode, the seeded order_update template stands in for it.
- Send with an
Idempotency-Key(the SDKs do it for you): if your request times out and you retry, the customer still gets one code. - Never put the code in
client_refor anything you log. The request log stores message text as[N chars], never the text. - Watch
message.failed:131026means the number is not on WhatsApp, so fall back to SMS.
cURL
#!/usr/bin/env bash
# Send an OTP: channels_list, messages_send (runs in test mode)
set -euo pipefail
API="${WAPITICK_API_URL:-https://api.wapitick.com}"
AUTH="Authorization: Bearer $WAPITICK_API_KEY"
CHANNEL=$(curl -sf "$API/v1/channels" -H "$AUTH" | jq -r '.data[0].id')
CODE=$(( RANDOM % 900000 + 100000 ))
# A template works outside the 24-hour window. In test mode "order_update" is a seeded sample; in live mode use
# your approved AUTHENTICATION template. The Idempotency-Key makes a retried request send at most once.
curl -sf "$API/v1/messages" -H "$AUTH" -H "Content-Type: application/json" -H "Idempotency-Key: otp-$CODE" \
-d "{\"channel_id\":\"$CHANNEL\",\"to\":{\"phone\":\"+15550100002\"},\"type\":\"template\",\"template\":{\"name\":\"order_update\",\"language\":\"en\",\"components\":[{\"type\":\"body\",\"parameters\":[{\"type\":\"text\",\"text\":\"Asha\"},{\"type\":\"text\",\"text\":\"$CODE\"}]}]}}" \
| jq '{id, status}'Node
// biome-ignore-all lint/suspicious/noConsole: a docs sample prints its result
// Send an OTP: channels_list, messages_send
import { randomInt } from "node:crypto";
import { Wapitick } from "@wapitick/node";
const wapitick = new Wapitick({
apiKey: process.env.WAPITICK_API_KEY,
baseUrl: process.env.WAPITICK_API_URL,
});
const code = String(randomInt(100000, 1000000));
// test mode: the seeded "order_update" template; live: your approved AUTHENTICATION template
const msg = await wapitick.messages.sendTemplate({
to: "+15550100002",
name: "order_update",
variables: ["Asha", code],
idempotencyKey: `otp-${code}`,
});
console.log(JSON.stringify({ id: msg.id, status: msg.status }));Python
# Send an OTP: channels_list, messages_send
import json
import os
import secrets
from wapitick import Wapitick
w = Wapitick(api_key=os.environ["WAPITICK_API_KEY"], base_url=os.environ.get("WAPITICK_API_URL", "https://api.wapitick.com"))
code = str(secrets.randbelow(900000) + 100000)
# test mode: the seeded "order_update" template; live: your approved AUTHENTICATION template
msg = w.messages.send_template("+15550100002", "order_update", variables=["Asha", code])
print(json.dumps({"id": msg["id"], "status": msg["status"]}))PHP
<?php
// Send an OTP: channels_list, messages_send
require __DIR__ . '/vendor/autoload.php';
$w = new Wapitick\Client(getenv('WAPITICK_API_KEY'), getenv('WAPITICK_API_URL') ?: 'https://api.wapitick.com');
$code = (string) random_int(100000, 999999);
// test mode: the seeded "order_update" template; live: your approved AUTHENTICATION template
$msg = $w->sendTemplate('+15550100002', 'order_update', 'en', ['Asha', $code]);
echo json_encode(['id' => $msg['id'], 'status' => $msg['status']]), "\n";Google Apps Script
// biome-ignore-all lint/correctness/noUnusedVariables: Apps Script runs these functions by name
// Send an OTP from Google Apps Script: channels_list, messages_send
// Script properties: WAPITICK_API_KEY (wk_test_...), optional WAPITICK_API_URL.
function wapitick_(method, path, body) {
const props = PropertiesService.getScriptProperties();
const base = props.getProperty("WAPITICK_API_URL") || "https://api.wapitick.com";
const res = UrlFetchApp.fetch(base + path, {
method: method,
contentType: "application/json",
headers: {
Authorization: `Bearer ${props.getProperty("WAPITICK_API_KEY")}`,
"Idempotency-Key": Utilities.getUuid(),
},
payload: body ? JSON.stringify(body) : undefined,
muteHttpExceptions: true,
});
const json = JSON.parse(res.getContentText() || "null");
if (res.getResponseCode() >= 300) throw new Error(`${json.code}: ${json.detail}`);
return json;
}
function sendOtp() {
const code = String(Math.floor(100000 + Math.random() * 900000));
const channel = wapitick_("get", "/v1/channels").data[0].id;
const msg = wapitick_("post", "/v1/messages", {
channel_id: channel,
to: { phone: "+15550100002" },
type: "template",
template: {
name: "order_update",
language: "en",
components: [
{
type: "body",
parameters: [
{ type: "text", text: "Asha" },
{ type: "text", text: code },
],
},
],
},
});
Logger.log(JSON.stringify({ id: msg.id, status: msg.status }));
}Operations: channels_list, messages_send.