Karta and Kantoor: the /kantoor/v1 interface
Contract version 1. This page describes what Kantoor expects from Karta and
what Karta offers. Karta's own description is its guide
guides/operations/kantoor.md; where the two differ, Karta's code is the
authority. Kantoor's side is lib/kantoor/karta/ (client.ex, req_client.ex,
inbound_payload.ex, body_fetcher.ex).
Karta is optional for Kantoor's design in principle, but today it is the only
mail source Kantoor can use. The interface is optional for Karta: it is mounted
only when Karta is built with KARTA_KANTOOR=1; without it there is no
/kantoor route and Karta behaves exactly as before.
Principles
- Karta stays the blob store and the mail transport; Kantoor keeps the index (folders, flags, UIDs, users). Karta learns nothing about those.
- Everything is at least once; Kantoor de-duplicates.
- Domains are created, verified and published in Karta. Kantoor only attaches to them.
Authentication
Every /kantoor/v1 request carries Authorization: Bearer <key>. The key is
an ordinary Karta API key (karta_<prefix>_<secret>, one mail server) marked as
a Kantoor key. A Kantoor key is accepted on /kantoor/v1 and nowhere else, and
other keys are refused there. A key can be restricted to domain and mailbox
patterns (*, example.com, *.example.com, *@example.com,
ann@example.com), so several Kantoor instances can serve different parts of
one server. Scopes: messages:list, messages:delete, messages:raw,
domains:read, plus the existing messages:send, messages:read,
routes:read, routes:write. Karta's operator tooling creates such keys, and so
does its provisioning API (PUT .../credentials/{ref} with kind: "kantoor"), which
is how a system with one Karta organisation per customer gets one per organisation.
Kantoor takes the instance key and base URL from KARTA_API_KEY and
KARTA_BASE_URL, and the signing secrets of its inbound endpoints from
KARTA_INBOUND_SECRETS. A mail domain may bring the key of its own Karta
organisation (POST /domains/{ref}/mail-source with api_key, kept encrypted on
the domain); every call about that domain uses it. Keys are never logged.
Operations
| # | Operation | Route | Scope | Status |
|---|---|---|---|---|
| 1 | Register inbound push | POST /kantoor/v1/endpoints (with 10) | routes:write | implemented in Karta (PR #62) and called by Kantoor |
| 2 | Receive inbound push | Karta calls POST <kantoor>/karta/inbound | webhook signature | implemented both sides |
| 3 | Message metadata | GET /kantoor/v1/messages/:id | messages:read | implemented in Karta (PR #62); Client.message/1, not used by a screen yet |
| 4 | Raw message, with Range and HEAD | GET /kantoor/v1/messages/:id/raw | messages:raw | implemented both sides |
| 5 | List messages (cursor) | GET /kantoor/v1/messages | messages:list | implemented in Karta; Kantoor's index rebuild from it is not built |
| 6 | Delete a delivery | DELETE /kantoor/v1/deliveries/:id | messages:delete | implemented both sides |
| 7 | Send, structured | POST /kantoor/v1/messages | messages:send | implemented in Karta; Kantoor sends raw only |
| 8 | Send, raw MIME | POST /kantoor/v1/messages/raw | messages:send | implemented both sides; auto_reply for the null sender (PR #64) |
| 9 | Outbound events | Karta webhooks | signature | not used by Kantoor |
| 10 | Attach to a domain or address (endpoint and route) | POST /kantoor/v1/endpoints, /routes | routes:write | implemented in Karta (PR #62) and called by Kantoor |
| 11 | Route retention | PATCH /kantoor/v1/routes/:id | routes:write | implemented in Karta; takes retention only. Kantoor does not call it |
| 12 | Health and contract version | GET /kantoor/v1/health | none | implemented both sides |
| 13 | List available domains | GET /kantoor/v1/domains | domains:read | implemented both sides |
Attaching (operations 1 and 10)
Two calls, both idempotent, so a lost answer or a failure after the first can be repeated. The checks below are against Karta's guide and tests (merged in Karta 1.0.0). The authors' live Kantoor has an attached domain on a live Karta and receives and sends through it; no report records that these two calls were watched there one by one.
POST /kantoor/v1/endpoints {"type": "http", "name": "kantoor:<domain>",
"url": "https://<kantoor>/karta/inbound", "notify_only": false}
201 {"id", "name", "kind", "url", "format": "karta", ..., "secret": "whsec_..."} new, secret shown once
200 {"id", ...} same name again: URL and notify_only updated, no secret
POST /kantoor/v1/routes {"domain": "<domain>", "local_part": "*" | "<part>",
"mode": "endpoint" | "reject" | "hold", "endpoint_id": "<id>",
"spam_mode": "mark" | "hold", "dmarc_reject": true,
"fail_closed": true, "plus_addressing": true}
optional "takeover": true
201 {"id", "address": "<part>@<domain>", "domain", "local_part", "mode", "endpoint_id", ...}
200 the same route when it already matches (or a takeover that changed it)The endpoint secret is returned once. Kantoor does not store it: the signing
secrets Kantoor checks are KARTA_INBOUND_SECRETS, as before.
Refusals ({"error": {"code", "message", "details": [{"field", "message"}]}}),
and what the admin Domains page says for each:
| Status, code | Meaning | Domains page |
|---|---|---|
403 forbidden_endpoint | the endpoint is another key's (or its owner is gone) and this key is restricted | use the registering key, or an unrestricted key of the server |
409 endpoint_conflict | an endpoint of that name exists that is not a mailbox endpoint; never taken over | rename or remove it in Karta |
409 route_conflict | a different route exists at the address; not replaced | change or remove it in Karta |
403 forbidden_domain | the domain is not verified for the key's organisation | verify it in Karta |
403 forbidden_address | the key's allowed_domains / allowed_mailboxes do not allow the address (a * route needs a restriction that matches *) | widen the key |
403 forbidden | the key lacks routes:write | |
422 invalid_endpoint, invalid_route | not https, a private destination, a bad mode, ... | Karta's detail is shown |
| 404 (no code) | the interface is switched off, or this Karta is older | "may not implement the Kantoor API yet" |
Ownership: the key that registered an endpoint owns it. A restricted key of the same server can neither register it again nor route to it; an unrestricted key may, and adopts an endpoint whose key is gone. This matters when the API key is rotated: attach again with an unrestricted key.
The routes Kantoor keeps
Kantoor keeps one route per address that should receive mail, plus the domain's
* route, and reconciles them on attach, on every mailbox or alias change, on
a catch-all change, on a domain or mailbox disable/enable, and when an
administrator clicks Re-sync routes (Kantoor.Karta.Routes):
- every active mailbox address and every alias address:
local_part= that local part,mode: "endpoint", with the protections on (spam_mode: "mark",dmarc_reject: true,fail_closed: true,plus_addressing: true); local_part: "*":mode: "endpoint"when the domain has a catch-all mailbox,mode: "reject"when it does not, so an unknown address is refused with550while the sender is connected instead of accepted and dropped;- a disabled mailbox: its exact route in
mode: "reject"(its mail is refused, not pushed to a mailbox that cannot take it); - a disabled or suspended domain: every route in
mode: "hold"(Karta keeps the mail and pushes nothing).
The routes are reversible: Karta's /kantoor/v1 has no route delete, and a
route in a non-delivering mode loses its endpoint_id (Karta.Inbound.Route's
changeset), after which "takeover": true would no longer find it. A reject
route is therefore posted with "spam_mode": "hold", which keeps the endpoint
on the row; reject means nothing is ever delivered, so the spam mode has no
effect. A later sync posts the route again with mode: "endpoint" and
spam_mode: "mark".
The order matters: exact routes first, * last. When an exact route cannot be
written, * is not moved to reject (that would refuse mail for a mailbox
whose own route is missing), and the failure is stored on the domain.
Switching a domain off (set_route_mode) is not a PATCH: Karta's PATCH /routes/:id changes retention only. Kantoor posts the same route again with
"mode": "hold" (or "endpoint") and "takeover": true, which Karta accepts
only for a route that already points at the endpoint named. For that Kantoor
records the endpoint id when it attaches a domain. A domain attached before it
did so cannot be switched this way (the page says so); remove and attach it
again.
GET /kantoor/v1/domains lists each domain with its routes (local_part,
mode, endpoint_id, the spam fields), which is how the sync sees the routes
that already exist and turns a route at an address that is no longer a mailbox
or alias to reject.
Raw reads
GET or HEAD /messages/:id/raw serves incoming and outgoing messages the key
may see. Content-Length is exactly the stored size. A single
Range: bytes=a-b (or a-, -n) answers 206; a range past the end answers
416. Bodies are encrypted in 64 KiB chunks, so only the chunks that hold the
range are read. A body cut short is an error, never fewer bytes.
Delete
Deleting is per delivery. A message sent to several mailboxes is one body with several deliveries; the body is removed only when every delivery is deleted or expired. The operation is idempotent and reveals nothing about deliveries the key may not touch.
Send
Raw MIME is posted as {"data": <base64>, "mail_from", "rcpt_to": [...], "retention", "source": "kantoor"}; the answer is 202 {"id", "recipients"}.
(Kantoor used to send the message under raw, which Karta's schema does not
know: fixed with this change.) The sender must be allowed by the key's
restrictions (403 forbidden_sender); the From header inside a raw message
is not checked. Sent copies live in Karta.
Automatic replies. A vacation reply is sent with "auto_reply": true and
"mail_from": "", which Karta turns into MAIL FROM:<> (RFC 3834), so a
failure cannot bounce. The message must carry Auto-Submitted: auto-replied
(it does), or Karta refuses it (422 not_auto_submitted); the null sender is not
subject to the key's sender restriction. A Karta that predates the field refuses
the request for auto_reply (422, its schema allows no other fields); Kantoor
then sends the reply once more with the mailbox address as the envelope sender,
as before this change. Any other refusal is not retried that way.
The inbound push (operation 2)
POST <kantoor>/karta/inbound
webhook-id: <delivery id> # the same on every attempt; the dedupe key
webhook-timestamp: ...
webhook-signature: v1,... # Standard Webhooks, the endpoint's whsec_ secret
{"type": "message.received", "timestamp": "...",
"data": {"id": "<delivery id>", "message_id": "<message id>",
"server_id": "...", "route_id": "...",
"mail_from": "...", "rcpt_to": "...", "header_message_id": "...",
"from": "...", "subject": "...", "size": 1234,
"received_at": "...",
"authentication": {"ip": "192.0.2.1",
"spf": {"result": "pass", "domain": "example.org"},
"dkim": [{"result": "pass", "domain": "example.org", "selector": "s1"}],
"dmarc": [{"result": "pass", "domain": "example.org"}],
"authserv_id": "mx.example.com"},
"raw": "<the message, base64>"}}authenticationis what Karta found out about the sender (SPF, DKIM, DMARC and ARC; keys that are absent are left out) and isnullfor a message that was not checked. Kantoor trusts only this field for the sender's identity, never the message's ownAuthentication-Resultsheader. A received calendar invitation is applied only when the From domain passed DMARC, or passed DKIM or SPF for an aligned domain. Kantoor still accepts the olderdata.authentication_resultsanddata.authforms.rawis absent on a notify-only endpoint (an operator option in Karta); Kantoor then fetches the body with operation 4. Kantoor accepts both forms.- Kantoor answers 2xx only after its index transaction commits. Karta retries a
5xx, 408 or 429; any other 4xx marks the delivery failed. The 2xx answer may
carry
{"retention": "keep"}or{"retention": <days>}for that delivery.
Retention and storage
Kantoor decides and Karta enforces. A policy is "keep", a number of days, or
none (the server's own rule, unchanged). It lives on the delivery: copied from
the route, replaced by the push answer, or taken from the send request. With
several deliveries the longest wins and keep beats any number of days.
keep is exempt from Karta's size cap. Karta has an optional per-server cap on
kept storage (no default) and exposes kept-storage metrics; it alerts through
metrics only.
Quotas
Quotas live in Kantoor, are virtual (computed from the index) and are set per domain with a per-mailbox override. Mail over quota is accepted and acknowledged, stored, and held out of sight (webmail, IMAP, POP3) until space is freed; its IMAP UID is assigned when it becomes visible. Deleting mail deletes the delivery in Karta.
Failure behaviour
- Karta unreachable on send: the send fails visibly; Kantoor does not queue it.
- Duplicate pushes: ignored on
webhook-id. - Missed pushes (Kantoor down longer than Karta's retry window): the listing (operation 5) could recover them, but the recovery job is not built.
- Loss of Kantoor's database: the listing and raw reads can rebuild messages but not flags or folders, so back up Kantoor's database.
- Revoked or rotated key: Kantoor loses access until the new key is configured.
Known gaps
- Operations 1, 3, 10 and the null sender are checked against Karta's code and
tests (merged in Karta 1.0.0). On the authors' live installation mail arrived
through an attached domain and a vacation reply went out through Karta during
the 1.0 audit (12 and 13 November 2026); the calls were not inspected one by one there.
The older-Karta fallback is written for the
422its strict schema gives; it has not been seen against a real older Karta. - Bounces and delivery status notifications are not handled in Kantoor.
- Kantoor's index rebuild and missed-push recovery from operation 5 are not built.
Signed scan metadata for forwarding
Karta sends data.scan in both full and notify-only pushes: the stored
Karta.Scan.stored/2 map, or null. Kantoor only forwards when
spam.status == "scanned", spam.is_spam == false, and
virus.status == "clean". Headers cannot supply this verdict. Older senders
without this additive field remain compatible for ordinary delivery; forwarding
fails closed and keeps the source copy. The raw send operation already signs
for the rewritten From domain; no SRS address is generated by Kantoor.
Spam verdicts
Karta sends a per-recipient spam verdict in its signed push. A scanned verdict
with is_spam: true starts the message in the mailbox's junk folder; mailbox
filters can then move it. Message headers cannot supply this verdict. An absent
verdict preserves normal inbox delivery. A released quarantine delivery is
marked clean by Karta and follows normal mailbox filtering.