# Classify existing emails with the AI label rules

Source: https://docs.maylee.app/api-reference/ai-label-rules/classify-existing-emails

`POST https://api.maylee.app/api/v1/ai-label-rules/backfill`

Scope: `write`.

Rules only apply to emails synchronized after their creation. This queues one background job per active mailbox (or the given one) that classifies the `limit` most recent INBOX emails with every rule of the workspace.

Labels land asynchronously: they appear in the `tags` of each email once it is classified. A backfill applies labels and nothing else: no auto-forward, auto-draft or auto-reply is triggered. Each classified email costs one model call.

## Request body

`application/json`, optional.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `limit` | integer | no | Number of most recent INBOX emails to classify, per mailbox. From 1 to 500. Default: `200`. |
| `mailboxId` | string | no | Only this mailbox. Default: every active mailbox. 1 to n characters. |

## Example request

```bash
curl -X POST "https://api.maylee.app/api/v1/ai-label-rules/backfill" \
  -H "Authorization: Bearer $MAYLEE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "limit": 200
}'
```

## Responses

### 202

Jobs queued.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `jobs` | integer | yes | Number of background jobs queued, one per mailbox. |
| `mailboxIds` | array of string | yes |  |

```json
{
  "jobs": 1,
  "mailboxIds": [
    "cmbx1a2b3c4d5e6f7g8h9i0j"
  ]
}
```

### 400

Invalid payload, or the workspace has no AI label rule.

### 401

Missing, malformed, revoked or expired API key.

### 403

Key lacks the required scope, or outbound sending is not enabled on it.

### 404

`MAILBOX_NOT_FOUND`: the mailbox is not in this workspace.

### 429

Per-minute rate limit (`RATE_LIMIT_EXCEEDED`) or monthly quota (`QUOTA_EXCEEDED`) exhausted.

Errors use the format `{ "code", "error", "details" }`: see [Errors](/errors.md).
