> ## Documentation Index
> Fetch the complete documentation index at: https://docs.omnicommerce.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Connected mailboxes

> Search and read your connected Gmail or Outlook mailbox, and save reply drafts, from the API, SDKs and CLI.

Work through your own connected mailbox in code: find unread supplier emails, pull order numbers out of message bodies, or draft replies for review. The endpoints read the same Gmail and Outlook accounts as the assistant's email tools, connected under Account → Email.

Nothing is ever sent through the API. Drafts are saved in your mailbox; review and send them from Gmail or Outlook, or ask the assistant to send in chat, where you approve each email.

## Authentication

| Endpoint | Required scope |
| - | - |
| `GET /api/v1/email/accounts` | `email:read` |
| `GET /api/v1/email/search` | `email:read` |
| `GET /api/v1/email/messages/{messageId}` | `email:read` |
| `GET /api/v1/email/threads/{threadId}` | `email:read` |
| `POST /api/v1/email/drafts/query` | `email:read` |
| `POST /api/v1/email/drafts` | `email:write` |

A mailbox belongs to one person, so these endpoints need a credential that acts for you:

* **CLI login.** `omni auth login` creates a key bound to your user. Logins made before these scopes existed must run `omni auth login` again.
* **Your own chat sandbox.** Code you run in an assistant chat receives a read-only `OMNI_API_KEY` for your mailboxes during that run. Saving a draft through the API requires a signed-in session or CLI login.
* **A signed-in session**, with `organizationId` in the query or JSON body.

Organization API keys act for no person and get `403`, as do Applications and Computer previews, which serve other viewers. You also need the integration permission the assistant's email tools require in your organization role.

## Choose an account

```http theme={null}
GET /api/v1/email/accounts
```

Returns each connected account's `connectionId`, `emailAddress`, `provider` (`gmail` or `outlook`), `isDefault` and `status`. Pass `connectionId` to the other endpoints to use an account other than your default. A `connectionId` that is not one of your own accounts is refused.

## Search and page through a mailbox

```http theme={null}
GET /api/v1/email/search?query=from:supplier.com newer_than:30d&readState=unread&maxResults=50
```

| Parameter | Description |
| - | - |
| `query` | Gmail search operators (`from:`, `subject:`, `newer_than:`) or free text for Outlook. |
| `readState` | `all`, `unread` or `read`. Unread is not the same as unreplied. |
| `maxResults` | 1–50, default 20. |
| `pageToken` | `nextPageToken` from the previous page. |
| `connectionId` | Optional account from `GET /api/v1/email/accounts`. |

Keep `query`, `readState`, `maxResults` and `connectionId` the same while you pass each `nextPageToken` back as `pageToken`, until it is `null`. A page can be empty while more pages remain. A token used with different search arguments is refused.

```python theme={null}
import os
from omnicommerce import Omni

# Reads OMNI_API_KEY. A chat sandbox also injects OMNI_API_URL; pass it.
client = Omni(base_url=os.environ.get("OMNI_API_URL"))
messages, page = [], None
while True:
    result = client.email.search(
        query="from:supplier.com newer_than:30d",
        read_state="unread",
        max_results=50,
        page_token=page,
    )["data"]
    messages += result["results"]
    page = result["nextPageToken"]
    if not page:
        break
```

```typescript theme={null}
import { OmniClient } from "@omni-commerce/sdk";

const client = new OmniClient({ apiKey: process.env.OMNI_API_KEY });
const page = await client.email.search({ query: "is:unread", maxResults: 50 });
```

```bash theme={null}
omni email search --query "is:unread" --max-results 50
```

## Read a message

```http theme={null}
GET /api/v1/email/messages/{messageId}
```

Returns the headers, the plain-text body (at most 12,000 characters) and attachment metadata. Use an `id` from search results, encoded as one path segment; the SDKs encode it for you. Outlook ids can contain `/`.

## Read a conversation

Use a `threadId` from Search Emails and the same `connectionId`. Call `client.email.getThread({ threadId, connectionId })` in TypeScript or `client.email.get_thread(thread_id=thread_id, connection_id=connection_id)` in Python. The conversation includes each message's headers, plain-text body and read and draft state.

## Save a draft

```http theme={null}
POST /api/v1/email/drafts
```

```json theme={null}
{
  "to": "buyer@example.com",
  "subject": "Re: Order 1042",
  "body": "Thanks for your order. It ships tomorrow.",
  "replyToMessageId": "18c2f0a9b7e4d123"
}
```

Pass `replyToMessageId` (and `threadId` for Gmail) to draft the reply in its conversation. When the provider saved a draft whose threading could not be verified, the request fails and `data.unverifiedDraftId` names the draft so you can find or discard it.

## Inspect saved drafts

Call `client.email.getDrafts({ connectionId, draftIds })` in TypeScript or `client.email.get_drafts(connection_id=connection_id, draft_ids=draft_ids)` in Python. Supply the original mailbox connection ID and up to 20 exact draft IDs. Inspect `data.results` for each distinct draft, including drafts that were already sent, moved or discarded.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.