---
name: emit-email
description: Send transactional email (order confirmations, password resets, one-time codes) with the Xenkloud Email Service API. Use when the user wants an app to send email through emitemail.
---

# Sending email with Xenkloud Email Service

Xenkloud Email Service is a transactional email API with hard-cap pricing: at the plan cap, sending pauses and the user is never billed overage.

## Setup the user must do (you cannot do this for them)

1. Create a Xenkloud account (https://console.xenkloud.com) and add a sender domain under Email Service, Domains.
2. Add the DNS records emitemail shows: the SPF include `include:spf.emitemail.com` in the single root SPF record, a DMARC record, and the DKIM CNAME `emit._domainkey.<domain>`.
3. Wait for the domain to show Verified, then create an API key for that domain.
4. Store the key as the environment variable `EMIT_API_KEY`. Never put it in code, logs or git.

## Send

```bash
curl -X POST https://app.emitemail.com/api/v1/emails \
  -H "Authorization: Bearer $EMIT_API_KEY" \
  -H "Idempotency-Key: order-1042-confirmation" \
  -H "Content-Type: application/json" \
  -d '{
    "from": "Shop <orders@yourdomain.com>",
    "to": "customer@example.com",
    "subject": "Your order is confirmed",
    "html": "<p>Thanks for your order.</p>",
    "text": "Thanks for your order."
  }'
```

Node (fetch):

```js
const res = await fetch("https://app.emitemail.com/api/v1/emails", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.EMIT_API_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": `order-${orderId}-confirmation`,
  },
  body: JSON.stringify({ from, to, subject, html, text }),
});
const body = await res.json(); // 202 { id, status } or { error, message }
```

## Behaviour to get right

- `from` must be on a verified domain, and the key must belong to that domain.
- Always send `html` and `text` when you can.
- Reuse the same `Idempotency-Key` when retrying the same message. Same key with a different message returns 422.
- Retry only on network errors, 5xx and 409. Never retry other 4xx.
- 429 `monthly_cap_reached` or `daily_limit_reached` means nothing was sent. Stop and tell the user.
- 202 means accepted by Xenkloud's mail server, not delivered. Delivery status appears in the console under Email Service, Logs.
- Recipients that previously bounced or complained are skipped; the response lists them under `suppressed`.
- Up to 50 recipients per request, each counted toward the cap.
- A key belongs to one brand (a separate sender with its own domains and limits). 429 `brand_budget_reached` means the owner's budget on that brand is used up: stop and tell the user.

## Webhooks

- Events: `email.delivered`, `email.deferred`, `email.bounced` (with `data.bounce_type`), `email.complained`.
- Verify `emit-signature` (`t=<unix>,v1=<hex>`): HMAC-SHA256 of `<t>.<raw body>` with the endpoint secret, and reject if `t` is older than 5 minutes.
- Answer 2xx within 5 seconds. Failures retry after 30s, 2m, 10m, 1h, 6h with the same event `id`, so de-duplicate on `emit-event-id`.
- Events with `"test": true` are not real mail.

## Do not

- Send marketing or bulk newsletters through the Email Service.
- Invent recipient addresses or send to addresses the user did not provide.
- Expose the API key in client-side code, only call the API from a server.
