Documentation
¶
Overview ¶
Package notionagents provides a Go client for the Notion Agents API.
This SDK allows you to list agents, create and manage chat threads, and stream real-time responses from Notion Agents. It is built entirely on the Go standard library with zero external dependencies.
Getting started ¶
Create a client with your Notion API token:
client := notionagents.NewClient(notionagents.ClientOptions{
Auth: "secret_...",
})
Async chat ¶
Start a chat, poll for completion, then fetch messages:
resp, _ := client.Agents.Agent(agentID).Chat(ctx, notionagents.ChatParams{
Message: "Hello!",
})
thread := client.Agents.Agent(agentID).Thread(resp.ThreadID)
thread.Poll(ctx, nil)
messages, _ := thread.ListMessages(ctx, nil)
Streaming chat ¶
Stream responses in real time using the iterator-style StreamReader:
reader, _ := agent.Stream(ctx, notionagents.ChatStreamParams{
Message: "Summarize my week",
})
defer reader.Close()
for {
chunk, err := reader.Next()
if err == io.EOF {
break
}
// handle chunk
}
Or use the channel-based Agent.ChatStream for concurrent consumption.
Pagination ¶
Auto-paginating iterators use Go 1.23 iter.Seq2:
for agent, err := range notionagents.IterAgents(ctx, client, nil) {
// ...
}
Index ¶
- Constants
- func IsPersonalAgent(agentID string) bool
- func IterAgents(ctx context.Context, client *Client, params *AgentListParams) iter.Seq2[AgentData, error]
- func IterMessages(ctx context.Context, thread *Thread, params *ThreadMessageListParams) iter.Seq2[ThreadMessageItem, error]
- func IterThreads(ctx context.Context, agent *Agent, params *ThreadListParams) iter.Seq2[ThreadListItem, error]
- func StripLangTags(text string) string
- type Agent
- func (a *Agent) Chat(ctx context.Context, params ChatParams) (*ChatInvocationResponse, error)
- func (a *Agent) ChatStream(ctx context.Context, params ChatStreamParams) (<-chan StreamChunk, <-chan *ThreadInfo, <-chan error)
- func (a *Agent) GetThread(ctx context.Context, threadID string) (*ThreadListItem, error)
- func (a *Agent) ListThreads(ctx context.Context, params *ThreadListParams) (*ThreadListResponse, error)
- func (a *Agent) PollThread(ctx context.Context, threadID string, opts *PollThreadOptions) (*ThreadListItem, error)
- func (a *Agent) Stream(ctx context.Context, params ChatStreamParams) (*StreamReader, error)
- func (a *Agent) Thread(threadID string) *Thread
- type AgentContentPart
- type AgentData
- type AgentIcon
- type AgentListParams
- type AgentListResponse
- type AgentNotFoundError
- type AgentOperations
- type AgentVersion
- type ChatAttachmentInput
- type ChatInvocationResponse
- type ChatParams
- type ChatStreamParams
- type Client
- type ClientOptions
- type CreatedBy
- type CustomAgentAvatar
- type CustomEmoji
- type ExternalURL
- type FileURL
- type FollowUp
- type MessageParent
- type NotionAgentsError
- type PollThreadOptions
- type PollingTimeoutError
- type StreamChunk
- type StreamError
- type StreamMessage
- type StreamReader
- type Thread
- type ThreadInfo
- type ThreadListItem
- type ThreadListParams
- type ThreadListResponse
- type ThreadMessageAttachment
- type ThreadMessageItem
- type ThreadMessageListParams
- type ThreadMessageListResponse
- type ThreadNotFoundError
- type ThreadStatus
- type ToolResult
Constants ¶
const ( // PersonalAgentID is the reserved UUID for the personal agent (Notion AI). PersonalAgentID = "33333333-3333-3333-3333-333333333333" // DefaultBaseURL is the default Notion API base URL. DefaultBaseURL = "https://api.notion.com" // DefaultVersion is the default Notion API version. DefaultVersion = "2025-09-03" )
Variables ¶
This section is empty.
Functions ¶
func IsPersonalAgent ¶
IsPersonalAgent returns true if the given agent ID is the personal agent.
func IterAgents ¶
func IterAgents(ctx context.Context, client *Client, params *AgentListParams) iter.Seq2[AgentData, error]
IterAgents returns an iterator over all agents, automatically handling pagination.
func IterMessages ¶
func IterMessages(ctx context.Context, thread *Thread, params *ThreadMessageListParams) iter.Seq2[ThreadMessageItem, error]
IterMessages returns an iterator over all messages in a thread.
func IterThreads ¶
func IterThreads(ctx context.Context, agent *Agent, params *ThreadListParams) iter.Seq2[ThreadListItem, error]
IterThreads returns an iterator over all threads for an agent.
func StripLangTags ¶
StripLangTags removes <lang ...> XML tags from text.
Types ¶
type Agent ¶
type Agent struct {
ID string
Name string
Instruction *string
// contains filtered or unexported fields
}
Agent provides operations on a specific agent.
func (*Agent) Chat ¶
func (a *Agent) Chat(ctx context.Context, params ChatParams) (*ChatInvocationResponse, error)
Chat starts an async chat with the agent.
func (*Agent) ChatStream ¶
func (a *Agent) ChatStream(ctx context.Context, params ChatStreamParams) (<-chan StreamChunk, <-chan *ThreadInfo, <-chan error)
ChatStream opens a streaming chat and returns channels for chunks, thread info, and errors.
func (*Agent) ListThreads ¶
func (a *Agent) ListThreads(ctx context.Context, params *ThreadListParams) (*ThreadListResponse, error)
ListThreads returns a paginated list of threads for this agent.
func (*Agent) PollThread ¶
func (a *Agent) PollThread(ctx context.Context, threadID string, opts *PollThreadOptions) (*ThreadListItem, error)
PollThread polls a thread until it completes or fails, using exponential backoff.
func (*Agent) Stream ¶
func (a *Agent) Stream(ctx context.Context, params ChatStreamParams) (*StreamReader, error)
Stream opens a streaming chat connection and returns a StreamReader.
type AgentContentPart ¶
type AgentContentPart struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ToolCallID *string `json:"tool_call_id,omitempty"`
ToolName string `json:"tool_name,omitempty"`
Input string `json:"input,omitempty"`
Results []ToolResult `json:"results,omitempty"`
FollowUps []FollowUp `json:"follow_ups,omitempty"`
}
AgentContentPart represents a structured part of agent message content.
type AgentData ¶
type AgentData struct {
Object string `json:"object"`
ID string `json:"id"`
Name string `json:"name"`
Description *string `json:"description"`
Instruction *string `json:"instruction"`
InstructionsPageID *string `json:"instructions_page_id"`
Icon *AgentIcon `json:"icon"`
Version *AgentVersion `json:"version"`
}
AgentData represents an agent returned by the API.
func CollectAgents ¶
func CollectAgents(ctx context.Context, client *Client, params *AgentListParams) ([]AgentData, error)
CollectAgents collects all agents into a slice.
type AgentIcon ¶
type AgentIcon struct {
Type string `json:"type"`
Emoji *string `json:"emoji,omitempty"`
File *FileURL `json:"file,omitempty"`
External *ExternalURL `json:"external,omitempty"`
CustomEmoji *CustomEmoji `json:"custom_emoji,omitempty"`
CustomAgentAvatar *CustomAgentAvatar `json:"custom_agent_avatar,omitempty"`
}
AgentIcon represents an agent's icon, which can be of several types.
type AgentListParams ¶
AgentListParams configures agent listing requests.
type AgentListResponse ¶
type AgentListResponse struct {
Object string `json:"object"`
Type string `json:"type"`
Results []AgentData `json:"results"`
HasMore bool `json:"has_more"`
NextCursor *string `json:"next_cursor"`
}
AgentListResponse is the paginated response for listing agents.
type AgentNotFoundError ¶
type AgentNotFoundError struct {
AgentID string
}
AgentNotFoundError is returned when an agent cannot be found.
func (*AgentNotFoundError) Error ¶
func (e *AgentNotFoundError) Error() string
type AgentOperations ¶
type AgentOperations struct {
// contains filtered or unexported fields
}
AgentOperations provides operations on agents.
func (*AgentOperations) Agent ¶
func (a *AgentOperations) Agent(agentID string) *Agent
Agent returns an Agent handle for the given agent ID.
func (*AgentOperations) List ¶
func (a *AgentOperations) List(ctx context.Context, params *AgentListParams) (*AgentListResponse, error)
List returns a paginated list of agents.
func (*AgentOperations) Personal ¶
func (a *AgentOperations) Personal() *Agent
Personal returns an Agent handle for the personal agent.
type AgentVersion ¶
type AgentVersion struct {
ID string `json:"id"`
Number int `json:"number"`
PublishedAt string `json:"published_at"`
}
AgentVersion contains version information for an agent.
type ChatAttachmentInput ¶
type ChatAttachmentInput struct {
FileUploadID string `json:"file_upload_id"`
Name string `json:"name,omitempty"`
}
ChatAttachmentInput is used to attach files when sending a chat message.
type ChatInvocationResponse ¶
type ChatInvocationResponse struct {
Object string `json:"object"`
AgentID string `json:"agent_id"`
ThreadID string `json:"thread_id"`
Status string `json:"status"`
}
ChatInvocationResponse is returned when starting an async chat.
type ChatParams ¶
type ChatParams struct {
Message string
Attachments []ChatAttachmentInput
ThreadID string
}
ChatParams configures a chat request.
type ChatStreamParams ¶
type ChatStreamParams struct {
Message string
Attachments []ChatAttachmentInput
ThreadID string
Verbose *bool
OnMessage func(message StreamMessage)
}
ChatStreamParams configures a streaming chat request.
type Client ¶
type Client struct {
Agents *AgentOperations
// contains filtered or unexported fields
}
Client is the Notion Agents API client.
func NewClient ¶
func NewClient(opts ClientOptions) *Client
NewClient creates a new Notion Agents client.
type ClientOptions ¶
type ClientOptions struct {
Auth string // Required: Notion API token
BaseURL string // Optional: defaults to DefaultBaseURL
NotionVersion string // Optional: defaults to DefaultVersion
HTTPClient *http.Client // Optional: custom HTTP client
}
ClientOptions configures a new Client.
type CustomAgentAvatar ¶
type CustomAgentAvatar struct {
URL string `json:"url"`
}
type CustomEmoji ¶
type ExternalURL ¶
type ExternalURL struct {
URL string `json:"url"`
}
type MessageParent ¶
MessageParent identifies the parent of a message.
type NotionAgentsError ¶
NotionAgentsError is the base error type for SDK errors.
func (*NotionAgentsError) Error ¶
func (e *NotionAgentsError) Error() string
type PollThreadOptions ¶
type PollThreadOptions struct {
MaxAttempts int
BaseDelayMs int
MaxDelayMs int
InitialDelayMs int
OnPending func(thread ThreadListItem, attempt int)
OnThreadNotFound func(attempt int)
}
PollThreadOptions configures thread polling behavior.
type PollingTimeoutError ¶
type PollingTimeoutError struct {
Attempts int
}
PollingTimeoutError is returned when thread polling exceeds max attempts.
func (*PollingTimeoutError) Error ¶
func (e *PollingTimeoutError) Error() string
type StreamChunk ¶
type StreamChunk struct {
Type string `json:"type"`
ThreadID string `json:"thread_id,omitempty"`
AgentID string `json:"agent_id,omitempty"`
ID string `json:"id,omitempty"`
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
Attachments []ThreadMessageAttachment `json:"attachments,omitempty"`
ContentParts []AgentContentPart `json:"content_parts,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
StreamChunk represents a single chunk from a streaming chat response.
type StreamError ¶
StreamError is returned for streaming-related errors.
func (*StreamError) Error ¶
func (e *StreamError) Error() string
type StreamMessage ¶
type StreamMessage struct {
ID string `json:"id"`
Role string `json:"role"`
Content string `json:"content"`
Attachments []ThreadMessageAttachment `json:"attachments,omitempty"`
ContentParts []AgentContentPart `json:"content_parts,omitempty"`
}
StreamMessage represents an accumulated message from a stream.
type StreamReader ¶
type StreamReader struct {
// contains filtered or unexported fields
}
StreamReader reads streaming chat responses using an iterator pattern.
func (*StreamReader) Close ¶
func (r *StreamReader) Close() error
Close closes the underlying response body.
func (*StreamReader) Next ¶
func (r *StreamReader) Next() (StreamChunk, error)
Next returns the next chunk from the stream. Returns io.EOF when the stream is complete.
func (*StreamReader) ThreadInfo ¶
func (r *StreamReader) ThreadInfo() *ThreadInfo
ThreadInfo returns the accumulated thread info after the stream completes.
type Thread ¶
Thread provides operations on a specific thread.
func (*Thread) Get ¶
func (t *Thread) Get(ctx context.Context) (*ThreadListItem, error)
Get retrieves this thread's details.
func (*Thread) ListMessages ¶
func (t *Thread) ListMessages(ctx context.Context, params *ThreadMessageListParams) (*ThreadMessageListResponse, error)
ListMessages returns a paginated list of messages in this thread.
func (*Thread) Poll ¶
func (t *Thread) Poll(ctx context.Context, opts *PollThreadOptions) (*ThreadListItem, error)
Poll polls this thread until completion using exponential backoff.
type ThreadInfo ¶
type ThreadInfo struct {
ThreadID string
AgentID string
Messages []StreamMessage
}
ThreadInfo contains the final result of a completed streaming chat.
type ThreadListItem ¶
type ThreadListItem struct {
Object string `json:"object"`
ID string `json:"id"`
Title string `json:"title"`
Status ThreadStatus `json:"status"`
CreatedBy CreatedBy `json:"created_by"`
AgentVersion *AgentVersion `json:"agent_version"`
}
ThreadListItem represents a thread in list responses.
func CollectThreads ¶
func CollectThreads(ctx context.Context, agent *Agent, params *ThreadListParams) ([]ThreadListItem, error)
CollectThreads collects all threads into a slice.
type ThreadListParams ¶
type ThreadListParams struct {
ID string
Title string
Status ThreadStatus
CreatedByType string
CreatedByID string
StartCursor string
PageSize int
}
ThreadListParams configures thread listing requests.
type ThreadListResponse ¶
type ThreadListResponse struct {
Object string `json:"object"`
Type string `json:"type"`
Results []ThreadListItem `json:"results"`
HasMore bool `json:"has_more"`
NextCursor *string `json:"next_cursor"`
}
ThreadListResponse is the paginated response for listing threads.
type ThreadMessageAttachment ¶
type ThreadMessageAttachment struct {
Name string `json:"name"`
ContentType string `json:"content_type"`
URL string `json:"url"`
ExpiryTime *string `json:"expiry_time,omitempty"`
}
ThreadMessageAttachment represents a file attached to a message.
type ThreadMessageItem ¶
type ThreadMessageItem struct {
Object string `json:"object"`
ID string `json:"id"`
Role string `json:"role"`
Content string `json:"content"`
Parent MessageParent `json:"parent"`
Attachments []ThreadMessageAttachment `json:"attachments,omitempty"`
ContentParts []AgentContentPart `json:"content_parts,omitempty"`
}
ThreadMessageItem represents a message within a thread.
func CollectMessages ¶
func CollectMessages(ctx context.Context, thread *Thread, params *ThreadMessageListParams) ([]ThreadMessageItem, error)
CollectMessages collects all messages into a slice.
type ThreadMessageListParams ¶
ThreadMessageListParams configures message listing requests.
type ThreadMessageListResponse ¶
type ThreadMessageListResponse struct {
Object string `json:"object"`
Type string `json:"type"`
Results []ThreadMessageItem `json:"results"`
HasMore bool `json:"has_more"`
NextCursor *string `json:"next_cursor"`
}
ThreadMessageListResponse is the paginated response for listing messages.
type ThreadNotFoundError ¶
type ThreadNotFoundError struct {
ThreadID string
}
ThreadNotFoundError is returned when a thread cannot be found.
func (*ThreadNotFoundError) Error ¶
func (e *ThreadNotFoundError) Error() string
type ThreadStatus ¶
type ThreadStatus string
ThreadStatus represents the status of a thread.
const ( ThreadStatusPending ThreadStatus = "pending" ThreadStatusCompleted ThreadStatus = "completed" ThreadStatusFailed ThreadStatus = "failed" )
type ToolResult ¶
type ToolResult struct {
ID string `json:"id"`
AgentStepID *string `json:"agent_step_id"`
ToolCallID *string `json:"tool_call_id"`
ToolName string `json:"tool_name"`
ToolType string `json:"tool_type"`
State string `json:"state"`
Input interface{} `json:"input"`
Output interface{} `json:"output"`
Error *string `json:"error"`
StartedAt int64 `json:"started_at"`
FinishedAt *int64 `json:"finished_at"`
DurationMs *int64 `json:"duration_ms"`
}
ToolResult represents the result of an agent tool call.