Mailgun-compatible API (/mailgun/v3)

Karta speaks the parts of Mailgun's API that applications use to send mail and manage suppressions and webhooks, so Mailgun's official clients and framework adapters work by changing the base URL and the key. Karta's tests run Mailgun's official Ruby client (mailgun-ruby 1.4.6) and Swoosh's Mailgun adapter against it, and compare its answers with the examples of Mailgun's API reference (test/fixtures/mailgun).

Pointing a client at Karta

SettingValue
Base URLhttps://karta.example.com/mailgun/v3
User nameapi
Password (API key)a Karta API key of the server
Domaina verified domain of the server's organisation

The API lives under /mailgun so that it does not meet other APIs' /v3 paths. Any domain that is not a verified domain of the key's organisation is 404 {"message": "Domain not found"}, whether it exists elsewhere or not.

Ruby, mailgun-ruby (the host includes /mailgun; the client adds /v3):

mg = Mailgun::Client.new(ENV["KARTA_API_KEY"], "karta.example.com/mailgun")
mg.send_message("example.com", from: "Ann <ann@example.com>", to: "bob@example.net",
                               subject: "Hello", text: "Hi", "o:tag" => "welcome")

Rails, mailgun-ruby's delivery method (Railgun::Mailer):

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
  api_key: ENV["KARTA_API_KEY"],
  domain: "example.com",
  api_host: "karta.example.com/mailgun"
}

Rails' delivery method copies every header of the message into h: fields; Karta ignores those it writes itself, so this works unchanged.

Elixir, Swoosh (Swoosh.Adapters.Mailgun):

config :my_app, MyApp.Mailer,
  adapter: Swoosh.Adapters.Mailgun,
  api_key: System.fetch_env!("KARTA_API_KEY"),
  domain: "example.com",
  base_url: "https://karta.example.com/mailgun/v3"

Other clients: set the base URL to https://<host>/mailgun/v3 (or the API host to <host>/mailgun).

Authentication and errors

Basic authentication with the user name api and the key as the password. A missing or wrong key, or a key without the endpoint's scope, gets 401 Forbidden (plain text, WWW-Authenticate: Basic realm="MG API"), as Mailgun answers. Other errors are {"message": "..."} with Mailgun's status codes: 400 for a request Karta cannot honour, 403 for a suspended server, 404 for an unknown domain or address, 429 when rate limited (with Retry-After), 500 when a message could not be stored (try again).

Sending

POST /mailgun/v3/{domain}/messages

A form (application/x-www-form-urlencoded, or multipart/form-data with files), up to 25 MiB. Fields may repeat; name[] is read as name.

FieldNotes
fromrequired; must be at a verified domain
torequired; repeated, or several addresses separated by commas; at most 1000
cc, bccthe same
subject, text, htmlat least one of text, html or an attachment
attachmentfiles
inlinefiles shown by the HTML as cid:<file name>
h:<Name>header fields. h:Reply-To and h:Sender become those fields; h:Message-Id the Message-ID; h:X-Mailgun-Variables (a JSON object) adds variables. From, To, Cc, Bcc, Subject, Date, MIME-Version, Content-Type, Content-Transfer-Encoding, Content-Disposition, Content-ID, Received, Return-Path and DKIM-Signature are ignored
v:<name>a variable
o:taga tag; repeated, at most 10
o:deliverytimean RFC 2822 date (Thu, 13 Oct 2011 18:02:00 GMT, or with +0200), at most 72 hours ahead
o:trackingno/false switches opens and clicks off for this message
o:tracking-opens, o:tracking-clicksno/false switches one off; yes, true, htmlonly leave the server's setting
recipient-variablesa JSON object by address: makes a batch (below)

The o:, h: and v: fields together may have at most 16 KiB ("Send options (parameters starting with o:, h:, or v:) are limited to 16KB total").

curl -s --user "api:$KARTA_API_KEY" https://karta.example.com/mailgun/v3/example.com/messages \
  -F from='Ann <ann@example.com>' -F to=bob@example.net \
  -F subject='Your order' -F text='Thank you for your order.' \
  -F o:tag=receipts -F v:order-id=1234
{"id": "<01a1bbdc-739f-7b0b-aac4-d0a5371d11b0@karta.example.com>", "message": "Queued. Thank you."}

id is the message's Message-ID with angle brackets, as Mailgun returns it.

Batches. With recipient-variables, Karta sends one message per to address, with only that address in To and %recipient.<name>% in the subject, text and HTML replaced by that address's values; unknown names stay as written. All messages share one Message-ID (<uuid>@<domain> unless h:Message-Id is given), which is the id returned. cc and bcc cannot be used in a batch. The whole batch is accepted or none of it. A batch has at most the instance's batch limit of recipients (KARTA_BATCH_MAX_MESSAGES, 100 by default; Too many recipients for a batch: at most 100) and at most 50 MiB of content in all its messages together, counted as they are made (The messages of a batch may have at most 50 MiB of content together).

Refused (400) rather than ignored, because ignoring them would send something other than what was asked:

FieldMessage
template, t:*Templates are not supported
amp-htmlamp-html is not supported
o:testmode=yeso:testmode is not supported: the message would be delivered
o:require-tls=yeso:require-tls is not supported
o:archive-too:archive-to is not supported

Other o: options (sending IPs, DKIM settings, send-time optimisation and so on) and unknown fields are ignored.

Other 400 messages name the problem, for example from parameter is not at a verified domain of this server's organisation, to parameter is missing, Need at least one of 'text', 'html', 'amp-html' or 'template' parameters specified, o:deliverytime may be at most 72 hours ahead.

POST /mailgun/v3/{domain}/messages.mime

A message given whole: to (the envelope recipients) and message (the MIME message, as a file or a field), plus the same o: and v: options (and h:X-Mailgun-Variables). Other h: fields are not added to a message given whole. The envelope sender is the message's Sender, or else its first From address. The answer is the same as for messages.

Events

GET /mailgun/v3/{domain}/events

Scope messages:read. Events of the server's outgoing mail, read from Karta's own records:

EventWhen
acceptedthe message was accepted for the recipient
delivereda delivery attempt succeeded
failedan attempt failed (severity temporary or permanent), or the recipient was suppressed (permanent, reason suppress-bounce, suppress-unsubscribe or suppress-complaint)
opened, clickedtracking events

Mailgun's other events (rejected, stored, unsubscribed, complained, list events) may be asked for and are simply never there: this list is read from Karta's message store, and complaints arrive as webhook events (complained, Webhooks) rather than as an event of a delivery.

Query parameters:

ParameterNotes
begin, endUnix seconds (fractions allowed) or RFC 2822 dates
ascendingyes or no; default newest first, or the direction from begin to end when both are given
limit1 to 300, default 100
eventone event, or several joined with OR
severitytemporary or permanent (failures only)
tagsone tag, or several joined with OR
recipientone address
message-idone Message-ID, with or without angle brackets

The filters from, to, subject, attachment, size, list and recipients, and expressions with NOT, AND, quotes or parentheses, are refused (400), so a filtered list is never silently wider than asked.

{
  "items": [
    {
      "envelope": {"sender": "ann@example.com", "targets": "bob@example.net", "transport": "smtp"},
      "event": "accepted",
      "flags": {"is-authenticated": true, "is-routed": false, "is-system-test": false, "is-test-mode": false},
      "id": "AaG73HOifamgo-GE8epldmE",
      "log-level": "info",
      "message": {
        "attachments": [],
        "headers": {
          "from": "ann@example.com",
          "message-id": "01a1bbdc-739f-7b0b-aac4-d0a5371d11b0@karta.example.com",
          "subject": "Your order",
          "to": "bob@example.net"
        },
        "size": 468
      },
      "recipient": "bob@example.net",
      "recipient-domain": "example.net",
      "tags": ["receipts"],
      "timestamp": 1794153149.344748,
      "user-variables": {"order-id": "1234"}
    }
  ],
  "paging": {
    "next": "https://karta.example.com/mailgun/v3/example.com/events/eyJhZnRlciI6...Wneb0OFg1_Zt2qzFvB4FPQ",
    "previous": "https://karta.example.com/mailgun/v3/example.com/events/eyJhZnRlciI6...0NgKMOypOU91RExPoGBfRQ"
  }
}
  • delivered and failed events add delivery-status (attempt-no, code, enhanced-code, message, mx-host, tls, certificate-verified); failures add severity and reason (generic for temporary, bounce for permanent, old when the retry period ran out).
  • opened and clicked add ip (the client's network) and client-info.user-agent; clicked adds url.
  • Not shown, because Karta does not keep them: attachments of the message (always []), geolocation, the client's device type and operating system, and storage links.

Paging. paging.next and paging.previous are links to /mailgun/v3/{domain}/events/{token}; the token holds the query and the position, signed with a key derived from the vault key, and is valid only for this server and domain. A page past the end is empty, as Mailgun's clients expect. Links are made from Karta's own URL (PHX_HOST), never from the request.

Suppressions

Each list is the server's suppression list, entries of one reason: bounces (hard_bounce), unsubscribes (unsubscribe) and complaints (complaint). The domain in the path only has to be a verified domain of the organisation; the list is the server's, for all its domains. Reading needs suppressions:read, changing suppressions:write.

An entry that has expired — the server's suppression policy gives entries of each reason a lifetime — is no longer on any of the lists, and adding an address here takes the policy's expiry for the list's reason (Suppressions).

RequestAnswer
GET /mailgun/v3/{domain}/{list}items and paging (first, last, next, previous links)
GET /mailgun/v3/{domain}/{list}/{address}the entry, or 404
POST /mailgun/v3/{domain}/{list} (form with address){"address", "message": "Address has been added to the bounces table"}
POST /mailgun/v3/{domain}/{list} (JSON list of {"address": ...}, fewer than 1000){"message": "N addresses have been added to the bounces table"} (all or none)
DELETE /mailgun/v3/{domain}/{list}/{address}{"address", "message"}, or 404
DELETE /mailgun/v3/{domain}/{list}removes every entry of that list

GET takes limit (default 100, at most 1000) and term (addresses starting with it); the paging links carry page (first, last, next, previous) and address. Entries are in address order.

{
  "items": [
    {"address": "erin@example.net", "code": "550", "created_at": "Sun, 08 Nov 2026 15:52:29 UTC", "error": ""}
  ],
  "paging": {
    "first": "https://karta.example.com/mailgun/v3/example.com/bounces?page=first&limit=10",
    "last": "https://karta.example.com/mailgun/v3/example.com/bounces?page=last&limit=10",
    "next": "https://karta.example.com/mailgun/v3/example.com/bounces?page=next&address=erin%40example.net&limit=10",
    "previous": "https://karta.example.com/mailgun/v3/example.com/bounces?page=previous&address=erin%40example.net&limit=10"
  }
}

What differs from Mailgun:

  • An address has one entry per server, so adding it to one list moves it from another; manual entries are in none of the lists.
  • A bounce's code and error are not kept (shown as "550" and ""), and a created_at given with an entry is ignored (the time it was added counts).
  • Unsubscribes are for all tags only ("tags": ["*"]); an entry for other tags is refused ("Karta keeps unsubscribes for all tags (*) only").

Webhooks

/mailgun/v3/domains/{domain}/webhooks, as mailgun-ruby's Mailgun::Webhooks calls it (scopes webhooks:read to read, webhooks:write to change). The webhooks are the server's in Mailgun's format (Webhooks), whether they were made here or over the Karta API, for every domain of the server; {domain} must be one of the organisation's verified domains.

RequestAnswer
GET …/webhooks{"webhooks": {"delivered": {"urls": ["https://…"]}, …}}: the names that have URLs
GET …/webhooks/{name}{"webhook": {"urls": […]}}; 404 Webhook not found when it has none
POST …/webhooks with id (the name) and url (repeated for up to three){"message": "Webhook has been created", "webhook": {"urls": […]}}
PUT …/webhooks/{name} with url{"message": "Webhook has been updated", "webhook": {"urls": […]}}
DELETE …/webhooks/{name}{"message": "Webhook has been deleted", "webhook": {"urls": […]}} (the URLs it had); 404 Webhook not found when it has none

The names are accepted, delivered, permanent_fail, temporary_fail, opened, clicked, unsubscribed and complained. POST and PUT both make the URLs given the name's URLs, whether it had some or not, so setting them again is harmless. Karta keeps one webhook per URL with the event types of every name it is listed for; a webhook left with none is removed. URLs are shown without a user name and password they were given with (mailgun-ruby's create then answers false, although the webhook was set). The webhooks made here share one signing key, that of the server's oldest webhook in Mailgun's format, as Mailgun signs every webhook with one key: rotate it over the Karta API to learn it (POST /api/v2/webhooks/{id}/secret; the new key is shown once and replaces the old one in every webhook that shares it).

Not available

Domains, routes, mailing lists, templates, statistics, tags management, IPs, the v4 webhooks API, the signing key endpoints and Mailgun's other APIs are not served.