# messages.update

Update message flags and labels.

Updates message flags, adds or removes labels, and modifies custom keywords. Removing a message's LAST label expunges it to the Recently-deleted tier (restorable until purgeAfter) — it does not attach the Trash label. A label write that leaves the message carrying only Trash is a delete: its previous filing is recorded as restoresTo, and POST /messages/untrash reverses it. One that leaves it carrying only Junk is a spam report, recorded the same way and reversed by POST /messages/unjunk.

```ts
openemail.messages.update(
  mailboxId: string,
  messageId: string,
  body?: MessagesUpdateBody,
  options?: CallOptions,
): ApiPromise<MessageUpdateResult>
```

It calls `PATCH /mailboxes/{mailboxId}/messages/{messageId}`: [Update message flags and labels](/docs/api/messages/patch_MessageUpdate) in the API reference.

## Example

```ts
const messageUpdateResult = await openemail.messages.update(mailboxId, messageId, {})
```

## Arguments

| Argument | Type | Description |
| --- | --- | --- |
| `mailboxId` | `string` | Mailbox ULID identifier. The path's `{mailboxId}`. |
| `messageId` | `string` | Message ULID identifier. The path's `{messageId}`. |
| `body` | [`MessagesUpdateBody`](#messagesupdatebody) | Optional. The request's body. |

## Options

It takes no options of its own. Every call also takes `signal`, `timeout`, `retry` and `headers`, which say how that one call is sent ([call options](/docs/sdks/typescript/calling-methods#call-options)).

## Answer

Awaited, it is the API's JSON, [`MessageUpdateResult`](/docs/sdks/typescript/types/message-update-result). It is an [`ApiPromise`](/docs/sdks/typescript/responses): `.withResponse()` gives the same with the `Response` it came in, for its headers.

## Errors

| Status | What it means |
| --- | --- |
| `400` | validation_failed (including flagLabel without `deleted` in either flag list) or invalid label/keyword name |
| `401` | unauthorized: the bearer credential is missing, malformed, expired, or revoked. Carries a `WWW-Authenticate: Bearer` challenge |
| `404` | Mailbox or message not found |
| `409` | not_in_label — flagLabel names a label the message is not filed in; nothing written |
| `413` | too_large: JSON request body exceeds 1 MiB or 500,000 values (object keys included), checked before parsing |
| `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`](/docs/sdks/typescript/errors): its `status`, and its `code`, the API's `error`. Decide on the code: one status covers several.

## Types

### MessagesUpdateBody

```ts
/** The body of `messages.update`. */
type MessagesUpdateBody = {
  /** The label (IMAP mailbox) the client had selected. When present, `deleted` in `flagsSet`/`flagsClear` marks the message's membership in THAT label only, leaves the message-wide bit untouched, and every other flag in the same patch keeps its message-wide meaning. Label-scoped listings (`/labels/{label}/uids`, `/labels/{label}/messages`, the label-filtered search) report this per-membership bit, `deletedCount` counts it, and `POST /labels/{label}/expunge` detaches exactly the memberships carrying it. A label the message is not filed in answers not_in_label with nothing written. Requires `deleted` in one of the two flag lists (validation_failed otherwise). Without `flagLabel`, `deleted` means every folder: the message-wide bit (JMAP's visibility rule; JMAP has no `\Deleted`) and every membership move together. A scoped clear that unmarks the last marked membership clears the message-wide bit as well. */
  flagLabel?: string
  /** Flags to remove */
  flagsClear?: ('seen' | 'answered' | 'flagged' | 'draft' | 'deleted')[]
  /** Flags to set */
  flagsSet?: ('seen' | 'answered' | 'flagged' | 'draft' | 'deleted')[]
  /** Custom keywords to remove */
  keywordsClear?: string[]
  /** Exact replacement list of custom keywords */
  keywordsExact?: string[]
  /** Custom keywords to add */
  keywordsSet?: string[]
  /** Labels to add */
  labelsAdd?: string[]
  /** Exact replacement list of labels */
  labelsExact?: string[]
  /** Labels to remove */
  labelsRemove?: string[]
  mime?: unknown
  /** What to do if this write would leave the message with no label. 'expunge' (the default) sends the message to Recently deleted. 'archive' keeps the message live by attaching the Archive system label (attaching Archive needs no right beyond the grant's 't' for a scoped viewer). If the write removes Archive itself and would leave no labels, Archive is kept without error. A SCOPED viewer whose write lands the message in Archive has moved it out of their own scope: the answer is still `updated`, and the hydrated message carries an EMPTY `labels` list rather than naming a label the grant never included. `labelsAdded` omits Archive for the same reason. */
  orphans?: ('expunge' | 'archive')
}
```

It names these types of the API, each on a page of its own: [`MessageUpdateResult`](/docs/sdks/typescript/types/message-update-result).
