messages.list
List mailbox messages.
Lists messages newest-first (or oldest-first with order=asc) with cursor pagination, optionally filtered to one label. sortBy=date orders by the Date header instead of arrival time. Set state=expunged to list trash/restorable messages. For UID-ordered listing use GET /mailboxes/:id/labels/:label/messages.
openemail.messages.list(
mailboxId: string,
options?: MessagesListOptions,
): PagePromise<MessagesListResult, MessageMeta | ExpungedMessageMeta>It calls GET /mailboxes/{mailboxId}/messages: List mailbox messages in the API reference.
Example
for await (const messageMeta of openemail.messages.list(mailboxId)) {
// every item of every page (await the call instead for one page)
}Arguments
| Argument | Type | Description |
|---|---|---|
mailboxId | string | Mailbox ULID identifier. The path's {mailboxId}. |
Options
The last argument, options, a MessagesListOptions, takes:
| Option | Type | Sent as | Description |
|---|---|---|---|
label | string | query label | Only messages carrying this label (folder) |
excludeRoles | ('inbox' | 'sent' | 'drafts' | 'trash' | 'archive' | 'junk')[] | query excludeRoles | System label roles to exclude: 'inbox', 'sent', 'drafts', 'trash', 'archive', 'junk' (e.g. 'trash,junk'). Unknown roles return 400 validation_failed. |
cursor | string | query cursor | Opaque page cursor from a previous response using the same sortBy. Arrival cursors must be canonical uppercase ULIDs; the cursor message need not still exist. |
limit | number | query limit | Page size (max 200) |
hasFlags | string | query hasFlags | Comma-separated flag names; a message matches only if ALL are set (e.g. flagged) |
notFlags | string | query notFlags | Comma-separated flag names; a message matches only if NONE are set (e.g. seen, for unread-only) |
order | ('asc' | 'desc') | query order | Listing direction: desc (default) = newest first, asc = oldest first. Hold it constant across a cursor crawl — cursors do not encode direction, so switching mid-crawl re-windows the page rather than erroring |
sortBy | ('arrival' | 'date') | query sortBy | Sort key: arrival (default) = the order mail reached this mailbox (message id); date = the Date header, falling back to arrival where it is absent or unparseable (RFC 5256 SORT DATE). Cursors follow the key, so hold it constant across a crawl — unlike order, a mismatched cursor is refused with 400 invalid_cursor rather than silently re-windowing. Note that under arrival an imported message (APPEND with ?internaldate=) lists where it was imported, not at its receivedAt; date orders imported mail by its own header |
state | ('live' | 'expunged') | query state | live (default): ordinary listing, including messages on the Trash LABEL. expunged: the Recently-deleted TIER — a different place, whose rows have no labels at all and are purged after purgeAfter — carrying expungedAt/purgeAfter, the labels held at expunge, and restoresTo |
Every call also takes signal, timeout, retry and headers, which say how that one call is sent (call options).
Answer
It is a listing, a PagePromise. Awaited, it is one page as the API answers it, MessagesListResult. Iterated with for await, it is every MessageMeta | ExpungedMessageMeta of every page, each page fetched as the loop reaches it.
Errors
| Status | What it means |
|---|---|
400 | validation_failed, label_not_applicable (label filter with state=expunged), or invalid_cursor (a malformed cursor or one from a different sortBy) |
401 | unauthorized: the bearer credential is missing, malformed, expired, or revoked. Carries a WWW-Authenticate: Bearer challenge |
404 | Mailbox not found or not accessible to this principal, or unknown_label (the label filter names no existing label) |
503 | 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) |
A failed call rejects with a RequestError: its status, and its code, the API's error. Decide on the code: one status covers several.
Types
MessagesListResult
/** What `messages.list` answers. */
type MessagesListResult = MessagesListResultOption1 | MessagesListResultOption2MessagesListResultOption1
/** The API's `MessagesListResultOption1`, as the document describes it. */
type MessagesListResultOption1 = {
messages: MessageMeta[]
/** Opaque cursor for the next page; absent on the last page */
nextCursor?: string
}MessagesListResultOption2
/** The API's `MessagesListResultOption2`, as the document describes it. */
type MessagesListResultOption2 = {
messages: ExpungedMessageMeta[]
/** Opaque cursor for the next page; absent on the last page */
nextCursor?: string
}It names these types of the API, each on a page of its own: ExpungedMessageMeta, MessageMeta.