Skip to content

SMS + WhatsApp ​

Goal: send and receive SMS and WhatsApp messages from the same API, with replies threaded into conversations on the contact's timeline.

1. Send ​

ts
import { Kolwire } from "@kolwire/sdk";
const vi = new Kolwire();

// SMS (channel defaults to "sms"; use "mms" with media_urls)
await vi.messages.create({ from: "+15550001111", to: "+15550002222", body: "Your table for 2 is confirmed for 7pm." });

// WhatsApp, inside the 24-hour customer-service window: free-form
await vi.messages.create({ channel: "whatsapp", from: "+15550001111", to: "+447700900123", body: "Thanks! Anything else?" });

// WhatsApp, outside the window: an approved template
await vi.messages.create({
  channel: "whatsapp",
  from: "+15550001111",
  to: "+447700900123",
  template: { name: "order_shipped", language: "en", variables: { "1": "Sam", "2": "#1042" } },
});
python
from kolwire import Kolwire
client = Kolwire()

client.messages.create(from_="+15550001111", to="+15550002222", body="Your table for 2 is confirmed for 7pm.")

client.messages.create(channel="whatsapp", from_="+15550001111", to="+447700900123", body="Thanks! Anything else?")

client.messages.create(
    channel="whatsapp", from_="+15550001111", to="+447700900123",
    template={"name": "order_shipped", "language": "en", "variables": {"1": "Sam", "2": "#1042"}},
)

Status updates arrive as message.sent, message.delivered, message.read (WhatsApp) and message.failed (with error_code).

Templates ​

WhatsApp requires Meta-approved templates for business-initiated messages:

ts
await vi.whatsapp.templates.create({
  name: "order_shipped",
  language: "en",
  category: "UTILITY",
  components: [{ type: "BODY", text: "Hi {{1}}, your order {{2}} has shipped." }],
});
for await (const t of vi.whatsapp.templates.list()) console.log(t.name, t.status);
python
client.whatsapp.templates.create(
    name="order_shipped", language="en", category="UTILITY",
    components=[{"type": "BODY", "text": "Hi {{1}}, your order {{2}} has shipped."}],
)
for t in client.whatsapp.templates.list():
    print(t.name, t.status)

2. Receive ​

Point the number's messaging_config.webhook_url (or a webhook endpoint subscribed to message.*) at your server:

ts
app.post("/messages", express.raw({ type: "application/json" }), async (req, res) => {
  const event = await vi.webhooks.constructEvent(req.body, req.headers);
  if (event.type === "message.received") {
    const { from, to, body, channel, conversation_id } = event.data;
    await vi.messages.create({ channel, from: to, to: from, body: `You said: ${body}` });
  }
  res.sendStatus(204);
});
python
@app.post("/messages")
async def messages(request: Request):
    event = client.webhooks.construct_event(await request.body(), request.headers)
    if event.type == "message.received":
        d = event.data
        client.messages.create(channel=d.channel, from_=d.to, to=d.from_, body=f"You said: {d.body}")
    return Response(status_code=204)

Hand the whole thread to an AI agent instead: set messaging_config: { agent_id } on the number — the same agent can serve voice and messaging, with memory per contact.

3. Conversations ​

Messages are grouped into conversations per contact, channel and sender address:

ts
for await (const c of vi.conversations.list({ status: "open", channel: "whatsapp" })) {
  const msgs = await vi.conversations.messages(c.id, { limit: 20 });
  console.log(c.id, c.wa_session_expires_at, msgs.data.length);
}
await vi.conversations.update("conv_123", { assigned_to: "ta_ana", status: "open" });
python
for c in client.conversations.list(status="open", channel="whatsapp"):
    msgs = client.conversations.messages(c.id, limit=20).get_page()
    print(c.id, c.wa_session_expires_at, len(msgs.data))
client.conversations.update("conv_123", assigned_to="ta_ana", status="open")

wa_session_expires_at tells you until when free-form WhatsApp replies are allowed.

Opt-out and opt-in ​

STOP, UNSUBSCRIBE, CANCEL, END, QUIT (and localized variants) opt the sender out of that channel automatically: further sends to them fail with an error and you receive conversation.opted_out. START / UNSTOP opt back in (conversation.opted_in). HELP gets your configured help reply. See compliance for 10DLC registration of US A2P SMS.