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 inagents/api/status.ts:
Your endpoint is now available at
GET /api/threads/:threadId/status. The framework auto-discovers it from the agents/api/ directory.Guarding User Endpoints with Application Roles
Hosted projects assign application roles in Platform → project Settings → Users. Platform pushes assignments to the Standard Agents Builder instance; applications read the local identity without making a Platform request. These roles are separate from the built-inadmin, user, and api access levels and the
permissions field. Existing sessions and personal API keys resolve the user’s
current replicated assignment.
Use a user endpoint when access belongs to the authenticated user:
releases:canary or releases:*, but must
implement that check explicitly. There is no automatic wildcard matching or
administrative bypass. Missing roles grant no application access.
Tools and thread endpoints can inspect (await state.user())?.roles; that
identity belongs to the thread owner, which may differ from an administrator
addressing the thread. Choose the identity that owns the protected operation.
Check authorization in each request or execution. Identity is memoized within
an execution, so pushed changes take effect on the next request or execution;
they do not cancel work already in flight. Enforce checks on the server rather
than relying on hidden UI controls or client-supplied role lists.
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
[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
defineThreadEndpoint marks the original handler.
The marked value keeps the (req, state, params, auth) signature.
A runtime adapter performs these steps:
- Extracts the thread ID from the URL
- Looks up the thread.
- Creates a ThreadState.
- Handles errors (400 for missing ID, 404 for not found, 500 for exceptions).
Signature
Parameters
req: Standard Web API Request object
state: ThreadState exposes thread data and capabilities.
state.execution is null for endpoint calls.
params: Route parameters captured from [name] and [*] path segments.
auth: auth.caller contains the resolved caller or null.
auth.threadAccess states whether the runtime grants access to this thread.
ThreadState in Endpoints
Thestate 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
Standard Agents Builder returns JSON for every failed request under/api/.
The response preserves the HTTP status.
The body uses { "error": { "code": "internal_error", "message": "..." }, "requestId": "..." }.
The X-Request-ID header matches requestId.
Failed responses use Cache-Control: no-store.
An uncaught exception returns HTTP 500 with code internal_error.
The server log contains the exception and request ID.
The response message omits exception details.
Authentication infrastructure failures return HTTP 503 with code auth_unavailable.
Platform network failures, unexpected HTTP statuses, and invalid payloads return HTTP 502 with code platform_unreachable.
Check PLATFORM_ENDPOINT and platform availability after that error.
An explicit platform credential or membership denial uses code platform_auth_rejected during local bootstrap.
Unknown API paths return HTTP 404 with code not_found.
Read error.message to display a failure.
Include requestId in a support report.
Returned legacy JSON errors retain their extra fields and response headers.
The router converts their error field to the structured envelope.
A legacy string error with a separate message retains its code in the envelope.
Its separate message becomes the envelope message.
WebSocket upgrades and successful responses retain their existing behavior.
This error contract is a Standard Agents Builder extension.
Handlers can return domain errors with an appropriate HTTP status:
Best Practices
Keep API Handlers Fast
Keep API Handlers Fast
API handlers run in the request path. Keep operations quick:
Handle Errors Gracefully
Handle Errors Gracefully
Always wrap risky operations:
Validate Input Data
Validate Input Data
Check request data before processing:
Use Descriptive File Names
Use Descriptive File Names
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