Skip to main content

What Are Thread API Endpoints?

Thread API endpoints are custom routes that operate on specific conversation threads. They allow you to:
  • Access thread data: Query messages, logs, and metadata
  • Perform operations: Export conversations, generate summaries, update state
  • Extend the API: Add domain-specific functionality beyond built-in routes
Thread API endpoints are file-based and auto-discovered. Create a TypeScript file in agents/api/ and export your endpoint definition—no manual registration needed.

Quick Start

Create your first endpoint in agents/api/status.ts:
Your endpoint is now available at GET /api/threads/:threadId/status. The framework auto-discovers it from the agents/api/ directory.

File-Based Routing

API files map directly to routes based on their filename:

Method Suffixes

Append the HTTP method to the filename to specify which method the endpoint handles:
  • .get.ts — GET requests (default if no suffix)
  • .post.ts — POST requests
  • .put.ts — PUT requests
  • .delete.ts — DELETE requests
  • .patch.ts — PATCH requests
Files without a method suffix default to GET. Method suffixes are case-insensitive. Dynamic route segments use [name] and are passed to the handler as params.name. Catch-all segments use [*] and are passed as params['*']; the catch-all value may contain / when it matches nested paths. Static routes are matched before dynamic routes, and dynamic routes are matched before catch-all routes.

Nested Routes

Create subdirectories for nested route paths:

Handler Function

The defineThreadEndpoint function wraps your handler and automatically:
  1. Extracts the thread ID from the URL
  2. Looks up the thread and creates a ThreadState
  3. Handles errors (400 for missing ID, 404 for not found, 500 for exceptions)

Signature

Parameters

req — Standard Web API Request object state — ThreadState providing access to thread identity, messages, file system, and more. Note: state.execution is always null in endpoints since the thread is at rest. params — Route parameters captured from [name] and [*] path segments.

ThreadState in Endpoints

The state parameter provides access to the thread’s data:

Identity

Access thread identity directly:

getMessages

Retrieve message history from the thread:

File System

Access the thread’s file system:

Resource Loading

Load agent, prompt, or model definitions:

Request Handling

Reading Request Body

Query Parameters

Headers

Error Handling

Wrap operations in try/catch for robust error handling:
The framework automatically handles common errors like missing thread IDs (400) and threads not found (404). Your handler only needs to handle domain-specific errors.

Best Practices

API handlers run in the request path. Keep operations quick:
Always wrap risky operations:
Check request data before processing:
Name files clearly to indicate their purpose:

Next Steps

Thread

Add custom methods to the Thread class

Tools

Create custom tools that endpoints can leverage

Hooks

Intercept and modify data at lifecycle points

REST API

Explore the built-in API endpoints