The messaging module

Karta can keep conversations with people over WhatsApp and SMS, beside its mail: a shop's own chats with its customers and the updates it sends about an order. An application sends through one call (POST /messaging/v1/messages), hears about replies and delivery through the signed webhooks Karta already has, and reads the whole conversation back. Karta stores everything it sends and receives, text and files, for a retention period you choose.

Status, 13 November 2026. Built and tested, merged, off by default; not proven end to end. No SMS has been delivered through it yet (against the real Bird API only authentication, the look-up shape and the error shape were seen), no real Postduif agent has connected, it has never run behind the production proxy, and the Postduif protocol is a draft. The API and the protocol are not stable interfaces yet. See Status and roadmap.

This is not a tool for bulk or campaign messages and has none of their features: there is no list, no broadcast, no template, and the category marketing is refused. It is for an organisation's own conversations and the updates about what a customer ordered.

The module is optional and off by default. While it is off there is no route to it (every /messaging/v1 path and every messaging page answers as an unknown path does, 404), no menu entry, no job, no process, no metric and no webhook event; mail does not change when it is on. Its tables are created by the migrations either way (all new tables; nothing existing is altered) and stay empty.

The words

  • A channel is a medium: whatsapp or sms.
  • A plug is the code of a kind of connector. postduif is the connection for an agent that sends WhatsApp from the shop's computer (no real agent has connected yet) (Connecting Postduif). bird_sms is meant to send SMS through Bird and runs inside Karta (not yet proven: nothing has been delivered) (SMS through Bird).
  • A connector is one configured instance of a plug on a mail server: "Postduif, Winkel Kralendijk". It has a status, a last-seen time and, for Postduif, an agent version.
  • An address is where a person is reached: a phone number in E.164 (+5997771234), lid:... when WhatsApp hides the number, group:... for a group (groups are received and stored, never sent to).
  • A conversation is one thread between a connector and one address; a message is one item in it. An outbound message is one attempt of a send request: the same request may produce an attempt on WhatsApp and, if allowed, a second one on SMS.

Everything belongs to a mail server, and so to its organisation, as the rest of Karta does. An API key of one server never sees another server's connectors, conversations, messages or files.

Switching it on

KARTA_MESSAGING=1

config/runtime.exs reads it at run time (config :karta, :messaging, enabled: true): set the variable and restart, no rebuild. The routes are always compiled; KartaWeb.Messaging.Gate answers them as unknown routes while the switch is off. Run the migrations (/app/bin/migrate) before you start the new version, as always.

VariableMeaning
KARTA_MESSAGING1 or true switches the module on.
KARTA_MESSAGING_RETENTION_DAYSThe default retention of messages and files, 1 to 36500 days; 730 if unset. An organisation may choose its own.
KARTA_MESSAGING_PUBLIC_URLThe public https:// address agents connect to, when it is not the address Karta serves (PHX_HOST). It becomes the wss:// address in the enrolment answer.
KARTA_MESSAGING_MIN_AGENT_VERSIONAgents older than this are told to update (close code 4005) and shown as needs update. Default 0.0.0: no minimum.
KARTA_MESSAGING_LATEST_AGENT_VERSION, KARTA_MESSAGING_UPDATE_MANIFEST_URL, KARTA_MESSAGING_UPDATE_CHANNELWhat welcome tells agents about updates.

With the module on, Karta also runs the Oban queue messaging (5 at a time on each node), a housekeeping job every minute and the retention job every night.

New API key scopes appear: messaging:send (send, check a recipient, upload a file), messaging:read (read conversations, messages, files, connectors) and messaging:manage (connectors, enrolment, settings). Make the key in the console or through the provisioning API; a mail key has none of them.

What is stored, for how long, and who can read it

Everything sent and received is stored: the text, the files, the addresses, the times and the states. Messages and their files are removed after the organisation's retention (the instance default is 730 days, set by KARTA_MESSAGING_RETENTION_DAYS; an organisation may choose 30 to 3650 days or keep them forever, on the Messaging settings page or PUT /messaging/v1/settings), and a sign-in code (category: authentication) after 7 days at most whatever was chosen. The retention job runs every night, in batches, and deletes a file from the store before the row that names it, so a failure leaves a row to try again and never a file nobody can find; the text of a send request goes with it. The text of a message is encrypted in the database column with Karta's vault key (AES-256-GCM, a fresh IV per value) and files are encrypted blobs, one key per object, like stored mail (Storage and encryption); addresses, times and states are not encrypted so that they can be searched, which is why the archive is searched by number and not by words in the text. The people who can read the messages are the members of the organisation in the console (read only), API keys with messaging:read, and anyone who has both the database and the vault key; an operator with the database alone sees ciphertext in place of the text. One exception, off by default: the messaging.message.received webhook event can carry the text (Messaging settings, "webhook events carry the text"); webhook events are kept 30 days in a table that is not encrypted and outside this retention, so switch it on only if the application cannot read the message with the API instead.

The console

Under a mail server the menu gets Connectors (status, last seen, agent version and key fingerprint, the last events; owners and admins make a connector, show a new one-time code, link or unlink an account, revoke an agent's key, remove a connector), Conversations (an archive, newest first, searched by any part of the number or the name, with each conversation's thread and its files) and Messaging settings (retention, send limits, quiet hours, default channels, the server's country for local numbers, whether SMS may be used, whether received webhook events carry the text). The pages are read only: replying, assigning and drafting belong to the application that uses the API. Everyone in the organisation can read them; changes need an owner or an admin.

Guard rails

Karta enforces them, not the application:

  • Limits: each connector sends at most 10 messages a minute and 300 a day by default, with a gap and random jitter between sends (the agent applies the limits Karta sends it); the connector's limits or the organisation's settings change them.
  • No bulk or campaign features: no lists, no broadcast, no template, no marketing category.
  • No first contact without a reason, quiet hours, paid channels (SMS) allowed per server and per request with a daily cap, a fallback only on facts: see Routing and guard rails.

Running several replicas, and behind a proxy

An agent's WebSocket lives on one replica. Whatever any replica needs the agent to do is written to the connector's outbox in the database (a row with a gap-free sequence number) and announced; the replica that holds the connection claims the command with one statement and pushes it. The announcement travels through the cluster's PubSub when the replicas are joined (KARTA_CLUSTER) and through the database notification (Karta.Relay) otherwise, and every connection also reads its outbox every 5 seconds, so a lost announcement only delays. The result is the same with KARTA_CLUSTER on and off, and with any number of replicas (Running several replicas). A second connection of the same connector replaces the first (close code 4004), on whichever replica it was.

The agent connects with wss://your-karta/messaging/v1/connect. Behind Traefik or another reverse proxy nothing special is needed beyond what a WebSocket needs: the proxy passes Upgrade/Connection (Traefik does by default) and does not close an idle connection in under a minute (agents ping every 25 seconds). The proxy terminates TLS; set KARTA_MESSAGING_PUBLIC_URL if the address agents use differs from PHX_HOST. The proof of the agent's key names the host of that address, so the host must be the same the agent used.

Metrics

With the module on, Karta exports (and nothing when it is off):

  • karta_messaging_messages{server, channel, direction, state}
  • karta_messaging_send_errors{plug, class}
  • karta_messaging_connector_up{connector}
  • karta_messaging_command_latency_milliseconds
  • karta_messaging_fallbacks{from, to, reason}
  • karta_messaging_cost{plug, currency}
  • karta_messaging_inbound_lag_seconds

No label carries a number, a name or text. Sends, unlinks, new codes, revokes and setting changes are written to the audit log.

What to check after switching it on

  1. GET /messaging/v1/openapi.json answers 200 (it answers 404 while the module is off).
  2. The console shows Connectors under the server.
  3. Make a Postduif connector and follow Connecting Postduif.
  4. Send a test message from the API (The messaging API).