# Train spam filter from a message

Submits spam or ham training feedback for a specific email message to tune the spam filter. Idempotent per message and class: if this message was already taught this class — by an earlier call, or by the user moving it into or out of Junk (which trains on its own after a short undo window) — the call answers 202 without sending it again, and it supersedes a move-triggered sample still waiting out that window. The opposite class is always sent.

## `POST /api/v1/mailboxes/{mailboxId}/messages/{messageId}/learn`

### Path parameters

- `mailboxId` (string, required) — Mailbox ULID identifier
- `messageId` (string, required) — Message ULID identifier

### Request body (required)

`application/json`

- `class` ("spam" | "ham", required) — Feedback classification ('spam' for junk, 'ham' for legitimate)

### Responses

**202** — Feedback accepted and queued for spam filter training

`application/json` → object

- `status` (string, required)

**401** — unauthorized: the bearer credential is missing, malformed, expired, or revoked. Carries a `WWW-Authenticate: Bearer` challenge

`application/json` → ErrorBody

**404** — Mailbox or message not found

`application/json` → ErrorBody

**413** — too_large: JSON request body exceeds 1 MiB or 500,000 values (object keys included), checked before parsing

`application/json` → ErrorBody

**503** — learning_unavailable — spam filter service is not configured. Or temporary_failure: the platform's storage could not answer just now; retry after the Retry-After window. Any operation can answer this. A write that got it may have been applied, so retry it with the same idempotency key where it takes one, or read the state back first (see Transient failures in the API description)

`application/json` → ErrorBody

### Example request

```bash
curl -X POST 'https://api.open.email/api/v1/mailboxes/{mailboxId}/messages/{messageId}/learn' \
  -H 'Authorization: Bearer oek_…' \
  -H 'Content-Type: application/json' \
  -d '{
    "class": "spam"
  }'
```

## TypeScript SDK

[`openemail.messages.learn`](/docs/sdks/typescript/messages/learn)

```ts
const result = await openemail.messages.learn(mailboxId, messageId, { class: 'spam' })
```
