---
title: "nextmail.chats"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://nextmail.sh/docs/python/reference/chats"
area: "Python"
category: "Reference"
---

# nextmail.chats

Every method in this namespace: its signature, its parameters, what it returns and an example.

## Methods

The conversations held with the assistant in the app: list and search them, read one, rename it and delete it. Each person reaches only their own.

### `chats.list()`

List the assistant chats

```python
def list(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    q: str | None = None,
    sort: ChatSort | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Page[ChatResource]
```

Returns one page of the conversations held with the assistant in the app, newest first, as its Chats page lists them, each with its title, how many messages it holds and when it last changed.

Each person reaches only their own chats: an API key reaches the chats of the workspace owner, and an access token reaches the chats of the person who connected it. A key or token limited to particular addresses or domains is 422 `capability_unsupported`.

Scopes: `threads:read`.

**Parameters**

- `limit` (`int`): Page size, from 1 to 100. The server defaults to 25.
- `cursor` (`str`): The `nextCursor` of the previous page. Leave it out for the first page.
- `q` (`str`): Searches the titles.
- `sort` (`ChatSort`): `newest` (the default) or `oldest` by the last change, or `title`. A cursor carries on in the order it was handed out in.
- `api_key` (`str`): Overrides the client's API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Page[ChatResource]`, a dict with `items`, `hasMore` and `nextCursor`. Each item has `id`, `title`, `kind`, `messageCount` and `updatedAt`.

**Example**

```python
from nextmail import nextmail

page = nextmail.chats.list(q='invoices', limit=10)

for chat in page['items']:
    print(chat['title'], chat['messageCount'], chat['updatedAt'])

print('More to read:', page['hasMore'])
```

**Notes**

- A cursor that names nothing in this list is 400 `invalid_cursor`.

Also available in: API [`GET /chats`](https://nextmail.sh/docs/api/reference/chats#get-chats); TypeScript [`nextmail.chats.list()`](https://nextmail.sh/docs/sdk/reference/chats#list); Ruby [`chats.list`](https://nextmail.sh/docs/ruby/reference/chats#list); PHP [`chats->list`](https://nextmail.sh/docs/php/reference/chats#list); Go [`Chats.List`](https://nextmail.sh/docs/go/reference/chats#list); Java [`chats().list`](https://nextmail.sh/docs/java/reference/chats#list); C# [`Chats.ListAsync`](https://nextmail.sh/docs/csharp/reference/chats#list); CLI [`nextmail chats list`](https://nextmail.sh/docs/cli/reference/chats#chats-list).

### `chats.list_all()`

Collect every chat into one list

```python
def list_all(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    q: str | None = None,
    sort: ChatSort | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> builtins.list[ChatResource]
```

Walks every page of `list` and returns every chat in one list, in the order `sort=` names. One request per page, with the same filters on each.

Scopes: `threads:read`.

**Parameters**

- `limit` (`int`): Page size for each request, from 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk after this cursor instead of the first page.
- `q` (`str`): Searches the titles.
- `sort` (`ChatSort`): `newest` (the default) or `oldest` by the last change, or `title`. A cursor carries on in the order it was handed out in.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`list[ChatResource]` holding every chat.

**Example**

```python
from nextmail import nextmail

chats = nextmail.chats.list_all()

print(len(chats), 'chats holding', sum(chat['messageCount'] for chat in chats), 'messages')
```

**Notes**

- If any page fails, the call raises and the rows already fetched are discarded.

Also available in: API [`GET /chats`](https://nextmail.sh/docs/api/reference/chats#get-chats); TypeScript [`nextmail.chats.listAll()`](https://nextmail.sh/docs/sdk/reference/chats#listAll); Ruby [`chats.list_all`](https://nextmail.sh/docs/ruby/reference/chats#listAll); PHP [`chats->listAll`](https://nextmail.sh/docs/php/reference/chats#listAll); Go [`Chats.ListAll`](https://nextmail.sh/docs/go/reference/chats#listAll); Java [`chats().listAll`](https://nextmail.sh/docs/java/reference/chats#listAll); C# [`Chats.ListAllAsync`](https://nextmail.sh/docs/csharp/reference/chats#listAll).

### `chats.iterate()`

Stream the chats one at a time

```python
def iterate(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    q: str | None = None,
    sort: ChatSort | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Iterator[ChatResource]
```

Returns a generator that yields one chat at a time, in the order `sort=` names, and requests the next page only once the current one is drained. Nothing is fetched until you loop over it, and breaking out of the loop stops the requests.

Scopes: `threads:read`.

**Parameters**

- `limit` (`int`): Page size for each request, from 1 to 100. The server defaults to 25.
- `cursor` (`str`): Starts the walk after this cursor instead of the first page.
- `q` (`str`): Searches the titles.
- `sort` (`ChatSort`): `newest` (the default) or `oldest` by the last change, or `title`. A cursor carries on in the order it was handed out in.
- `api_key` (`str`): Overrides the client API key for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Iterator[ChatResource]`, a generator yielding one chat per step.

**Example**

```python
from nextmail import nextmail

for chat in nextmail.chats.iterate(sort='title'):
    print(chat['title'], chat['messageCount'])
```

**Notes**

- Each page is one request, so a long walk makes many.

Also available in: API [`GET /chats`](https://nextmail.sh/docs/api/reference/chats#get-chats); TypeScript [`nextmail.chats.iterate()`](https://nextmail.sh/docs/sdk/reference/chats#iterate); Ruby [`chats.iterate`](https://nextmail.sh/docs/ruby/reference/chats#iterate); PHP [`chats->iterate`](https://nextmail.sh/docs/php/reference/chats#iterate); Go [`Chats.Iterate`](https://nextmail.sh/docs/go/reference/chats#iterate); Java [`chats().iterate`](https://nextmail.sh/docs/java/reference/chats#iterate); C# [`Chats.IterateAsync`](https://nextmail.sh/docs/csharp/reference/chats#iterate).

### `chats.get()`

Retrieve a chat

```python
def get(
    id: str,
    *,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ChatResource
```

Returns one chat with the assistant by its id: its title, how many messages it holds and when it last changed.

Each person reaches only their own chats: an API key reaches the chats of the workspace owner, and an access token reaches the chats of the person who connected it. A key or token limited to particular addresses or domains is 422 `capability_unsupported`.

Scopes: `threads:read`.

**Parameters**

- `id` (`str`, required): The chat id from `list`.
- `api_key` (`str`): Overrides the client's API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ChatResource` with `id`, `title`, `kind`, `messageCount` and `updatedAt`.

**Example**

```python
from nextmail import nextmail

chat = nextmail.chats.get('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65')

print(chat['title'], chat['messageCount'], chat['updatedAt'])
```

**Notes**

- An unknown id is a 404.

Also available in: API [`GET /chats/{id}`](https://nextmail.sh/docs/api/reference/chats#get-chats-id); TypeScript [`nextmail.chats.get()`](https://nextmail.sh/docs/sdk/reference/chats#get); Ruby [`chats.get`](https://nextmail.sh/docs/ruby/reference/chats#get); PHP [`chats->get`](https://nextmail.sh/docs/php/reference/chats#get); Go [`Chats.Get`](https://nextmail.sh/docs/go/reference/chats#get); Java [`chats().get`](https://nextmail.sh/docs/java/reference/chats#get); C# [`Chats.GetAsync`](https://nextmail.sh/docs/csharp/reference/chats#get); CLI [`nextmail chats get`](https://nextmail.sh/docs/cli/reference/chats#chats-get).

### `chats.rename()`

Rename a chat

```python
def rename(
    id: str,
    title: str,
    *,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ChatResource
```

Gives a chat with the assistant a new title, up to 120 characters, and returns the chat as it is now.

Each person reaches only their own chats: an API key reaches the chats of the workspace owner, and an access token reaches the chats of the person who connected it. A key or token limited to particular addresses or domains is 422 `capability_unsupported`.

Scopes: `threads:write`.

**Parameters**

- `id` (`str`, required): The chat id from `list`.
- `title` (`str`, required): The new title, from 1 to 120 characters.
- `api_key` (`str`): Overrides the client's API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ChatResource` with the new `title`.

**Example**

```python
from nextmail import nextmail

chat = nextmail.chats.rename('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', 'Quarterly invoices')

print(chat['id'], chat['title'])
```

**Notes**

- An unknown id is a 404.
- Retried automatically on network failure, since the same title sent twice changes nothing.

Also available in: API [`PATCH /chats/{id}`](https://nextmail.sh/docs/api/reference/chats#patch-chats-id); TypeScript [`nextmail.chats.rename()`](https://nextmail.sh/docs/sdk/reference/chats#rename); Ruby [`chats.rename`](https://nextmail.sh/docs/ruby/reference/chats#rename); PHP [`chats->rename`](https://nextmail.sh/docs/php/reference/chats#rename); Go [`Chats.Rename`](https://nextmail.sh/docs/go/reference/chats#rename); Java [`chats().rename`](https://nextmail.sh/docs/java/reference/chats#rename); C# [`Chats.RenameAsync`](https://nextmail.sh/docs/csharp/reference/chats#rename); CLI [`nextmail chats rename`](https://nextmail.sh/docs/cli/reference/chats#chats-rename).

### `chats.delete()`

Delete a chat

```python
def delete(
    id: str,
    *,
    api_key: str | None = None,
    timeout: float | None = None,
) -> DeletedChatResource
```

Deletes a chat with the assistant and every message in it, for good. Nothing the assistant did in the mailbox is undone.

Each person reaches only their own chats: an API key reaches the chats of the workspace owner, and an access token reaches the chats of the person who connected it. A key or token limited to particular addresses or domains is 422 `capability_unsupported`.

Scopes: `threads:write`.

**Parameters**

- `id` (`str`, required): The chat id from `list`.
- `api_key` (`str`): Overrides the client's API key for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`DeletedChatResource` with `object` set to `chat`, the `id` and `deleted` set to `True`.

**Example**

```python
from nextmail import NextmailApiError, nextmail

try:
    removed = nextmail.chats.delete('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65')
except NextmailApiError as error:
    if not error.is_not_found:
        raise

    print('That chat is already gone')
else:
    print(removed['id'], removed['deleted'])
```

**Notes**

- An unknown id is a 404, and so is a second call for the same chat.
- The SDK does not retry it.

Also available in: API [`DELETE /chats/{id}`](https://nextmail.sh/docs/api/reference/chats#delete-chats-id); TypeScript [`nextmail.chats.delete()`](https://nextmail.sh/docs/sdk/reference/chats#delete); Ruby [`chats.delete`](https://nextmail.sh/docs/ruby/reference/chats#delete); PHP [`chats->delete`](https://nextmail.sh/docs/php/reference/chats#delete); Go [`Chats.Delete`](https://nextmail.sh/docs/go/reference/chats#delete); Java [`chats().delete`](https://nextmail.sh/docs/java/reference/chats#delete); C# [`Chats.DeleteAsync`](https://nextmail.sh/docs/csharp/reference/chats#delete); CLI [`nextmail chats delete`](https://nextmail.sh/docs/cli/reference/chats#chats-delete).
