Skip to content
Private betaTreebars is letting teams in from the waitlist.Join the waitlist

WhatsApp API

Send WhatsApp from your own server through the project's WhatsApp channel, and hear back on a webhook: every message people send to your number, and where each message you sent has got to.

Before the first call

  • A WhatsApp channel connected to Meta, under Channels → WhatsApp, with the templates you will send approved there.
  • An API key with Send WhatsApp, from Settings → API keys. Keep it on your server.
  • To hear back, a webhook with WhatsApp messages ticked, under Settings → Webhooks.

Send a message

POST https://api.treebars.com/v1/whatsapp/messages with your key as a bearer token. Give an approved template, or free text, never both.

A templatebash
curl -X POST https://api.treebars.com/v1/whatsapp/messages \
  -H "Authorization: Bearer $TREEBARS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "project_id": "YOUR_PROJECT_ID",
    "to": "+201001234567",
    "idempotency_key": "order-5512-shipped",
    "reference": "order-5512",
    "template": {
      "name": "order_shipped",
      "language": "en_US",
      "variables": ["Mona", "5512"],
      "header": { "text": "5512" },
      "buttons": ["5512"]
    }
  }'
  • variables fill the body's {{1}}, {{2}} and so on, in order, one for each.
  • header fills a text header's slot, or gives a media header its media_url (and a document its filename).
  • buttons, by position, give a link button the end of its address, or a copy-code button its code. Use null for a button that needs nothing.
  • language can be left out when the template is approved in one language, or in the channel's default one.
  • channel_id is needed only when the project has more than one WhatsApp channel.
200json
{
  "delivery_id": "0193f2a1-7c4e-7b10-9a55-2f6d1c0e8b42",
  "message_id": "wamid.HBgMMjAxMDAxMjM0NTY3FQIAERgS",
  "status": "sent",
  "error": null,
  "reference": "order-5512"
}

Free text reaches a person only within 24 hours of their own last message to your number. That is WhatsApp's rule; outside it, send a template.

Retrying safely

idempotency_key is your own id for the message. Send the same call again, after a timeout say, and you get the first answer back with "duplicate": true, and nobody is messaged twice. The same key for a different message is refused.

A 503 with retry_later means WhatsApp asked us to slow down. Nothing was sent: try again shortly with the same key.

When it is refused

Every refusal is { "error": code, "message": sentence }:

  • invalid_number — to is not a phone number.
  • channel_not_found, channel_required, channel_off — no WhatsApp channel to send from, more than one and none named, or it is turned off.
  • opted_out — the number sent STOP. It is not messaged until it opts in again.
  • template_not_found — no template of that name is approved for the channel, in that language.
  • invalid_template_values — the number of variables does not match the template.
  • outside_window — free text more than 24 hours after the person last wrote.
  • idempotency_conflict, in_progress — the key was used for another message, or that call is still being sent.
  • retry_later — see above.

A message WhatsApp refused for good answers 422 with "status": "failed" and WhatsApp's reason in error.

We ask for no opt-in before an API send, only that the number has not opted out. Message people who agreed to hear from you; WhatsApp limits numbers that people block or report.

Hear back on your webhook

A webhook with WhatsApp messages ticked gets these, signed like every Treebars webhook (checking a signature):

  • whatsapp.message.received — somebody wrote to your number. Every message, with the whole text.
  • whatsapp.message.sent, .delivered, .read, .failed — where a message you sent through this API has got to, with your reference. Campaign messages are not reported here; their numbers are on the dashboard.
whatsapp.message.receivedjson
{
  "type": "whatsapp.message.received",
  "timestamp": "2026-10-24T09:41:00.000Z",
  "project": { "id": "deec0000-0000-4000-8000-000000000003", "name": "Demo App" },
  "data": {
    "message_id": "wamid.HBgMMjAxMDAxMjM0NTY3FQIAEhgg",
    "channel_id": "7f3c2a1e-9b4d-4c11-8e2f-1a0d5c6b7e90",
    "from": "+201001234567",
    "to": "+15556343466",
    "person_id": "4b1e0c55-2d7a-4f0e-9c3b-8a6e1f2d9c70",
    "profile_name": "Mona",
    "text": "Where is my order 5512?",
    "reply_to": "wamid.HBgMMjAxMDAxMjM0NTY3FQIAERgS",
    "keyword": null
  }
}

reply_to is the message_id of yours the person quoted, so you can thread a conversation. person_id is null until somebody in the project has that number. To answer within the 24 hours, send text to from.

Ask where a message is

Without a webhook, GET /v1/whatsapp/messages/DELIVERY_ID?project_id=… answers the same fields, with status one of sent, delivered, read or failed. Messages are kept for 30 days.