Use cases
Back-in-stock and waitlist alerts over WhatsApp
Keep a waitlist table of people who opted in to a WhatsApp back-in-stock message. On each restock, insert one notification row per waiting person, keyed by the restock id so a repeat does nothing. A timer claims each row at send time and sends it through HelloHop. Once the send is accepted, the person leaves the list.
Facts (as of October 2026, API v1)
- Each alert is one POST
/v1/sessions/{channel}/messagescall. HelloHop does not know your stock, so your inventory system calls your code on each restock. - The unified POST
/v1/sessions/{channel}/messagesroute accepts an Idempotency-Key of up to 255 characters. Repeating the same key and body within 24 hours returns the first result without another send, with the response header Idempotent-Replayed: true. - A 504 send_unconfirmed, a timeout, a 409 idempotency_uncertain or a 503 with ambiguous true means the message may have gone out. Treat it as possibly delivered and look at the chat before you send it again.
- The free sandbox channel allows 180 messages sent a day and 10 new chats a month, counted in Bangkok time. That is enough to build and test the whole flow before you pay.
- Prices: from $20 USD per number per month, billed annually, with a free sandbox and no per-message fees.
Steps
- Collect opt-in. Add a checkbox next to the notify me option, and save the phone number with country code and the answer.
- On restock, create the rows. Insert one notification per waiting, opted-in person, with a key built from the restock id. A repeat of the same restock changes nothing.
- Claim at send time. A timer claims each row while the person is still opted in and waiting.
- Send once and remove. Send with the row key as the Idempotency-Key and record the outcome. A sent, queued or unknown outcome takes the person off the list. A failed send or one waiting to retry leaves them on it.
The job: sell the restock
People on a waitlist asked to hear about one product. One message when it returns, with the link, does the job. The sample sends once per restock and takes the person off the waitlist as soon as the send is accepted (sent or queued) or its outcome is unknown. It does not wait for a delivery receipt.
Restock and notify (Node.js)
Install the SQLite trigger before accepting restocks: it records the outcome and removes the person in the same commit. Recovery treats abandoned claims as unknown and reconciles older terminal rows before another restock. Other databases need the equivalent transaction. The restock id comes from your inventory system, so a repeated event creates no new notification. If stock is limited, notify in waitlist order and stop when stock runs out: that rule is yours to add. The text is an example.
const API = 'https://api.hellohop.app/v1';
const { CHANNEL_ID, CHANNEL_TOKEN } = process.env;
// One send. Repeating the same key with the same body returns the first result.
async function sendWhatsApp(key, message) {
try {
const res = await fetch(`${API}/sessions/${CHANNEL_ID}/messages`, {
method: 'POST',
headers: {
Authorization: `Bearer ${CHANNEL_TOKEN}`,
'Content-Type': 'application/json',
'Idempotency-Key': key,
},
body: JSON.stringify(message),
signal: AbortSignal.timeout(15000),
});
const data = await res.json().catch(() => ({}));
const result = { status: res.status, code: data.code, ambiguous: data.ambiguous, id: data.id };
if (classify(result) === 'retry') {
// 429 whatsapp_rate_limited carries no Retry-After, so it falls back to 1 second. This helper waits at most 30 seconds;
// a longer Retry-After is not slept through: result.retryAt (Unix ms) says when the row is due, and your sweep or timer retries then.
const h = res.headers.get('Retry-After');
const ms = Math.max(0, (h ? (/^\d+$/.test(h) ? Number(h) * 1000 : Date.parse(h) - Date.now()) : 1000) || 1000);
result.retryAt = Date.now() + ms;
await new Promise((resolve) => setTimeout(resolve, Math.min(30000, ms)));
}
return result;
} catch {
return { status: 0 }; // timeout or lost connection: the message may have gone out
}
}
// One answer per send: sent (server_ack), queued, retry, unknown or failed.
function classify({ status, code, ambiguous }) {
if (status === 200) return 'sent'; // server_ack, not delivered
if (status === 202) return 'queued'; // observe webhooks, do not resend
// Refused before sending: repeat the same key and body later.
const refused = ['upstream_unavailable', 'hopsmart_settings_unavailable', 'operator_unresolved'];
if (status === 429 || (status === 503 && (ambiguous === false || refused.includes(code)))) return 'retry';
// Possibly sent: look at the chat, never resend.
if (status === 0 || status >= 500 || code === 'idempotency_uncertain') return 'unknown';
return 'failed'; // an unlinked channel, idempotency_conflict, a bad request: fix it, do not retry
}
// Your tables (SQLite examples: db.run returns { changes }, db.all returns rows, db.get one row). Times are Unix ms.
// waitlist: id, product_id, phone (digits with country code), opted_in (1 = agreed to a back-in-stock message), active (1 = still waiting)
// notifications: key (unique), waitlist_id, text, state, claimed_at, message_id
// resolvePhone(chatJid) looks up the stored phone, including its LID alias; never guess a LID.
// Install this SQLite trigger before accepting restocks. Outcome and removal commit in one statement.
await db.run("CREATE TRIGGER IF NOT EXISTS notification_leaves_waitlist AFTER UPDATE OF state ON notifications " +
"WHEN NEW.state IN ('sent', 'queued', 'unknown') BEGIN UPDATE waitlist SET active = 0 WHERE id = NEW.waitlist_id; END", []);
async function recoverNotifications() {
await db.run("UPDATE notifications SET state = 'unknown' WHERE state = 'claimed' AND (claimed_at IS NULL OR claimed_at < ?)", [Date.now() - 5 * 60 * 1000]);
// Also reconcile terminal rows saved before the trigger was installed.
await db.run("UPDATE waitlist SET active = 0 WHERE EXISTS (SELECT 1 FROM notifications n WHERE n.waitlist_id = waitlist.id AND n.state IN ('sent', 'queued', 'unknown'))", []);
}
// Call once per restock with an id your inventory system gives that restock. A repeat call changes nothing.
async function onRestock(productId, restockId, productName, productUrl) {
await recoverNotifications();
const text = `Good news: ${productName} is back in stock. ${productUrl} We message you once per restock. Reply STOP to leave the waitlist.`;
await db.run(
"INSERT INTO notifications (key, waitlist_id, text, state) SELECT ? || '-' || w.id, w.id, ?, 'pending' " +
"FROM waitlist w WHERE w.product_id = ? AND w.opted_in = 1 AND w.active = 1 ON CONFLICT(key) DO NOTHING",
[`restock-${restockId}`, text, productId]);
}
// Run every minute.
async function runNotifications() {
await recoverNotifications();
// Someone who left the list since the restock gets nothing.
await db.run("UPDATE notifications SET state = 'skipped' WHERE state = 'pending' AND NOT EXISTS (SELECT 1 FROM waitlist w WHERE w.id = notifications.waitlist_id AND w.opted_in = 1 AND w.active = 1)", []);
const due = await db.all('SELECT n.key, n.text, n.waitlist_id, w.phone FROM notifications n JOIN waitlist w ON w.id = n.waitlist_id ' +
"WHERE n.state = 'pending' ORDER BY n.key LIMIT 200", []);
for (const d of due) {
// Claim at send time, while the person is still opted in and waiting.
const claim = await db.run("UPDATE notifications SET state = 'claimed', claimed_at = ? WHERE key = ? AND state = 'pending' " +
"AND EXISTS (SELECT 1 FROM waitlist w WHERE w.id = notifications.waitlist_id AND w.opted_in = 1 AND w.active = 1)", [Date.now(), d.key]);
if (claim.changes !== 1) continue;
const result = await sendWhatsApp(d.key, { type: 'text', to: d.phone, text: d.text });
const outcome = classify(result);
await db.run("UPDATE notifications SET state = ?, message_id = ? WHERE key = ? AND state = 'claimed'", [outcome === 'retry' ? 'pending' : outcome, result.id ?? null, d.key]);
// The trigger removes sent, queued and unknown recipients in this same commit.
if (outcome === 'retry') break; // the helper waited at most 30 seconds; the next run resumes with the same key
}
}
setInterval(() => runNotifications().catch(console.error), 60 * 1000);
// Call this from a receiver that verifies the signature, for a message event that is only STOP.
async function leaveWaitlist(chatJid) {
const phone = await resolvePhone(chatJid);
if (!phone) throw new Error('Resolve this sender before acknowledging STOP');
await db.run('UPDATE waitlist SET opted_in = 0 WHERE phone = ?', [phone]);
}What the timer does with each send result
| Send result | What your handler does |
|---|---|
| 200 server_ack | WhatsApp acknowledged the send. Mark it sent and save id; this is not a delivery receipt. |
| 202 queued | Persisted in the queue, not delivered. A channel that queues its sends (for example while accepted sends drain) answers every text send this way, with id, status queued, position and expires_at. Mark it queued and save id. Observe the result through webhooks without resending. |
| 429, or 503 with ambiguous false or code upstream_unavailable, hopsmart_settings_unavailable or operator_unresolved | Refused before sending. Honor Retry-After, keep the same key and body, and retry later. The Node helper waits at most 30 seconds before returning retry with a retryAt time, and the pages that keep a due time skip the row until then; the other Node timer pages retry on their next run. The Flask, Python reminder and PHP samples return the time to retry instead of waiting; the Sheets helper saves next_attempt_at and skips the row until then. |
| 504 send_unconfirmed, 409 idempotency_uncertain, 503 with ambiguous true, a timeout or a lost response | Possibly delivered. Mark it unknown and look at the chat before any resend. Never force a resend of an uncertain key with a new key. |
| 409 idempotency_conflict | The same key was used for a different message. Fix how you build the key. The first message is unchanged. |
| Any other 409 | Mark it failed. Fix the channel before scheduling another attempt. |
| Other 4xx | The request is wrong and a retry will not fix it. Log it and move on. |
Subscribe to message and message.status webhooks and verify their signatures. The result of a queued send arrives as a message event: match the saved response id to payload.id. message.status reports delivered or read for each id in payload.ids. A send held by HopSmart™ expires 72 hours after acceptance, and the 202 carries expires_at. If no event arrives, read the job with GET /hopsmart/jobs/{id}. Save event ids to deduplicate. Do not resend queued work while waiting for a result.
Leaving the list
The message tells people to reply STOP. Subscribe a receiver to message events and call leaveWaitlist, as the review request guide does for its opt-out.
Questions
- How do I send an alert only once per restock?
- Build the notification key from the restock id and the waitlist id, with a unique constraint. A repeated restock event inserts nothing new.
- Who may be on the waitlist?
- People who asked to be told and agreed to a WhatsApp message. Save that answer with the phone number.
- What if someone leaves between the restock and the send?
- The claim checks opt-in and waiting status again at send time, so a person who left gets nothing.
- What if a send times out?
- Treat it as possibly delivered. The sample marks it unknown and removes the person from the list so nobody is messaged twice.
Free sandbox, forever. Upgrade when you're ready.
Start freeKeep going
HelloHop is an independent WhatsApp API for developers: send and receive messages, automate groups and communities, and get signed webhooks. Not affiliated with WhatsApp or Meta. You link your own number.