MailCraft
Home Pricing Docs
Integration

REST API Reference (/v1.1/email)

Complete guide to POST /v1.1/email, Idempotency-Key deduplication, HTTP 402 credit errors, and GET status polling.

Authentication

Pass your Mail Agent API Key using the Authorization header:

Authorization: Bearer maas_live_xxxxxxxxxxxx

Send Email: POST /v1.1/email

Headers

Header Required Description
Authorization Yes Bearer <api_key>
Content-Type Yes application/json
Idempotency-Key Recommended Deduplicates retries for 24 hours per agent and guarantees single credit deduction.

Request Body

{
  "from": { "address": "alerts@mail.example.com", "name": "MailCraft Alerts" },
  "to": [
    { "address": "user1@example.org", "name": "User One" }
  ],
  "cc": [
    { "address": "ops@example.org" }
  ],
  "bcc": [],
  "reply_to": [
    { "address": "support@example.com", "name": "Support" }
  ],
  "subject": "Your verification code",
  "htmlbody": "<p>Your one-time code is <strong>482910</strong>.</p>",
  "textbody": "Your one-time code is 482910.",
  "mime_headers": {
    "X-App-Event": "login-otp"
  },
  "attachments": [
    {
      "name": "receipt.pdf",
      "mime_type": "application/pdf",
      "content": "<base64-encoded-bytes>"
    }
  ]
}

Idempotency-Key

Retrying a request with the same Idempotency-Key within 24 hours under the same Mail Agent returns the original response without queuing duplicate messages or charging credits a second time.

Handling 402 Payment Required

Credits are deducted atomically inside the acceptance transaction (1 credit per non-suppressed recipient across to, cc, and bcc). If your account balance is insufficient, nothing is queued and HTTP 402 is returned:

{
  "error": "insufficient credits",
  "balance": 2,
  "required": 5
}

Check Delivery Status: GET /v1.1/email/{message_id}

Poll aggregate status plus per-recipient delivery outcomes and raw SMTP responses:

curl https://api.mailcraft.local/v1.1/email/7f3a1c2e-8b4d-4f6a-9a10-32f9e0a1b2c3 \
  -H "Authorization: Bearer maas_live_YOUR_API_KEY"
{
  "message_id": "7f3a1c2e-8b4d-4f6a-9a10-32f9e0a1b2c3",
  "aggregate_status": "partial",
  "recipients": [
    {
      "address": "ok@gmail.com",
      "status": "delivered",
      "attempt_count": 1,
      "last_smtp_code": 250,
      "last_enhanced_code": "2.0.0",
      "last_smtp_response": "250 2.0.0 OK"
    },
    {
      "address": "busy@outlook.com",
      "status": "deferred",
      "attempt_count": 1,
      "last_smtp_code": 421,
      "last_enhanced_code": "4.7.0",
      "last_smtp_response": "421 4.7.0 Temporary rate limit"
    }
  ]
}

aggregate_status is one of queued, partial, delivered, bounced, failed, scheduled or cancelled. Per-recipient status can also be sending, deferred, suppressed, scheduled or cancelled. Scheduled messages additionally return send_at (and cancelled_at once cancelled); template sends return template_id.

Optional request fields

These fields can be added to POST /v1.1/email (and to every message of a batch):

Field Type Description
template_id / template_alias string Render a stored template instead of sending subject / htmlbody / textbody (see Templates). Use one of the two.
template_data object Variables for the template. Only allowed together with template_id / template_alias. Max 256 KB.
send_at string (RFC 3339) Schedule the message, from 1 minute to 30 days ahead, e.g. 2026-12-24T09:00:00+07:00 (see Scheduled sending).
list_unsubscribe boolean Add (or suppress) the one-click unsubscribe headers for this message, overriding the Mail Agent default (see One-click unsubscribe).
idempotency_key string Per-message idempotency key, mainly for batch items. For single sends the Idempotency-Key header takes precedence.

Templates

Create templates in the dashboard (Templates menu): a name, an alias (unique per account, e.g. order-shipped), a subject, an HTML part, a plain-text part and sample JSON data for the live preview. A template can be available to every Mail Agent of the account or to one agent only. Members can create and edit templates; only the account owner can delete them.

Send it by alias (or template_id) with your data. template_* cannot be combined with subject, htmlbody or textbody:

curl https://send.example.com/v1.1/email \
  -H "Authorization: Bearer maas_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": { "address": "shop@mail.example.com", "name": "Example Shop" },
    "to": [{ "address": "somchai@example.org" }],
    "template_alias": "order-shipped",
    "template_data": {
      "name": "Somchai",
      "order": { "id": "A-1024", "total": 1290 },
      "items": [{ "title": "T-shirt", "qty": 2 }, { "title": "Cap", "qty": 1 }]
    }
  }'

Template syntax

Templates use a small, safe subset of Mustache. There are no functions, partials or code execution.

Syntax Meaning
{{ name }} Insert a value. In the HTML part it is HTML-escaped (< becomes &lt;); in the subject and text part it is inserted as is.
{{ order.total }} Value inside a nested object (items.0.title also works for list positions).
{{{ html }}} or {{& html }} Insert raw, unescaped HTML. Only use it for HTML you control.
{{#items}} … {{/items}} Section: repeated for every element of a list (each element becomes the lookup context), rendered once for any other truthy value, skipped when falsy.
{{^items}} … {{/items}} Inverted section: rendered only when the value is missing, false, null, "", an empty list or an empty object.
{{.}} The current element inside a section, e.g. a list of strings.
{{! comment }} Ignored.

Missing variables render as an empty string. Numbers are printed exactly as sent (1290, 99.5). Line breaks in a rendered subject are replaced with spaces.

Limits: subject 998 bytes (single line), HTML 512 KB, text 256 KB, template_data 256 KB, 500 templates per account, rendered output 4 MB.

Template errors

Status When
400 template_id and template_alias both set; template_* combined with subject/htmlbody/textbody; template_data without a template; template_data not a JSON object; the template rendered an empty body.
404 template not found — unknown id/alias, another account’s template, or a template scoped to a different Mail Agent.

Scheduled sending

Add send_at (RFC 3339, at least 1 minute and at most 30 days in the future) to send later:

{
  "from": { "address": "news@mail.example.com" },
  "to": [{ "address": "user@example.org" }],
  "subject": "Your weekly summary",
  "textbody": "…",
  "send_at": "2026-12-24T09:00:00+07:00"
}

The message is validated and credits are charged when it is accepted; the response has "status": "scheduled" and echoes send_at (UTC). Recipients stay in the scheduled state and are handed to the delivery queue only once send_at is reached. The 72-hour delivery retry window starts at send_at.

Cancel a scheduled message: POST /v1.1/email/{message_id}/cancel

Requires the email:send scope. Cancels every recipient that is still scheduled and refunds their credits (a refund entry appears in the credit ledger). The dashboard has the same Cancel send button on the log detail page.

curl -X POST https://send.example.com/v1.1/email/7f3a1c2e-8b4d-4f6a-9a10-32f9e0a1b2c3/cancel \
  -H "Authorization: Bearer maas_live_YOUR_API_KEY"
{
  "message_id": "7f3a1c2e-8b4d-4f6a-9a10-32f9e0a1b2c3",
  "status": "cancelled",
  "cancelled": 3,
  "credits_refunded": 3
}
Status When
200 Cancelled.
404 Unknown message or a message of another Mail Agent.
409 not_cancellable: the message has already started sending, was delivered, or was cancelled before.

Batch sending: POST /v1.1/email/batch

Send up to 100 messages in one request. Every message has the same shape as the body of POST /v1.1/email (templates, send_at and list_unsubscribe included) plus an optional per-message idempotency_key. Each message is validated, charged and queued independently: one invalid message never blocks the others.

{
  "messages": [
    {
      "from": { "address": "shop@mail.example.com" },
      "to": [{ "address": "a@example.org" }],
      "template_alias": "order-shipped",
      "template_data": { "name": "A", "order": { "id": "A-1" } },
      "idempotency_key": "order-A-1-shipped"
    },
    {
      "from": { "address": "shop@mail.example.com" },
      "to": [{ "address": "b@example.org" }],
      "subject": "Hello",
      "textbody": "Hi B"
    }
  ]
}

The response is 200 OK when every message was accepted and 207 Multi-Status when at least one failed. results keeps the order of messages:

{
  "request_id": "0d9b…",
  "accepted": 1,
  "failed": 1,
  "results": [
    { "index": 0, "status": "accepted", "id": "7f3a1c2e-…", "message_status": "queued", "credits_used": 1 },
    { "index": 1, "status": "error", "http_status": 402, "error": "insufficient_credits", "details": { "balance": 0, "required": 1 } }
  ]
}

Per-item fields: index, status (accepted / error), and for accepted items id, message_status, credits_used, deduplicated (replayed idempotency_key) and send_at; for failed items http_status (the status the single-message endpoint would have returned), error, optional details and retry_after (seconds, for 429).

Rate limits are recipient-weighted exactly as for separate calls: every message consumes as many tokens of the Mail Agent’s per-minute limit as it has recipients, so a batch may be partly accepted and partly rejected with 429. The whole request body is limited like a single message (about 1.33 × the maximum message size).

One-click unsubscribe (RFC 8058)

Gmail and Yahoo require bulk/marketing senders to support one-click unsubscribe. When it is enabled, MailCraft adds two headers to every recipient’s copy before DKIM signing (both are covered by the DKIM signature):

List-Unsubscribe: <https://send.example.com/u/AbC…token>
List-Unsubscribe-Post: List-Unsubscribe=One-Click

The link is unique per recipient and signed, so it cannot be guessed or altered. A POST to it (what Gmail’s “Unsubscribe” button sends) immediately adds the address to the account’s suppression list with reason unsubscribe; opening the link in a browser shows a Thai/English confirmation page. Later messages to that address from any Mail Agent of the account are recorded as suppressed and not charged. These suppressions are shown in Email logs → Suppression list and cannot be removed by the account (the recipient opted out).

How to turn it on:

Where How
Mail Agent default Dashboard → Mail Agents → One-click unsubscribe link. Off by default (transactional mail such as OTPs and receipts should not have it).
REST API, per message "list_unsubscribe": true or false overrides the agent default.
SMTP, per message Add the header X-MailCraft-List-Unsubscribe: on (or off). It is removed before the message is stored and delivered. Other values are rejected.

If your message already contains its own List-Unsubscribe header (via mime_headers or SMTP), MailCraft keeps yours and does not add its own.