$client->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 and its messages, say something and get the reply, stop a reply, approve or deny the tool calls the assistant asks to make, rename a chat and delete it. Each person reaches only their own.
chats->list
List the assistant chats
list( ?string $q = null, ?string $sort = null, ?int $limit = null, ?string $cursor = null, ?string $apiKey = null,): PageReturns one page of the conversations held with the assistant in the app, newest first, as its assistant panel 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.
Parameters
qstringSearches the titles.
sortstringnewest(the default) oroldestby the last change, ortitle, as inNextmail\Constants\ChatSorts. A cursor carries on in the order it was handed out in.limitintPage size, from 1 to 100. The server defaults to 25.
cursorstringThe
nextCursorof the previous page. Leave it out for the first page.apiKeystringOverrides the client's API key for this call only.
Returns
A Page of arrays with items, hasMore and nextCursor. Each item has id, title, kind, messageCount and updatedAt.
Example
use Nextmail\Constants\ChatSorts; $page = $client->chats->list(q: 'invoices', sort: ChatSorts::NEWEST, limit: 20); foreach ($page as $chat) { echo $chat['title'], ' (', $chat['messageCount'], ' messages)', PHP_EOL;} if ($page->hasMore) { $next = $client->chats->list(q: 'invoices', cursor: $page->nextCursor); echo $next->items[0]['title'] ?? 'nothing more', PHP_EOL;}Notes
A cursor that names nothing in this list is 400
invalid_cursor.
Also available in
- API
GET /chats- TypeScript
chats.list()- Python
chats.list()- Ruby
chats.list- Go
Chats.List- Java
chats().list- C#
Chats.ListAsync
chats->listAll
Collect every chat into one array
listAll( ?string $q = null, ?string $sort = null, ?int $limit = null, ?string $cursor = null, ?string $apiKey = null,): arrayWalks every page of list and returns every chat, in the order sort: names. One request per page, with the same filters on each.
Parameters
qstringSearches the titles.
sortstringnewest(the default) oroldestby the last change, ortitle, as inNextmail\Constants\ChatSorts. A cursor carries on in the order it was handed out in.limitintPage size for each request, from 1 to 100. The server defaults to 25.
cursorstringStarts the walk after this cursor instead of the first page.
apiKeystringOverrides the client API key for every page of this walk.
Returns
A list of arrays holding every chat.
Example
$chats = $client->chats->listAll(q: 'invoices'); echo implode(PHP_EOL, array_column($chats, 'title')), PHP_EOL;Notes
If any page fails the call throws and the rows already fetched are discarded.
Also available in
- API
GET /chats- TypeScript
chats.listAll()- Python
chats.list_all()- Ruby
chats.list_all- Go
Chats.ListAll- Java
chats().listAll- C#
Chats.ListAllAsync
chats->iterate
Stream the chats one at a time
iterate( ?string $q = null, ?string $sort = null, ?int $limit = null, ?string $cursor = null, ?string $apiKey = null,): GeneratorReturns 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 consume it, and breaking out of the loop stops the requests.
Parameters
qstringSearches the titles.
sortstringnewest(the default) oroldestby the last change, ortitle, as inNextmail\Constants\ChatSorts. A cursor carries on in the order it was handed out in.limitintPage size for each request, from 1 to 100. The server defaults to 25.
cursorstringStarts the walk after this cursor instead of the first page.
apiKeystringOverrides the client API key for every page of this walk.
Returns
A Generator that yields one chat array per step.
Example
use Nextmail\Constants\ChatSorts; foreach ($client->chats->iterate(sort: ChatSorts::TITLE) as $chat) { echo $chat['title'], ' ', $chat['updatedAt'], PHP_EOL;}Notes
Each page is one request, so a long walk makes many.
Also available in
- API
GET /chats- TypeScript
chats.iterate()- Python
chats.iterate()- Ruby
chats.iterate- Go
Chats.Iterate- Java
chats().iterate- C#
Chats.IterateAsync
chats->get
Retrieve a chat
get(string $id, ?string $apiKey = null): arrayReturns 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.
Parameters
idstringRequiredThe chat id from
list.apiKeystringOverrides the client's API key for this call only.
Returns
An array with id, title, kind, messageCount and updatedAt.
Example
$chat = $client->chats->get('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65'); echo $chat['title'], ', last changed ', $chat['updatedAt'], PHP_EOL;Notes
An unknown id is a 404.
Also available in
- API
GET /chats/{id}- TypeScript
chats.get()- Python
chats.get()- Ruby
chats.get- Go
Chats.Get- Java
chats().get- C#
Chats.GetAsync
chats->rename
Rename a chat
rename(string $id, string $title, ?string $apiKey = null): arrayGives 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.
Parameters
idstringRequiredThe chat id from
list.titlestringRequiredThe new title, from 1 to 120 characters.
apiKeystringOverrides the client's API key for this call only.
Returns
The same array as chats->get, with the new title.
Example
$chat = $client->chats->rename('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', 'Quarterly invoices'); echo $chat['title'], PHP_EOL;Notes
An unknown id is a 404.
Also available in
- API
PATCH /chats/{id}- TypeScript
chats.rename()- Python
chats.rename()- Ruby
chats.rename- Go
Chats.Rename- Java
chats().rename- C#
Chats.RenameAsync
chats->delete
Delete a chat
delete(string $id, ?string $apiKey = null): arrayDeletes 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.
Parameters
idstringRequiredThe chat id from
list.apiKeystringOverrides the client's API key for this call only.
Returns
An array with object set to chat, the id and deleted set to true.
Example
use Nextmail\Exception\NotFoundException; try { $client->chats->delete('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65');} catch (NotFoundException) { echo 'Already gone', PHP_EOL;}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}- TypeScript
chats.delete()- Python
chats.delete()- Ruby
chats.delete- Go
Chats.Delete- Java
chats().delete- C#
Chats.DeleteAsync
chats->listMessages
List the messages of a chat
listMessages( string $id, ?string $order = null, ?int $limit = null, ?string $cursor = null, ?string $apiKey = null,): PageReturns one page of the messages of one chat with the assistant, from the first unless order: is desc: what was said, the tools the assistant called with their results, and the files that came with them. chats->listAllMessages collects every page and chats->iterateMessages walks them lazily.
Each message has text, its words alone, and parts, the message in order: text parts, tool_call parts with the tool, its arguments, its result and its status, and file parts. A tool result the key or app may not read, because it lacks a scope the tool needs, reads as a redacted sentence instead, and so does the answer of a tool that needs no scope for an app acting for a member. The tool, its arguments and its status stay.
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.
Parameters
idstringRequiredThe chat id from
chats->list.orderstringasc(the default) reads the chat from its first message, anddescfrom its newest, as inNextmail\Constants\ChatMessageOrders.limitintPage size, from 1 to 50. The server defaults to 20.
cursorstringThe
nextCursorof the previous page. Leave it out for the first page.apiKeystringOverrides the client's API key for this call only.
Returns
A Page of arrays with items, hasMore and nextCursor. Each item has id, chatId, position, role, createdAt, text and parts. The roles are in Nextmail\Constants\ChatMessageRoles and the kinds of part in Nextmail\Constants\ChatMessagePartTypes.
Example
use Nextmail\Constants\ChatMessageOrders; $page = $client->chats->listMessages('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', order: ChatMessageOrders::DESC, limit: 10); foreach ($page as $message) { echo $message['role'], ': ', $message['text'], PHP_EOL;}Notes
An unknown chat id is a 404.
A cursor that does not come from this list, or one handed out before the chat changed under it, is 400
invalid_cursor. Start again without one.
Also available in
chats->listAllMessages
Collect every message of a chat into one array
listAllMessages( string $id, ?string $order = null, ?int $limit = null, ?string $cursor = null, ?string $apiKey = null,): arrayWalks every page of chats->listMessages and returns every message of the chat, in the order order: names. One request per page, with the same filters on each.
Parameters
idstringRequiredThe chat id from
chats->list.orderstringasc(the default) reads the chat from its first message, anddescfrom its newest, as inNextmail\Constants\ChatMessageOrders.limitintPage size for each request, from 1 to 50. The server defaults to 20.
cursorstringStarts the walk after this cursor instead of the first page.
apiKeystringOverrides the client API key for every page of this walk.
Returns
A list of arrays holding every message of the chat.
Example
$messages = $client->chats->listAllMessages('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65'); foreach ($messages as $message) { echo $message['role'], ': ', $message['text'], PHP_EOL;}Notes
If any page fails the call throws and the rows already fetched are discarded.
Also available in
chats->iterateMessages
Stream the messages of a chat one at a time
iterateMessages( string $id, ?string $order = null, ?int $limit = null, ?string $cursor = null, ?string $apiKey = null,): GeneratorReturns a Generator that yields one message at a time, in the order order: names, and requests the next page only once the current one is drained. Nothing is fetched until you consume it, and breaking out of the loop stops the requests.
Parameters
idstringRequiredThe chat id from
chats->list.orderstringasc(the default) reads the chat from its first message, anddescfrom its newest, as inNextmail\Constants\ChatMessageOrders.limitintPage size for each request, from 1 to 50. The server defaults to 20.
cursorstringStarts the walk after this cursor instead of the first page.
apiKeystringOverrides the client API key for every page of this walk.
Returns
A Generator that yields one message array per step.
Example
use Nextmail\Constants\ChatMessagePartTypes; foreach ($client->chats->iterateMessages('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65') as $message) { foreach ($message['parts'] as $part) { if ($part['type'] === ChatMessagePartTypes::TOOL_CALL) { echo $part['toolName'], ' ', $part['status'], PHP_EOL; } }}Notes
The generator is lazy, so an abandoned loop costs only the pages it consumed.
Also available in
chats->sendMessage
Send a message to the assistant
sendMessage(string $id, array $body, int|float|null $timeout = null, ?string $apiKey = null): arraySays something in the chat and returns once the assistant has replied, in the same chat the app shows. An id that names no chat yet starts one. With 'regenerate' => true the assistant answers your last message again, and the reply it had is replaced.
The assistant works with the tools the scopes of the key or app reach. A tool that asks for a verification code is never offered, and a tool that changes something waits for an answer: the turn ends with status awaiting_approval and names the call in pendingToolCalls, which chats->approveToolCall or chats->denyToolCall answers. A call you asked for in so many words runs without waiting, as it does in the app.
A turn runs for at most 240 seconds. Past that it stops with stopReason time_limit and keeps what it had written, and chats->stopTurn stops it sooner. One turn runs in a chat at a time.
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.
Parameters
idstringRequiredThe chat id from
chats->list, or a new id of at most 64 characters to start a chat.contentstringWhat you say to the assistant, up to 32,000 characters once trimmed. Required unless
regenerateis true.regenerateboolTrue answers your last message again instead of sending a new one. It takes no
contentand nomessageId.messageIdstringAn id for your message, unique in this chat, up to 64 characters. Left out, one is made up.
attachmentsarrayUp to 4 files for the assistant to read, each an array:
['fileId' => $file['id']]for a file in Files, orfilename,contentandcontentTypefor a text file of at most 256 KiB, wherecontentis base64 text, or a stream resource, anSplFileInfoor a PSR-7 stream that the SDK reads and encodes for you. Acontentstring that is not base64 throws anInvalidArgumentExceptionbefore anything is sent.threadIdstringA conversation the chat is about. It is pinned to the chat with its first message, and the assistant reads it before it answers.
templateIdstringA template the assistant should work on, as the template editor of the app passes it.
timeoutint|floatHow long to wait for the turn to end, in seconds. Left out, it is 5 minutes, or the client's
timeoutwhen that is longer.0waits as long as it takes, and a negative value throws anInvalidArgumentException.apiKeystringOverrides the client's API key for this call only.
Returns
An array with object set to chat_turn, chatId, turnId, status (completed, awaiting_approval, stopped or failed, as in Nextmail\Constants\ChatTurnStatuses), stopReason, messages (your message and the replies to it), pendingToolCalls and error, which says why a failed turn failed.
Example
use Nextmail\Constants\ChatTurnStatuses; $turn = $client->chats->sendMessage('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', [ 'content' => 'Which invoices came in this week?',]); if ($turn['status'] === ChatTurnStatuses::AWAITING_APPROVAL) { foreach ($turn['pendingToolCalls'] as $call) { echo 'Waiting on ', $call['toolName'], PHP_EOL; }} else { foreach ($turn['messages'] as $message) { echo $message['role'], ': ', $message['text'], PHP_EOL; }}Notes
While the assistant is still answering in the chat, a new message is 409
chat_busy, and while a tool call waits for an answer it is 409approval_pending. AmessageIdalready in the chat is 422invalid_parameteronmessageId.An id that belongs to a task run, or
regeneratein a chat with no message from you, is a 404.Each turn spends the workspace's AI actions, and a workspace that has used them all for the day is refused with 429
ai_quota_exceededbefore anything is saved. When AI is unavailable on the server it is 409ai_not_configured, and when the assistant cannot be reached 503assistant_unavailable, with nothing sent.The SDK does not retry it, since a lost attempt may already have reached the assistant. Send a
messageIdto make a second attempt safe: a message the chat already holds is refused instead of said twice.
Also available in
chats->stopTurn
Stop the turn running in a chat
stopTurn(string $id, ?array $body = null, ?string $apiKey = null): arrayStops the assistant while it answers in the chat, in the app or through this API, and keeps what it had written. A tool call that had not returned is marked interrupted. Calling it when nothing runs is not an error: the answer is idle. Send turnId to stop the turn only while it is that one.
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.
Parameters
idstringRequiredThe chat id from
chats->list.turnIdstringStops the turn only while it is this one, as
turnIdof a chat turn names it. Left out, whatever runs in the chat is stopped.apiKeystringOverrides the client's API key for this call only.
Returns
An array with object set to chat_turn, chatId, turnId and status: stopped when a turn was running and has stopped, idle when nothing was running or another turn than turnId was, as in Nextmail\Constants\ChatStopStatuses.
Example
$stop = $client->chats->stopTurn('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65'); echo $stop['status'], ' ', $stop['turnId'] ?? 'no turn', PHP_EOL;Notes
An unknown chat id is a 404. When the assistant cannot be reached it is 503
assistant_unavailable, and nothing was stopped.The SDK does not retry it. A second call after a lost response answers
idle, since the turn has already stopped.
Also available in
- API
POST /chats/{id}/stop- TypeScript
chats.stopTurn()- Python
chats.stop_turn()- Ruby
chats.stop_turn- Go
Chats.StopTurn- Java
chats().stopTurn- C#
Chats.StopTurnAsync
chats->approveToolCall
Approve a tool call
approveToolCall( string $id, string $toolCallId, ?array $body = null, int|float|null $timeout = null, ?string $apiKey = null,): arrayLets the assistant make a call it asked to make, with the arguments the call shows and any args you send laid over them, and returns once the assistant has carried on. When the turn waits for several calls, it carries on once every one is answered, so the answer to an earlier one still has status awaiting_approval, with the calls left in pendingToolCalls.
A call of a tool this key or app may not run in a chat, such as one asked for in the app that needs a scope the key does not hold, is refused with 403 tool_not_offered before anything runs, and it keeps waiting: deny it, or approve it in the app.
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.
Parameters
idstringRequiredThe chat id from
chats->list.toolCallIdstringRequiredThe call, as
pendingToolCallsof the last chat turn names it.argsarrayArguments to change before the call runs, keyed by name and laid over the ones the assistant chose. Left out, the call runs as asked.
timeoutint|floatHow long to wait for the assistant to carry on, in seconds. Left out, it is 5 minutes, or the client's
timeoutwhen that is longer.0waits as long as it takes, and a negative value throws anInvalidArgumentException.apiKeystringOverrides the client's API key for this call only.
Returns
The same array as chats->sendMessage: the turn once the assistant has carried on, with status, messages, pendingToolCalls and error.
Example
$turn = $client->chats->sendMessage('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', ['content' => 'Archive the newsletters from last month']); foreach ($turn['pendingToolCalls'] as $call) { $next = $client->chats->approveToolCall($turn['chatId'], $call['toolCallId']); echo $call['toolName'], ': ', $next['status'], PHP_EOL;}Notes
No tool call with that id waiting for an answer is a 404. While the assistant is still answering it is 409
chat_busy, and when the assistant cannot be reached 503assistant_unavailable.The SDK does not retry it, since a lost attempt may already have run the tool. A second attempt after a lost response is 409
chat_busywhile the assistant carries on, and a 404 once the call no longer waits.
Also available in
chats->denyToolCall
Deny a tool call
denyToolCall( string $id, string $toolCallId, int|float|null $timeout = null, ?string $apiKey = null,): arrayRefuses a call the assistant asked to make. The assistant is told, and carries on without it once every call it waits for is answered, and the method returns once it has. Nothing the call described is done. It takes no body.
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.
Parameters
idstringRequiredThe chat id from
chats->list.toolCallIdstringRequiredThe call, as
pendingToolCallsof the last chat turn names it.timeoutint|floatHow long to wait for the assistant to carry on, in seconds. Left out, it is 5 minutes, or the client's
timeoutwhen that is longer.0waits as long as it takes, and a negative value throws anInvalidArgumentException.apiKeystringOverrides the client's API key for this call only.
Returns
The same array as chats->sendMessage, the turn once the assistant has carried on, with the call marked denied in its messages.
Example
$turn = $client->chats->denyToolCall('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', 'call_7Hq2Xb9Lm4'); echo $turn['status'], PHP_EOL;Notes
No tool call with that id waiting for an answer is a 404. While the assistant is still answering it is 409
chat_busy, and when the assistant cannot be reached 503assistant_unavailable.The SDK does not retry it. A second attempt after a lost response is 409
chat_busywhile the assistant carries on, and a 404 once the call no longer waits.