# How do I send email with the Flyanote API?

Send each email as one `POST /v1/messages` request: one recipient, a sender, a subject, and a body, with an `Idempotency-Key` header so that a retry never sends it twice. Flyanote answers at once with the message's id, and sends it within seconds.

This is for transactional mail: receipts, password resets, and notifications. For a first test send, step by step, start with the [quick start](https://flyanote.com/docs/quickstart).

## What does a send take?

* `from.address`: an address on one of your verified domains or your Flyanote address, or, with a test key, `test@sandbox.flyanote.com`. `from.name` is the name recipients see. [Your sending domain](https://flyanote.com/docs/domains) says how to verify one.
* `to`: one recipient. To write to several people, send one message to each. Until the account has a verified domain and credit, it sends only to its own members' verified addresses; anyone else fails as `tier0_recipient_not_member`.
* `subject`, and the body as `html`, `text`, or both. Without `text`, Flyanote makes one from `html`. Each body can be up to 256 KiB, and the whole request up to 2 MiB.
* If you want them: `reply_to`, where replies go if not to the sender, never an address on one of Flyanote's own domains (leave it out when sending from your Flyanote address: its mail replies to the mailbox you confirmed for it); `headers`, only `In-Reply-To`, `References`, and `X-Entity-Ref-ID`, to thread a reply or keep Gmail from grouping unrelated mail; and `tags`, up to 10 labels of your own that come back when you read the message.
* An API key that can send: its scopes include `messages:send`.

## How do I send one?

With your key in `FLYANOTE_API_KEY`, and your own addresses in place of the example's:

```
curl -sS https://app.flyanote.com/api/v1/messages \
  -H "Authorization: Bearer $FLYANOTE_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: receipt-1042" \
  -d '{
    "from": {"address": "receipts@example.com", "name": "Example Shop"},
    "to": "jane@example.com",
    "subject": "Your receipt for order 1042",
    "html": "<p>Thanks for your order. It ships on Monday.</p>",
    "tags": {"order": "1042"}
  }'
```

Make the `Idempotency-Key` something that names this one email for good, like your order number and what the email is for. A random UUID works too, as long as you keep it for the retries.

## What does the answer mean?

`202 Accepted`, with the message's `id` and its `status`: `queued` means accepted and sending shortly, and `suppressed` means it won't be sent and isn't charged (the message's `reason` says why). Accepted isn't delivered: read the message to see what happened next.

A `4xx` is a refusal, and nothing was sent; after a `5xx` or a timeout, retry as below, since it may have been accepted. The refusals a working integration meets:

* [sender\_not\_ready](https://flyanote.com/docs/errors#sender-not-ready) (422): the sender isn't on a domain or Flyanote address of the account that has finished setting up.
* [reply\_to\_unverified](https://flyanote.com/docs/errors#reply-to-unverified) (422): the sender is your Flyanote address, and its reply-to mailbox isn't confirmed yet, or `reply_to` isn't that mailbox.
* [reply\_to\_invalid](https://flyanote.com/docs/errors#reply-to-invalid) (422): `reply_to` is on one of Flyanote's own domains, where replies can't go.
* [validation\_failed](https://flyanote.com/docs/errors#validation-failed) (422): a field is missing or wrong, and the answer's `errors` says which.
* [insufficient\_credit](https://flyanote.com/docs/errors#insufficient-credit) (402): the account's credit doesn't cover a live send.
* [idempotency\_key\_reused](https://flyanote.com/docs/errors#idempotency-key-reused) (409): the key already names a different message.
* [rate\_limited](https://flyanote.com/docs/errors#rate-limited) (429): too many requests; wait the`Retry-After` seconds.

[API errors](https://flyanote.com/docs/errors) lists every code with the steps that fix it, and the [messages reference](https://flyanote.com/docs/api/messages) lists each one this request can answer.

## How do I retry without sending twice?

Send the same body with the same `Idempotency-Key`. Once a request with that key is accepted, a retry within 24 hours answers the first `202` again, with the header `Idempotent-Replayed: true`, and sends nothing.

* After a timeout or any `5xx`, retry with the same key and body: the message may have been accepted before the error.
* After a `4xx`, nothing was kept: fix the request, then send it again.
* After a `409`, the key already names another message, so the fixed request needs a new key.

## How do I find out what happened to it?

Read it with `GET /v1/messages/{id}`, using a key whose scopes include `messages:read`. Put the `id` from the answer in place of `msg_…`:

```
export MESSAGE_ID='msg_…'
curl -sS "https://app.flyanote.com/api/v1/messages/$MESSAGE_ID" \
  -H "Authorization: Bearer $FLYANOTE_API_KEY"
```

* Its `status` goes `queued`, `sending`, `sent`, then `delivered`, and can become `bounced` or `complained` after that.
* `failed` and `suppressed` were never sent. `reason.code` says why, in a code to branch on, and `reason.detail` in words.
* A test key's messages end as `test`: they never deliver.
* `events` lists what happened, oldest first: each event's `type` (`send`, `delivery`, `deferral`, `bounce`, `complaint`, and the rest), when (`at`), and a `code` when there is one.

When `reason.retryable` is `true`, sending the same message again may work once `reason.retry_after` seconds have passed. Read it again first, since a late answer may have moved it on, then send it as a new request with a new `Idempotency-Key`.

## How do I list what I've sent?

`GET /v1/messages` answers the newest first, 25 to a page by default and up to 100 with `limit`. Filter with `status`, `created_after`, and `created_before`. Each page's `next_cursor` goes in the next request's `cursor`, and it's `null` on the last page. Here, the bounces:

```
curl -sS "https://app.flyanote.com/api/v1/messages?status=bounced&limit=100" \
  -H "Authorization: Bearer $FLYANOTE_API_KEY"
```
