Four setup and design notes for a WhatsApp health chat: Meta accounts, access token expiry, webhook signature, and a PIN before stored records open

Putting a Health Chat on WhatsApp: Setup Traps, Safeguards

This post is for anyone connecting WhatsApp to a product that holds health records. You will get the three setup traps that cost me time, six design rules for what the channel should never carry, a signature check you can run, and a diagram of our PIN flow.

I founded Kaizen Health, and our goal simply put in 2 sentences: “Caregiving already asks a lot. The health admin shouldn’t.” Adding WhatsApp is one way we are working toward that. It lets someone talk to Kai, “your guide to the records you upload,” in an app they already have. This is part one of two. Part two covers voice notes.

The problem

The test message went in and nothing came back. Our webhook had received it. The reply had been written. The send call to Meta had failed, and nothing on the phone said why.

Six steps from a test message to a reply on the phone. The webhook received the message and the reply was written, but the send call to Meta was rejected because the access token had expired, so nothing reached the phone.

That is the first problem. Setting up WhatsApp is full of steps that fail without telling you. The second is design. WhatsApp is a channel we do not control, so we decided early that stored health records never travel over it. Most of the rules in the second half follow from that.

Getting connected: four steps

Step 1: Keep your personal profile out of the business accounts

I needed a business profile on Meta. Every Meta account I had was connected to my personal profile in Meta Business Suite, and separating them was a challenge in itself. I ended up moving the Kaizen accounts into Meta accounts of their own.

If you are starting fresh, create the business accounts first. If you are not, budget extra time for the untangling.

Step 2: Get past the temporary token

The WhatsApp setup in the developer dashboard was straightforward. The test phone number and the access token were the confusing part. Meta hands you a temporary token to get started, and Meta’s developer announcement puts its lifetime plainly: “the token expires in less than 24 hours.”

When it lapses, the way back is not obvious. For me it meant editing the app, returning to the production setup step, and regenerating the token before I could test again. Until then, sends failed with no sign on the phone that a token was the problem.

Meta’s post names the way out of temporary tokens: “All you need to do is add a System User to your business account to obtain an access token.” We also now record an authentication rejection from Meta as a failure, not routine noise, so an expired token announces itself.

Step 3: Verify the business

I entered the Kaizen business details and Meta verified the business in under five minutes. That surprised me, because Twilio’s documentation warns that “Meta’s processing time for business verification varies by region and can take several weeks.” My sample is one business, once. Yours may take longer.

Step 4: Check before you pay Twilio

I also tried a Twilio number. I bought a generic number, not one set up for WhatsApp, and then started the A2P messaging registration. Twilio charged the one-time fee before I realized the registration was not needed for my goal.

My goal was only to use the number with WhatsApp through Meta. Twilio’s A2P 10DLC page says the registration applies to “SMS/MMS messages over a 10DLC number” sent to the US, and it does not mention WhatsApp. Twilio’s WhatsApp sign-up guide has a different requirement: “The phone number must not already be registered with WhatsApp.”

If you want a Twilio number for Meta’s WhatsApp and nothing else, check whether you need the A2P step before you pay for it. I did not.

Deciding what rides on the channel: six rules

What follows describes design decisions and development work with synthetic data. It says nothing about what is running in production.

Rule 1: Check the signature on the exact bytes Meta sent

Meta signs each webhook delivery so you can tell it came from Meta. Its webhook guide tells you to “Generate an HMAC-SHA256 hash using the JSON payload as the message input and your app secret as the secret key,” then compare it with the X-Hub-Signature-256 header.

The catch is that “the JSON payload” must be the exact bytes Meta sent. A body parser that runs first and re-serializes the JSON changes those bytes, and the hash stops matching. A minimal version, with placeholder names:

import express from "express";
import { createHmac, timingSafeEqual } from "node:crypto";

// Illustrative only. rawBody must be the Buffer Meta sent, not parsed JSON.
function isValid(rawBody, header, appSecret) {
  const expected =
    "sha256=" + createHmac("sha256", appSecret).update(rawBody).digest("hex");
  const a = Buffer.from(expected);
  const b = Buffer.from(header ?? "");
  return a.length === b.length && timingSafeEqual(a, b);
}

// Mounted before express.json() and before any request logger.
app.use("/hooks/example", express.raw({ type: "application/json" }), router);

To see the failure, sign a synthetic message, then check it again after a parse and re-serialize. The space after the colon disappears, and the check fails:

const secret = "demo-secret"; // synthetic
const raw = Buffer.from('{"text": "hi"}');
const header =
  "sha256=" + createHmac("sha256", secret).update(raw).digest("hex");

isValid(raw, header, secret); // true
isValid(Buffer.from(JSON.stringify(JSON.parse(raw.toString()))), header, secret); // false

That is why the webhook route sits above the JSON parser and above request logging. The verification handshake also carries a token in the query string, and it should stay out of logs.

Rule 2: Do each reply once, even when Meta delivers twice

Creating an account could be repeated without side effects. Replies needed their own guard. Meta redelivers a message if you do not acknowledge it, and people tap buttons twice.

We claim each incoming message with a short lease and mark it done only after the reply has gone out, so a failed send is retried and a repeated one is not sent twice. The guarantee has to live in the write, not in a check that happens before it.

Rule 3: Treat a phone number as weaker than a login, and add a PIN

Our app signs people in with email, Google or Apple. A phone number is a different kind of identity, because carriers reassign numbers over time. So on WhatsApp the number alone is enough to chat and to save a file you send, and nothing more.

Anything that opens stored records needs a PIN. A person who starts on WhatsApp is asked to set one right after they agree to the consent notice. They can keep chatting without it, but opening a saved record, downloading their data, or opening the Kaizen app from WhatsApp requires it. A phone is easy to share or pick up, and we did not want the chat thread on a shared phone to be enough for someone else to open a person’s records.

Five steps. The bot sends a one-use link in the chat. The link opens a hosted page that asks for the PIN. After the PIN passes, the server issues a separate one-use code. The app trades that code for a sign-in. The PIN is never typed into the chat.

Three decisions shaped it:

  • The PIN never goes through the chat. The chat thread is readable by whoever holds the phone, so a PIN typed there would sit in it. The bot sends a link instead. The link opens a page we host, the page asks for the PIN, and the link works once and expires quickly.
  • A link alone is not a login. The link only opens the PIN page. After the PIN passes, the server issues a separate one-use code, and the app trades that code for a sign-in.
  • The PIN is stored as a slow, salted hash with a server-side secret mixed in. We also refuse the most predictable choices, since a short numeric PIN has a small space of guesses.

The PIN is one layer. It sits next to consent, the hosted page and the context rules below, and I would not describe any of them as enough on its own.

Rule 4: Give the model less, by construction

The chat prompt on WhatsApp is built from fixed instructions and that conversation’s own history. Stored records, memory, profile and health score are never read by that code path.

We could have given the model everything and filtered its replies. We chose a context builder that cannot receive the data, because a rule enforced by what the code can reach holds up better than a rule enforced by asking the model nicely.

Rule 5: Say nothing to the model until the person consents

Before someone agrees to the notice, every reply is fixed text. Nothing they send is classified or sent to a model until they say yes.

Rule 6: Make the off switch one-way

A remote control can disable the channel, chat or file saving without a restart, and setting a field to false is all it can do. It cannot turn on a feature the deployment left off. A daily spend ceiling sits next to it, counted in the same transaction as each chat turn.

What I would tell someone starting today

  • Create the business Meta accounts before anything else, and keep your personal profile out of them.
  • Expect the temporary token to lapse mid-test, and make a rejected send show up as a failure you can see.
  • Read the registration docs before paying a vendor for one. Twilio’s A2P page covers SMS and MMS, and my goal was WhatsApp.
  • Verify webhook signatures against raw bytes, and mount that route before the JSON parser and the request logger.
  • Put a guarantee in the write, not in a check before it.
  • Limit what the model can reach in code instead of asking it to hold back.

What is next

WhatsApp also lets people send voice notes, and handling those is a different problem from handling text. That is part two.

Sources