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

InterfaceForDocument
Provisioning API, /api/v2/provisioning (own OpenAPI document at /api/v2/provisioning/openapi.json)your system: organisations, domains, credentials, webhooks, limits, suspend, resume, deleteProvisioning 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 namesWebhooks, registered with the provisioning API
Sending APIs and SMTP submissionthe customer's own applicationsKarta 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 with Karta.Release.revoke_provisioning_key/1. There is no HTTP call to make or revoke one.
  • Scopes: provisioning (everything) and provisioning:read (the GET operations). A missing scope is 403 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 provisioning key 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. A provisioning:read key 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: GET an organisation reference that does not exist. 404 not_found means the key is good; 401 unauthorized means it is not.

Lifecycle

StepCallWhat Karta does
createPUT /organisations/{ref}makes the organisation and one mail server "Default"; it starts restricted with low daily limits (limits)
changePUT /organisations/{ref} (new name), PUT .../limits, PUT .../domains/{name}, PUT .../credentials/{cref}, PUT .../webhooks/{wref}idempotent; a repeated PUT returns what exists
suspendPOST /organisations/{ref}/suspendsending stops, credentials are refused (server_suspended), queued mail waits; nothing is deleted
resumePOST /organisations/{ref}/resumesending resumes and queued mail is delivered; a server an administrator suspended stays suspended
deleteDELETE /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": [...]}}. The code is meant for programs. The ones the provisioning API renders:
StatuscodeWhen
401unauthorizedno key, or an invalid or revoked one
403forbiddenthe key lacks the scope
404not_foundno organisation, domain, credential or webhook with this reference
409kind_differsthe credential reference exists with another kind
409no_secretthe webhook's format has no secret to rotate
422invalid_requestthe body does not match the OpenAPI document
422invalid_organisation, invalid_domain, invalid_credential, invalid_limits, invalid_webhookthe record is invalid; details names the fields
422too_many_credentials50 active credentials already
429rate_limitedRetry-After gives seconds
  • Retries: every operation is safe to repeat (see Idempotency); retry a timeout, a 429 (after Retry-After) or a 5xx with 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 (404 otherwise).

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_today in GET .../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.