SendGrid-compatible API (/sendgrid/v3)

Karta speaks SendGrid's mail send API, its bounce, spam report and global unsubscribe lists and its Event Webhook settings, so SendGrid's official clients and framework adapters work by changing the host and the key. Karta's tests run SendGrid's official Ruby client (sendgrid-ruby 6.7.0) and Swoosh's Sendgrid adapter against it, and compare its answers with the examples of SendGrid's OpenAPI description (test/fixtures/sendgrid).

Pointing a client at Karta

SettingValue
Base URLhttps://karta.example.com/sendgrid/v3
Keya Karta API key of the server, in Authorization: Bearer

The API lives under /sendgrid so that it does not meet other APIs' /v3 paths.

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

sg = SendGrid::API.new(api_key: ENV["KARTA_API_KEY"], host: "https://karta.example.com/sendgrid")
response = sg.client.mail._("send").post(request_body: mail.to_json)

Elixir, Swoosh (Swoosh.Adapters.Sendgrid):

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

Other clients: set the API host to https://<host>/sendgrid or the base URL to https://<host>/sendgrid/v3. For Rails, use SMTP submission or sendgrid-ruby.

Errors

In SendGrid's form, with SendGrid's status codes:

{"errors": [{"field": "template_id", "help": null, "message": "Stored templates are not supported"}]}

field names the part of the request at fault, as SendGrid names it (personalizations.0.to.1.email), or is null.

StatusMessageWhen
400(names the problem)an invalid or refused request
401authorization requiredno key, or a wrong one
403access forbiddenthe key lacks the scope
403The from address is not at a verified domain of this server's organisationfield from
403The server is suspended
404not founda SendGrid path Karta does not serve
405method not allowed/mail/send with another method than POST
413content too largea body over 30 MiB, as sent or after decompression
429too many requestsrate limited; Retry-After
500internal server errorthe messages could not be stored; try again

POST /sendgrid/v3/mail/send

Scope messages:send. A JSON body of up to 30 MiB, also gzip-compressed (Content-Encoding: gzip, which Swoosh's adapter sends with compress: true). Each personalization is one message, and all messages of a request are sent or none.

FieldNotes
personalizationsrequired, 1 to the instance's batch limit (KARTA_BATCH_MAX_MESSAGES, 100 by default); at most 1000 recipients in all, and 50 MiB of content in all the messages together
personalizations[].torequired, at least one; cc, bcc optional
personalizations[].from, subject, send_atreplace the request's for this message
personalizations[].headers, custom_argsmerged over the request's; the personalization's win
personalizations[].substitutionstext values, at most 100,000 bytes; each key is replaced by its value, as written, in the subject and the content (-name- becomes Bob), in one pass, the longest key first where keys overlap
from{"email", "name"}; must be at a verified domain
reply_to or reply_to_listnot both
subject
content[{"type": "text/plain" or "text/html", "value"}], each type once
attachments[{"content" (base64), "filename", "type", "disposition", "content_id"}]; disposition: inline with a content_id makes an inline part
headersMessage-ID and Sender become those fields; fields Karta writes itself are refused
categoriestags, at most 10, each 1 to 128 bytes
custom_argsvariables (string values), at most 10,000 bytes as JSON
send_atUnix seconds, at most 72 hours ahead
tracking_settings.click_tracking, open_trackingenable: false switches it off for these messages; an open_tracking.substitution_tag is removed from the content (Karta places its own pixel)
ip_pool_nameignored
{
  "personalizations": [
    {"to": [{"email": "bob@example.net", "name": "Bob"}], "substitutions": {"-name-": "Bob"}}
  ],
  "from": {"email": "ann@example.com", "name": "Ann"},
  "subject": "Hello -name-",
  "content": [{"type": "text/plain", "value": "Hello -name-, thank you for your order."}],
  "categories": ["receipts"]
}

The answer is 202 Accepted with an empty body and the Message-ID shared by the request's messages, without angle brackets, in X-Message-Id:

HTTP/1.1 202 Accepted
x-message-id: 01a1bbdc-73c6-77ef-8436-d6138cf2b656@example.com

The Message-ID is the Message-ID header given, or <uuid>@<from domain>.

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

FieldWhy
template_id, dynamic_template_datastored templates do not exist in Karta
sections
batch_ida batch cannot be paused or cancelled
asmunsubscribe groups do not exist
any mail_settings with enable: truesandbox mode, the bypass filters, footers, ...
tracking_settings.subscription_tracking, ganalytics with enable: true
content of other typesKarta sends text/plain and text/html only

Suppressions

Each list is the server's suppression list, entries of one reason: bounces (hard_bounce), spam reports (complaint) and the global unsubscribes (unsubscribe). Reading needs suppressions:read, changing suppressions:write.

RequestAnswer
GET /sendgrid/v3/suppression/bouncesa list of {"created", "email", "reason", "status"}
GET /sendgrid/v3/suppression/bounces/{email}a list with the entry, or []
DELETE /sendgrid/v3/suppression/bounces/{email}204
DELETE /sendgrid/v3/suppression/bouncesbody {"delete_all": true} or {"emails": [...]}; 204
GET, DELETE /sendgrid/v3/suppression/spam_reports[/{email}]the same, entries {"created", "email", "ip"}
GET /sendgrid/v3/suppression/unsubscribesa list of {"created", "email"}
POST /sendgrid/v3/asm/suppressions/globalbody {"recipient_emails": [...]} (at most 1000, all or none); 201 with the same body
GET /sendgrid/v3/asm/suppressions/global/{email}{"recipient_email": ...}, or {}
DELETE /sendgrid/v3/asm/suppressions/global/{email}204

The lists take limit (1 to 500, default 500), offset, start_time and end_time (Unix seconds, both included, on the time an entry last changed) and email (addresses starting with it; % stands for any text). created is a Unix time.

[{"created": 1794153149, "email": "frank@example.net"}]

What differs from SendGrid: an address has one entry per server, so adding it to the global unsubscribes moves it from another list; manual entries are in none of the lists; a bounce's reason and status and a spam report's ip are not kept (shown as "").

Event Webhook settings

/sendgrid/v3/user/webhooks/event/… (scopes webhooks:read to read, webhooks:write to change and test): the server's webhooks in SendGrid's format (Webhooks), whether they were made here or over the Karta API.

RequestAnswer
GET settings/all{"max_allowed": 20, "webhooks": [...]}
POST settings201 and the webhook
GET, PATCH settings/{id}the webhook (a PATCH changes only what it gives)
DELETE settings/{id}204
GET, PATCH settingsthe oldest webhook; GET shows empty settings ("enabled": false, "url": "") when there is none, PATCH adds it
GET settings/signed/{id}, GET settings/signed{"id": "…", "public_key": "…"} (id empty without one)
PATCH settings/signed/{id} with {"enabled": true}the same; false is refused: Karta signs every request
POST test with url or id204: a webhook.test event is sent to that webhook

A webhook is {"enabled", "url", "group_resubscribe", "delivered", "group_unsubscribe", "spam_report", "bounce", "deferred", "unsubscribe", "processed", "open", "click", "dropped", "friendly_name", "id", "public_key", "created_date", "updated_date"}, as SendGrid shows it. Its id is Karta's; friendly_name is its name (SendGrid webhook when none is given); public_key verifies its requests. At least one event must be on. spam_report, group_unsubscribe and group_resubscribe have no event in Karta: they stay off. A URL may be used by one webhook only (400, naming the other webhook's id and url). An unknown id is 404 Invalid id provided. OAuth (oauth_client_id, ...) is refused: give the URL a user name and password instead. account_status_change is ignored.

Not available

Every other SendGrid path under /sendgrid/v3 answers 404 not found: templates, statistics, suppression groups other than the global one, the Inbound Parse settings, senders, and so on.