Skip to Content

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

ArgumentTypeDescription
mailboxIdstringMailbox ULID identifier. The path's {mailboxId}.

Options

The last argument, options, a MessagesListOptions, takes:

OptionTypeSent asDescription
labelstringquery labelOnly messages carrying this label (folder)
excludeRoles('inbox' | 'sent' | 'drafts' | 'trash' | 'archive' | 'junk')[]query excludeRolesSystem label roles to exclude: 'inbox', 'sent', 'drafts', 'trash', 'archive', 'junk' (e.g. 'trash,junk'). Unknown roles return 400 validation_failed.
cursorstringquery cursorOpaque page cursor from a previous response using the same sortBy. Arrival cursors must be canonical uppercase ULIDs; the cursor message need not still exist.
limitnumberquery limitPage size (max 200)
hasFlagsstringquery hasFlagsComma-separated flag names; a message matches only if ALL are set (e.g. flagged)
notFlagsstringquery notFlagsComma-separated flag names; a message matches only if NONE are set (e.g. seen, for unread-only)
order('asc' | 'desc')query orderListing 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 sortBySort 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 statelive (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

StatusWhat it means
400validation_failed, label_not_applicable (label filter with state=expunged), or invalid_cursor (a malformed cursor or one from a different sortBy)
401unauthorized: the bearer credential is missing, malformed, expired, or revoked. Carries a WWW-Authenticate: Bearer challenge
404Mailbox not found or not accessible to this principal, or unknown_label (the label filter names no existing label)
503temporary_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 | MessagesListResultOption2

MessagesListResultOption1

/** 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.