Driving Karta from your own system
This page is the map for a self-hoster who wants to run Karta from their own desk, billing or sign-up system. Karta knows nothing about such a system: it sees a provisioning key, references the system chooses, and ordinary organisations, domains, credentials and webhooks. Shiloh's own hosted service uses exactly this interface; nothing else is needed.
The roles:
- Karta: the mail service you run.
- Your provisioning system: whatever signs customers up, takes payment and decides who may send how much.
- The customer's applications: they send mail with the credentials your system hands out, through SMTP submission or the HTTP APIs.
What exists
| Interface | For | Document |
|---|---|---|
Provisioning API, /api/v2/provisioning (own OpenAPI document at /api/v2/provisioning/openapi.json) | your system: organisations, domains, credentials, webhooks, limits, suspend, resume, delete | Provisioning API |
| Webhooks, in Standard Webhooks format (or the Postal, Mailgun, SendGrid, Postmark formats) | events about an organisation's mail, sent from Karta to a URL your system names | Webhooks, registered with the provisioning API |
| Sending APIs and SMTP submission | the customer's own applications | Karta API, API keys and scopes |
Karta's optional messaging module (SMS, WhatsApp) is not part of the published interfaces yet.
Authentication
- A provisioning key,
Authorization: Bearer kprov_<prefix>_<secret>. It is made by an instance administrator on the command line (Karta.Release.provisioning_key/2), shown once, kept only as an Argon2id hash, and revoked withKarta.Release.revoke_provisioning_key/1. There is no HTTP call to make or revoke one. - Scopes:
provisioning(everything) andprovisioning:read(theGEToperations). A missing scope is403 forbidden. - The key is instance-wide: it reaches every organisation made through the provisioning API, by reference. It belongs to no mail server and is accepted on no other API; a mail server's API key is not accepted on this one.
- Rotation: make a second key, switch your system to it, revoke the first.
- What a leaked
provisioningkey can do: make, rename, suspend and delete organisations, add and remove their sending domains, make and rotate their credentials (so it can obtain a working sending credential for any organisation it can name) and set their limits and trust level. It cannot read a message, a recipient address or a body, and it cannot touch organisations made on the admin pages. Aprovisioning:readkey can read organisations, limits, domains (including the DNS records to publish), and the list of credentials and webhooks, never a secret. - A cheap way to test a key:
GETan organisation reference that does not exist.404 not_foundmeans the key is good;401 unauthorizedmeans it is not.
Lifecycle
| Step | Call | What Karta does |
|---|---|---|
| create | PUT /organisations/{ref} | makes the organisation and one mail server "Default"; it starts restricted with low daily limits (limits) |
| change | PUT /organisations/{ref} (new name), PUT .../limits, PUT .../domains/{name}, PUT .../credentials/{cref}, PUT .../webhooks/{wref} | idempotent; a repeated PUT returns what exists |
| suspend | POST /organisations/{ref}/suspend | sending stops, credentials are refused (server_suspended), queued mail waits; nothing is deleted |
| resume | POST /organisations/{ref}/resume | sending resumes and queued mail is delivered; a server an administrator suspended stays suspended |
| delete | DELETE /organisations/{ref} | sending stops, credentials are revoked, webhooks are switched off, domains and their keys are removed, the reference is free again; the organisation's mail is kept until retention removes it |
Read-outs: GET /organisations/{ref} (status active or suspended),
GET .../limits (trust level, limits and recipients_today), GET .../domains
and GET .../domains/{name} (status, DNS records to publish, last DNS checks).
Requests, errors, retries
- JSON in, JSON out. Lists are
{"data": [...]}. - Errors:
{"error": {"code": "...", "message": "...", "details": [...]}}. Thecodeis meant for programs. The ones the provisioning API renders:
| Status | code | When |
|---|---|---|
| 401 | unauthorized | no key, or an invalid or revoked one |
| 403 | forbidden | the key lacks the scope |
| 404 | not_found | no organisation, domain, credential or webhook with this reference |
| 409 | kind_differs | the credential reference exists with another kind |
| 409 | no_secret | the webhook's format has no secret to rotate |
| 422 | invalid_request | the body does not match the OpenAPI document |
| 422 | invalid_organisation, invalid_domain, invalid_credential, invalid_limits, invalid_webhook | the record is invalid; details names the fields |
| 422 | too_many_credentials | 50 active credentials already |
| 429 | rate_limited | Retry-After gives seconds |
- Retries: every operation is safe to repeat (see
Idempotency); retry a timeout, a
429(afterRetry-After) or a5xxwith the same request. Operations on one organisation are serialised by a database lock, so two simultaneous requests for one reference cannot make two records. - Rate limit: 600 requests a minute for each key; failed key verifications are limited per client address.
- Ordering: there is no ordering to respect between organisations. Within one
organisation make the organisation first; domains, credentials and webhooks
need it (
404otherwise).
A worked example
All values are fake.
export KARTA=https://karta.example.com/api/v2/provisioning
export KEY=kprov_abcd1234_not-a-real-secret
# 1. the customer
curl -s -X PUT "$KARTA/organisations/cust-1001" \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"name": "Acme BV"}'
# 2. a sending domain; publish the records in the answer, then verify
curl -s -X PUT "$KARTA/organisations/cust-1001/domains/mail.example.org" \
-H "Authorization: Bearer $KEY"
curl -s -X POST "$KARTA/organisations/cust-1001/domains/mail.example.org/verify" \
-H "Authorization: Bearer $KEY"
# 3. a sending credential (the secret is in the first answer only)
curl -s -X PUT "$KARTA/organisations/cust-1001/credentials/web-app" \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"kind": "api", "scopes": ["messages:send"]}'
# 4. let it send more, once your system has decided it may
curl -s -X PUT "$KARTA/organisations/cust-1001/limits" \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"trust_level": "standard", "daily_send_limit": 5000}'
# 5. an unpaid invoice: stop sending, later resume
curl -s -X POST "$KARTA/organisations/cust-1001/suspend" \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"reason": "payment overdue"}'
curl -s -X POST "$KARTA/organisations/cust-1001/resume" -H "Authorization: Bearer $KEY"
Versioning and stability
The provisioning OpenAPI document is titled "Karta provisioning API" with
info.version 1, under the path prefix /api/v2. Error codes are described
as stable for programs (Karta API). Beyond that there is
no stability promise yet: no deprecation policy or compatibility period is
written down, so read the changelog before an upgrade.
Not available yet
- Listing organisations: there is no list call; your system keeps its own references.
- Making or revoking provisioning keys over HTTP (command line only).
- Events about the provisioning lifecycle itself (an organisation suspended or deleted): webhooks report an organisation's mail events, not changes your system made.
- Usage read-outs beyond
recipients_todayinGET .../limits: no counts of messages sent, bounced or held per organisation, no history. - Changing an organisation's reference, or the slug on rename.
- Several mail servers per organisation: an organisation has one, "Default".
- Per-organisation DKIM key rotation or extra DKIM keys.
- A dry run or bulk form of any call.