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 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(options?: ChatListOptions): Promise<Page<ChatResource>>Resolves 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
options.qstringSearches the titles.
options.sortChatSortnewest(the default) oroldestby the last change, ortitle. A cursor carries on in the order it was handed out in.options.limitnumberPage size, from 1 to 100. The server defaults to 25.
options.cursorstringThe
nextCursorof the previous page. Leave it out for the first page.options.signalAbortSignalCancels the request.
options.apiKeystringOverrides the client's API key for this call only.
Returns
Page<ChatResource> with items, hasMore and nextCursor. Each item has id, title, kind, messageCount and updatedAt.
Example
const page = await nextmail.chats.list({ q: 'invoices' }) for (const chat of page.items) console.log(chat.title, chat.messageCount)Notes
A cursor that names nothing in this list is 400
invalid_cursor.
Also available in
- API
GET /chats- Python
chats.list()- Ruby
chats.list- PHP
chats->list- Go
Chats.List- Java
chats().list- C#
Chats.ListAsync
chats.listAll()
Collect every chat into one array
listAll(options?: ChatListOptions): Promise<Array<ChatResource>>Walks every page of list and resolves with every chat, in the order sort names. One request per page, with the same filters on each.
Parameters
options.qstringSearches the titles.
options.sortChatSortnewest(the default) oroldestby the last change, ortitle. A cursor carries on in the order it was handed out in.options.limitnumberPage size for each request, from 1 to 100. The server defaults to 25.
options.cursorstringStarts the walk after this cursor instead of the first page.
options.signalAbortSignalCancels the request in flight and the walk with it.
options.apiKeystringOverrides the client API key for every page of this walk.
Returns
Array<ChatResource> holding every chat.
Example
const chats = await nextmail.chats.listAll() console.log(chats.length)Notes
If any page fails the promise rejects and the rows already fetched are discarded.
Also available in
chats.iterate()
Stream the chats one at a time
iterate(options?: ChatListOptions): AsyncGenerator<ChatResource, void, undefined>Returns an async 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
options.qstringSearches the titles.
options.sortChatSortnewest(the default) oroldestby the last change, ortitle. A cursor carries on in the order it was handed out in.options.limitnumberPage size for each request, from 1 to 100. The server defaults to 25.
options.cursorstringStarts the walk after this cursor instead of the first page.
options.signalAbortSignalCancels the request in flight and the walk with it.
options.apiKeystringOverrides the client API key for every page of this walk.
Returns
AsyncGenerator<ChatResource, void, undefined> yielding one chat per step.
Example
for await (const chat of nextmail.chats.iterate({ sort: 'title' })) console.log(chat.title)Notes
Each page is one request, so a long walk makes many.
Also available in
chats.get()
Retrieve a chat
get(id: string, options?: RequestScope): Promise<ChatResource>Resolves 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.options.signalAbortSignalCancels the request.
options.apiKeystringOverrides the client's API key for this call only.
Returns
ChatResource with id, title, kind, messageCount and updatedAt.
Example
const chat = await nextmail.chats.get('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65') console.log(chat.title)Notes
An unknown id is a 404.
Also available in
- API
GET /chats/{id}- Python
chats.get()- Ruby
chats.get- PHP
chats->get- Go
Chats.Get- Java
chats().get- C#
Chats.GetAsync
chats.rename()
Rename a chat
rename(id: string, title: string, options?: RequestScope): Promise<ChatResource>Gives a chat with the assistant a new title, up to 120 characters, and resolves with 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.
options.signalAbortSignalCancels the request.
options.apiKeystringOverrides the client's API key for this call only.
Returns
ChatResource with the new title.
Example
await nextmail.chats.rename('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', 'Quarterly invoices')Notes
An unknown id is a 404.
Also available in
chats.delete()
Delete a chat
delete(id: string, options?: RequestScope): Promise<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.
Parameters
idstringRequiredThe chat id from
list.options.signalAbortSignalCancels the request.
options.apiKeystringOverrides the client's API key for this call only.
Returns
DeletedChatResource with object: 'chat', the id and deleted: true.
Example
await nextmail.chats.delete('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65')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
chats.listMessages()
List the messages of a chat
listMessages(id: string, options?: ChatMessageListOptions): Promise<Page<ChatMessageResource>>Resolves 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. listAllMessages collects every page and 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
list.options.orderChatMessageOrderasc(the default) reads the chat from its first message, anddescfrom its newest.options.limitnumberPage size, from 1 to 50. The server defaults to 20.
options.cursorstringThe
nextCursorof the previous page. Leave it out for the first page.options.signalAbortSignalCancels the request.
options.apiKeystringOverrides the client's API key for this call only.
Returns
Page<ChatMessageResource> with items, hasMore and nextCursor. Each item has id, chatId, position, role, createdAt, text and parts.
Example
const page = await nextmail.chats.listMessages('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', { order: 'desc', limit: 10 }) for (const message of page.items) console.log(message.role, message.text)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(id: string, options?: ChatMessageListOptions): Promise<Array<ChatMessageResource>>Walks every page of listMessages and resolves with every message of the chat, in the order order names. One request per page, with the same options on each.
Parameters
idstringRequiredThe chat id from
list.options.orderChatMessageOrderasc(the default) reads the chat from its first message, anddescfrom its newest.options.limitnumberPage size for each request, from 1 to 50. The server defaults to 20.
options.cursorstringStarts the walk after this cursor instead of the first page.
options.signalAbortSignalCancels the request in flight and the walk with it.
options.apiKeystringOverrides the client API key for every page of this walk.
Returns
Array<ChatMessageResource> holding every message of the chat.
Example
const messages = await nextmail.chats.listAllMessages('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65') console.log(messages.map((message) => `${message.role}: ${message.text}`).join('\n'))Notes
If any page fails the promise rejects and the rows already fetched are discarded.
Also available in
chats.iterateMessages()
Stream the messages of a chat one at a time
iterateMessages(id: string, options?: ChatMessageListOptions): AsyncGenerator<ChatMessageResource, void, undefined>Returns an async 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
list.options.orderChatMessageOrderasc(the default) reads the chat from its first message, anddescfrom its newest.options.limitnumberPage size for each request, from 1 to 50. The server defaults to 20.
options.cursorstringStarts the walk after this cursor instead of the first page.
options.signalAbortSignalCancels the request in flight and the walk with it.
options.apiKeystringOverrides the client API key for every page of this walk.
Returns
AsyncGenerator<ChatMessageResource, void, undefined> yielding one message per step.
Example
for await (const message of nextmail.chats.iterateMessages('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65')) { for (const part of message.parts) { if (part.type === 'tool_call') console.log(part.toolName, part.status) }}Notes
The generator is lazy, so an abandoned loop costs only the pages you consumed.
Also available in
chats.sendMessage()
Send a message to the assistant
sendMessage(id: string, body: ChatMessageSend, options?: ChatTurnOptions): Promise<ChatTurnResource>Says something in the chat and resolves 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 approveToolCall or 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 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
list, or a new id of at most 64 characters to start a chat.body.contentstringWhat you say to the assistant, up to 32,000 characters once trimmed. Required unless
regenerateis true.body.regeneratebooleanTrue answers your last message again instead of sending a new one. It takes no
contentand nomessageId.body.messageIdstringAn id for your message, unique in this chat, up to 64 characters. Left out, one is made up.
body.attachmentsArray<ChatAttachmentInput>Up to 4 files for the assistant to read:
{ fileId }for a file in Files, or{ filename, content, contentType }for a text file of at most 256 KiB, its content in base64.body.threadIdstringA conversation the chat is about. It is pinned to the chat with its first message, and the assistant reads it before it answers.
body.templateIdstringA template the assistant should work on, as the template editor of the app passes it.
options.timeoutMsnumberHow long to wait for the turn to end, in milliseconds. Left out, it is 5 minutes, or the client
timeoutMswhen that is longer.0waits as long as it takes.options.signalAbortSignalStops waiting for the answer. The turn goes on in the chat, so call
stopTurnto stop it.options.apiKeystringOverrides the client's API key for this call only.
Returns
ChatTurnResource with object: 'chat_turn', chatId, turnId, status (completed, awaiting_approval, stopped or failed), stopReason, messages (your message and the replies to it), pendingToolCalls and error, which says why a failed turn failed.
Example
const turn = await nextmail.chats.sendMessage('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', { content: 'Which invoices came in this week?'}) if (turn.status === 'awaiting_approval') { for (const call of turn.pendingToolCalls) console.log('Waiting on', call.toolName, call.args)} else { console.log(turn.messages.at(-1)?.text)}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(id: string, body?: ChatTurnStop, options?: RequestScope): Promise<ChatTurnStopResource>Stops 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 body.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
list.body.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.options.signalAbortSignalCancels the request.
options.apiKeystringOverrides the client's API key for this call only.
Returns
ChatTurnStopResource with object: '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.
Example
const stop = await nextmail.chats.stopTurn('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65') console.log(stop.status, stop.turnId)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
chats.approveToolCall()
Approve a tool call
approveToolCall(id: string, toolCallId: string, body?: ChatToolCallApproval, options?: ChatTurnOptions): Promise<ChatTurnResource>Lets the assistant make a call it asked to make, with the arguments the call shows and any body.args laid over them, and resolves 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
list.toolCallIdstringRequiredThe call, as
pendingToolCallsof the last chat turn names it.body.argsRecord<string, unknown>Arguments to change before the call runs, laid over the ones the assistant chose. Left out, the call runs as asked.
options.timeoutMsnumberHow long to wait for the assistant to carry on, in milliseconds. Left out, it is 5 minutes, or the client
timeoutMswhen that is longer.0waits as long as it takes.options.signalAbortSignalStops waiting for the answer. The turn goes on in the chat, so call
stopTurnto stop it.options.apiKeystringOverrides the client's API key for this call only.
Returns
ChatTurnResource, the turn once the assistant has carried on, with status, messages, pendingToolCalls and error.
Example
const turn = await nextmail.chats.sendMessage('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', { content: 'Archive the newsletters from last month' }) for (const call of turn.pendingToolCalls) { const next = await nextmail.chats.approveToolCall(turn.chatId, call.toolCallId) console.log(next.status)}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(id: string, toolCallId: string, options?: ChatTurnOptions): Promise<ChatTurnResource>Refuses 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 promise resolves 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
list.toolCallIdstringRequiredThe call, as
pendingToolCallsof the last chat turn names it.options.timeoutMsnumberHow long to wait for the assistant to carry on, in milliseconds. Left out, it is 5 minutes, or the client
timeoutMswhen that is longer.0waits as long as it takes.options.signalAbortSignalStops waiting for the answer. The turn goes on in the chat, so call
stopTurnto stop it.options.apiKeystringOverrides the client's API key for this call only.
Returns
ChatTurnResource, the turn once the assistant has carried on, with the call marked denied in its messages.
Example
const turn = await nextmail.chats.denyToolCall('8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65', 'call_7Hq2Xb9Lm4') console.log(turn.status, turn.messages.at(-1)?.text)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.