Skip to the documentation
Go

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

Scopesthreads:readPages through results
Signature
List(ctx context.Context, opts ...nextmail.RequestOption) (*nextmail.Page, error)

Returns 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

nextmail.WithQstring

Searches the titles.

nextmail.WithSortstring

newest (the default) or oldest by the last change, or title. A cursor carries on in the order it was handed out in.

nextmail.WithLimitint

Page size, from 1 to 100. The server defaults to 25.

nextmail.WithCursorstring

The NextCursor of the previous page. Leave it out for the first page.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The *nextmail.Page with Items, HasMore and NextCursor. Each item has id, title, kind, messageCount and updatedAt.

Example

page, err := client.Chats.List(ctx, nextmail.WithQ("invoices"))if err != nil {	return err} for _, chat := range page.Items {	fmt.Println(chat.String("title"), chat.Int("messageCount"))} fmt.Println(page.HasMore, page.NextCursor)

Notes

Also available in

API
GET /chats
TypeScript
chats.list()
Python
chats.list()
Ruby
chats.list
PHP
chats->list
Java
chats().list
C#
Chats.ListAsync

Chats.ListAll

Collect every chat into one slice

Scopesthreads:readPages through results
Signature
ListAll(ctx context.Context, opts ...nextmail.RequestOption) ([]nextmail.Object, error)

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

Parameters

nextmail.WithQstring

Searches the titles.

nextmail.WithSortstring

newest (the default) or oldest by the last change, or title. A cursor carries on in the order it was handed out in.

nextmail.WithLimitint

Page size for each request, from 1 to 100. The server defaults to 25.

nextmail.WithCursorstring

Starts the walk after this cursor instead of the first page.

nextmail.WithAPIKeystring

Overrides the client API key for every page of this walk.

Returns

A []nextmail.Object holding every chat.

Example

chats, err := client.Chats.ListAll(ctx)if err != nil {	return err} for _, chat := range chats {	fmt.Println(chat.String("id"), chat.String("object"))}

Notes

  • If any page fails the call fails and the rows already fetched are discarded.

Also available in

API
GET /chats
TypeScript
chats.listAll()
Python
chats.list_all()
Ruby
chats.list_all
PHP
chats->listAll
Java
chats().listAll
C#
Chats.ListAllAsync

Chats.Iterate

Stream the chats one at a time

Scopesthreads:readPages through results
Signature
Iterate(ctx context.Context, opts ...nextmail.RequestOption) *nextmail.Iterator

Returns an iterator 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

nextmail.WithQstring

Searches the titles.

nextmail.WithSortstring

newest (the default) or oldest by the last change, or title. A cursor carries on in the order it was handed out in.

nextmail.WithLimitint

Page size for each request, from 1 to 100. The server defaults to 25.

nextmail.WithCursorstring

Starts the walk after this cursor instead of the first page.

nextmail.WithAPIKeystring

Overrides the client API key for every page of this walk.

Returns

The *nextmail.Iterator yielding one chat per step.

Example

for chat, err := range client.Chats.Iterate(ctx, nextmail.WithSort("title")).All() {	if err != nil {		return err	} 	fmt.Println(chat.String("title"))}

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
PHP
chats->iterate
Java
chats().iterate
C#
Chats.IterateAsync

Chats.Get

Retrieve a chat

Scopesthreads:read
Signature
Get(ctx context.Context, id string, opts ...nextmail.RequestOption) (nextmail.Object, error)

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.

Parameters

idstringRequired

The chat id from List.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The nextmail.Object with id, title, kind, messageCount and updatedAt.

Example

chat, err := client.Chats.Get(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65")if err != nil {	return err} fmt.Println(chat.String("title"))

Notes

  • An unknown id is a 404.

Also available in

API
GET /chats/{id}
TypeScript
chats.get()
Python
chats.get()
Ruby
chats.get
PHP
chats->get
Java
chats().get
C#
Chats.GetAsync

Chats.Rename

Rename a chat

Scopesthreads:write
Signature
Rename(ctx context.Context, id string, title string, opts ...nextmail.RequestOption) (nextmail.Object, error)

Gives a chat with the assistant a new title, up to 120 characters, and returns 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

idstringRequired

The chat id from List.

titlestringRequired

The new title, from 1 to 120 characters.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The nextmail.Object with the new title.

Example

chat, err := client.Chats.Rename(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65", "Quarterly invoices")if err != nil {	return err} fmt.Println(chat.String("id"), chat.String("object"))

Notes

  • An unknown id is a 404.

Also available in

API
PATCH /chats/{id}
TypeScript
chats.rename()
Python
chats.rename()
Ruby
chats.rename
PHP
chats->rename
Java
chats().rename
C#
Chats.RenameAsync

Chats.Delete

Delete a chat

Scopesthreads:write
Signature
Delete(ctx context.Context, id string, opts ...nextmail.RequestOption) (nextmail.Object, error)

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

idstringRequired

The chat id from List.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The nextmail.Object with object: 'chat', the id and deleted: true.

Example

chat, err := client.Chats.Delete(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65")if err != nil {	return err} fmt.Println(chat.String("id"), chat.String("object"))

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
PHP
chats->delete
Java
chats().delete
C#
Chats.DeleteAsync

Chats.ListMessages

List the messages of a chat

Scopesthreads:readPages through results
Signature
ListMessages(ctx context.Context, id string, opts ...nextmail.RequestOption) (*nextmail.Page, error)

Returns one page of the messages of one chat with the assistant, from the first unless nextmail.WithOrder 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

idstringRequired

The chat id from List.

nextmail.WithOrderstring

asc (the default) reads the chat from its first message, and desc from its newest.

nextmail.WithLimitint

Page size, from 1 to 50. The server defaults to 20.

nextmail.WithCursorstring

The NextCursor of the previous page. Leave it out for the first page.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The *nextmail.Page with Items, HasMore and NextCursor. Each item has id, chatId, position, role, createdAt, text and parts.

Example

page, err := client.Chats.ListMessages(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65", nextmail.WithOrder(constants.ChatMessageOrdersDesc), nextmail.WithLimit(10))if err != nil {	return err} for _, message := range page.Items {	fmt.Println(message.String("role"), message.String("text"))} fmt.Println(page.HasMore, page.NextCursor)

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

API
GET /chats/{id}/messages
TypeScript
chats.listMessages()
Python
chats.list_messages()
Ruby
chats.list_messages
PHP
chats->listMessages
Java
chats().listMessages
C#
Chats.ListMessagesAsync

Chats.ListAllMessages

Collect every message of a chat into one slice

Scopesthreads:readPages through results
Signature
ListAllMessages(ctx context.Context, id string, opts ...nextmail.RequestOption) ([]nextmail.Object, error)

Walks every page of ListMessages and returns with every message of the chat, in the order nextmail.WithOrder names. One request per page, with the same options on each.

Parameters

idstringRequired

The chat id from List.

nextmail.WithOrderstring

asc (the default) reads the chat from its first message, and desc from its newest.

nextmail.WithLimitint

Page size for each request, from 1 to 50. The server defaults to 20.

nextmail.WithCursorstring

Starts the walk after this cursor instead of the first page.

nextmail.WithAPIKeystring

Overrides the client API key for every page of this walk.

Returns

A []nextmail.Object holding every message of the chat.

Example

messages, err := client.Chats.ListAllMessages(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65")if err != nil {	return err} for _, message := range messages {	fmt.Println(message.String("role")+":", message.String("text"))}

Notes

  • If any page fails the call fails and the rows already fetched are discarded.

Also available in

API
GET /chats/{id}/messages
TypeScript
chats.listAllMessages()
Python
chats.list_all_messages()
Ruby
chats.list_all_messages
PHP
chats->listAllMessages
Java
chats().listAllMessages
C#
Chats.ListAllMessagesAsync

Chats.IterateMessages

Stream the messages of a chat one at a time

Scopesthreads:readPages through results
Signature
IterateMessages(ctx context.Context, id string, opts ...nextmail.RequestOption) *nextmail.Iterator

Returns an iterator that yields one message at a time, in the order nextmail.WithOrder 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

idstringRequired

The chat id from List.

nextmail.WithOrderstring

asc (the default) reads the chat from its first message, and desc from its newest.

nextmail.WithLimitint

Page size for each request, from 1 to 50. The server defaults to 20.

nextmail.WithCursorstring

Starts the walk after this cursor instead of the first page.

nextmail.WithAPIKeystring

Overrides the client API key for every page of this walk.

Returns

The *nextmail.Iterator yielding one message per step.

Example

for message, err := range client.Chats.IterateMessages(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65").All() {	if err != nil {		return err	} 	for _, part := range message.Objects("parts") {		if part.String("type") == constants.ChatMessagePartTypesToolCall {			fmt.Println(part.String("toolName"), part.String("status"))		}	}}

Notes

  • The iterator is lazy, so an abandoned loop costs only the pages you consumed.

Also available in

API
GET /chats/{id}/messages
TypeScript
chats.iterateMessages()
Python
chats.iterate_messages()
Ruby
chats.iterate_messages
PHP
chats->iterateMessages
Java
chats().iterateMessages
C#
Chats.IterateMessagesAsync

Chats.SendMessage

Send a message to the assistant

Scopesthreads:write
Signature
SendMessage(ctx context.Context, id string, body nextmail.Body, opts ...nextmail.RequestOption) (nextmail.Object, error)

Says 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 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

idstringRequired

The chat id from List, or a new id of at most 64 characters to start a chat.

contentstring

What you say to the assistant, up to 32,000 characters once trimmed. Required unless regenerate is true.

regeneratebool

True answers your last message again instead of sending a new one. It takes no content and no messageId.

messageIdstring

An id for your message, unique in this chat, up to 64 characters. Left out, one is made up.

attachments[]nextmail.Body

Up to 4 files for the assistant to read: a map with fileId for a file in Files, or a map with filename, content and contentType for a text file of at most 256 KiB, its content as bytes or a base64 string (bytes are encoded for you).

threadIdstring

A conversation the chat is about. It is pinned to the chat with its first message, and the assistant reads it before it answers.

templateIdstring

A template the assistant should work on, as the template editor of the app passes it.

nextmail.WithTimeouttime.Duration

How long to wait for the turn to end. Left out, it is 5 minutes, or the client timeout when that is longer. 0 waits as long as it takes.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The nextmail.Object 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

turn, err := client.Chats.SendMessage(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65", nextmail.Body{	"content": "Which invoices came in this week?",})if err != nil {	return err} if turn.String("status") == constants.ChatTurnStatusesAwaitingApproval {	for _, call := range turn.Objects("pendingToolCalls") {		fmt.Println("Waiting on", call.String("toolName"), call.Object("args"))	} 	return nil} if messages := turn.Objects("messages"); len(messages) > 0 {	fmt.Println(messages[len(messages)-1].String("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 409 approval_pending. A messageId already in the chat is 422 invalid_parameter on messageId.

  • An id that belongs to a task run, or regenerate in 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_exceeded before anything is saved. When AI is unavailable on the server it is 409 ai_not_configured, and when the assistant cannot be reached 503 assistant_unavailable, with nothing sent.

  • The SDK does not retry it, since a lost attempt may already have reached the assistant. Send a messageId to make a second attempt safe: a message the chat already holds is refused instead of said twice.

Also available in

API
POST /chats/{id}/messages
TypeScript
chats.sendMessage()
Python
chats.send_message()
Ruby
chats.send_message
PHP
chats->sendMessage
Java
chats().sendMessage
C#
Chats.SendMessageAsync

Chats.StopTurn

Stop the turn running in a chat

Scopesthreads:write
Signature
StopTurn(ctx context.Context, id string, body nextmail.Body, opts ...nextmail.RequestOption) (nextmail.Object, error)

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 turnId in the body 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

idstringRequired

The chat id from List.

turnIdstring

Stops the turn only while it is this one, as turnId of a chat turn names it. Left out, whatever runs in the chat is stopped.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The nextmail.Object 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

stop, err := client.Chats.StopTurn(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65", nil)if err != nil {	return err} fmt.Println(stop.String("status"), stop.String("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

API
POST /chats/{id}/stop
TypeScript
chats.stopTurn()
Python
chats.stop_turn()
Ruby
chats.stop_turn
PHP
chats->stopTurn
Java
chats().stopTurn
C#
Chats.StopTurnAsync

Chats.ApproveToolCall

Approve a tool call

Scopesthreads:write
Signature
ApproveToolCall(ctx context.Context, id string, toolCallID string, body nextmail.Body, opts ...nextmail.RequestOption) (nextmail.Object, error)

Lets the assistant make a call it asked to make, with the arguments the call shows and any args in the body 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

idstringRequired

The chat id from List.

toolCallIDstringRequired

The call, as pendingToolCalls of the last chat turn names it.

argsnextmail.Body

Arguments to change before the call runs, laid over the ones the assistant chose. Left out, the call runs as asked.

nextmail.WithTimeouttime.Duration

How long to wait for the assistant to carry on. Left out, it is 5 minutes, or the client timeout when that is longer. 0 waits as long as it takes.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The nextmail.Object, the turn once the assistant has carried on, with status, messages, pendingToolCalls and error.

Example

turn, err := client.Chats.SendMessage(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65", nextmail.Body{	"content": "Archive the newsletters from last month",})if err != nil {	return err} for _, call := range turn.Objects("pendingToolCalls") {	next, err := client.Chats.ApproveToolCall(ctx, turn.String("chatId"), call.String("toolCallId"), nil)	if err != nil {		return err	} 	fmt.Println(next.String("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 503 assistant_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_busy while the assistant carries on, and a 404 once the call no longer waits.

Also available in

API
POST /chats/{id}/tool-calls/{toolCallId}/approve
TypeScript
chats.approveToolCall()
Python
chats.approve_tool_call()
Ruby
chats.approve_tool_call
PHP
chats->approveToolCall
Java
chats().approveToolCall
C#
Chats.ApproveToolCallAsync

Chats.DenyToolCall

Deny a tool call

Scopesthreads:write
Signature
DenyToolCall(ctx context.Context, id string, toolCallID string, opts ...nextmail.RequestOption) (nextmail.Object, error)

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 call 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

idstringRequired

The chat id from List.

toolCallIDstringRequired

The call, as pendingToolCalls of the last chat turn names it.

nextmail.WithTimeouttime.Duration

How long to wait for the assistant to carry on. Left out, it is 5 minutes, or the client timeout when that is longer. 0 waits as long as it takes.

nextmail.WithAPIKeystring

Overrides the client's API key for this call only.

Returns

The nextmail.Object, the turn once the assistant has carried on, with the call marked denied in its messages.

Example

turn, err := client.Chats.DenyToolCall(ctx, "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65", "call_7Hq2Xb9Lm4", nextmail.WithTimeout(10*time.Minute))if err != nil {	return err} fmt.Println(turn.String("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 503 assistant_unavailable.

  • The SDK does not retry it. A second attempt after a lost response is 409 chat_busy while the assistant carries on, and a 404 once the call no longer waits.

Also available in

API
POST /chats/{id}/tool-calls/{toolCallId}/deny
TypeScript
chats.denyToolCall()
Python
chats.deny_tool_call()
Ruby
chats.deny_tool_call
PHP
chats->denyToolCall
Java
chats().denyToolCall
C#
Chats.DenyToolCallAsync