# Mailboxes & Sync

> List mailboxes, read counts and run incremental IMAP synchronization.

---

LLMS index: [llms.txt](/llms.txt)

---

## The mailbox object

```json
{
  "id": 1,
  "accountId": 1,
  "name": "INBOX",
  "path": "INBOX",
  "delimiter": "/",
  "attrs": ["\\Inbox"],
  "uidValidity": 1720000000,
  "uidNext": 512,
  "total": 128,
  "unread": 3,
  "lastSyncAt": 1764550000
}
```

| Field | Description |
| --- | --- |
| `id` | Mailbox ID used by the messages endpoints |
| `accountId` | Owning account |
| `name` | Display name (last path segment) |
| `path` | Full IMAP path, e.g. `INBOX`, `Archive/2026` |
| `delimiter` | Hierarchy delimiter reported by the server |
| `attrs` | IMAP attributes such as `\Inbox`, `\Sent`, `\Drafts`, `\Junk`, `\Trash` |
| `uidValidity`, `uidNext` | IMAP UID bookkeeping from the last sync |
| `total`, `unread` | Message and unseen counts from the last sync |
| `lastSyncAt` | Unix seconds of the last sync; `0` if never synced |

Mailboxes appear after the account's first sync. New accounts have none until
[sync](#synchronizing) runs.

## List mailboxes

`GET /api/accounts/{id}/mailboxes`

```bash
curl http://127.0.0.1:8787/api/accounts/1/mailboxes
```

Returns an array of mailbox objects, ordered with `INBOX` first. The `unread`
values are aggregated into the account's `unread` field.

## Synchronizing

`POST /api/accounts/{id}/sync`

```bash
# Sync the INBOX (default)
curl -X POST http://127.0.0.1:8787/api/accounts/1/sync

# Sync another mailbox by path or display name
curl -X POST http://127.0.0.1:8787/api/accounts/1/sync \
  -H 'Content-Type: application/json' \
  -d '{"mailbox": "Sent"}'
```

Each sync does the following over a fresh IMAP connection:

1. **Refreshes the mailbox list**, creating or updating every selectable
   mailbox and its counts (`STATUS`), and picks up the server's `\Sent`
   mailbox.
2. **Selects the target mailbox** (`INBOX` when the body is empty or
   `mailbox` is omitted).
3. **Resets the local cache** if `UIDVALIDITY` changed, because UIDs are no
   longer comparable.
4. **Fetches new messages** with UID-based incremental sync — only UIDs above
   the highest one stored locally.

Response:

```json
{
  "mailbox": "INBOX",
  "mailboxes": 6,
  "newMessages": 2,
  "total": 128,
  "unread": 3
}
```

| Field | Description |
| --- | --- |
| `mailbox` | Path that was synchronized |
| `mailboxes` | Selectable mailboxes seen on the server |
| `newMessages` | Messages stored locally by this sync |
| `total`, `unread` | Fresh counts for the target mailbox |

### What gets cached

- The **first sync** of a mailbox stores every message received in the last
  7 days, bodies included.
- Later syncs fetch up to 200 new messages per run. Bodies are stored when the
  batch is at most 200 messages; larger backlogs are stored newest-first and
  bodies are fetched on demand when a message is opened.
- Fetching uses `BODY.PEEK`, so syncing never marks mail as read.
- Older messages beyond the cache are **not** deleted from the server and are
  not listed locally. Opening a cached message that lacks a body triggers a
  single-message body fetch.

Sync runs only when requested — there is no background scheduler yet, so call
this endpoint (or `pigeon digest --sync`, or the MCP `sync_mailbox` tool) when
you want fresh mail.

Errors contacting or authenticating with the IMAP server return `502`; cached
mail remains readable.

## Fetching older mail

`POST /api/mailboxes/{id}/fetch-older`

```bash
curl -X POST http://127.0.0.1:8787/api/mailboxes/1/fetch-older \
  -H 'Content-Type: application/json' -d '{}'
```

The web UI calls this when you click **Load more** and the local cache is
exhausted. It backfills mail older than the oldest cached message:

- The mailbox is searched **day by day**, walking backwards from the oldest
  cached message, collecting at most 200 uncached messages per call (override
  with `{"limit": N}`, clamped to 200).
- A quiet week does **not** mean the mailbox start. The walk keeps going
  further back until it finds mail again, and only a history that is empty all
  the way down reports `hasOlder: false` — hide the button then.
- Only envelopes are stored; bodies are fetched lazily when a message is
  opened.
- Repeat calls keep rewinding day by day; each response reports whether older
  mail remains.

Response:

```json
{
  "mailbox": "INBOX",
  "newMessages": 200,
  "hasOlder": true,
  "total": 3176,
  "unread": 1698
}
```

| Field | Description |
| --- | --- |
| `mailbox` | Path that was backfilled |
| `newMessages` | Messages stored locally by this call |
| `hasOlder` | Whether more mail remains beyond what has been walked |
| `total`, `unread` | Fresh counts for the mailbox |

If the server reset the mailbox `UIDVALIDITY`, the call returns `409` — run a
regular [sync](#synchronizing) first, which rebuilds the cache.

---

Backlinks:

- [Documentation](/docs/)
- [Accounts & Autodetection](/docs/accounts/)
- [REST API Basics](/docs/api/)
- [Architecture](/docs/architecture/)
- [Search](/docs/search/)
