API Reference
Relevant source files
The following files were used as context for generating this wiki page:
The Mastra API provides a standardized interface for interacting with core components of the Mastra framework, including Agents, Workflows, and Memory. It is a RESTful API that allows developers to programmatically manage and execute AI tasks. The API is defined using an OpenAPI 3.0 specification, which enables auto-generation of clients and documentation.
This reference documents the available endpoints, request/response schemas, and the overall architecture of the API server. The primary entities managed by the API are agents, which perform tasks, and workflows, which orchestrate multi-step processes. The API also exposes endpoints for managing conversational memory through threads and runs.
API Server Architecture
The Mastra server uses a modular routing system to handle incoming API requests. All routes are prefixed with /api and are organized by resource, such as agents and workflows. The server adapter is responsible for registering these routes and forwarding requests to the appropriate handlers.
The following diagram illustrates the high-level routing structure.
This diagram shows how incoming requests are directed to different handlers based on their path.
Sources: packages/server/src/server/server-adapter/routes/index.ts
Health & Metadata
These endpoints provide information about the server's status and its API specification.
Health Check
Returns the health status of the server. This is useful for monitoring and service discovery.
- Endpoint:
GET /health - Description: Checks if the server is running and responsive.
- Responses:
200 OK: Server is healthy.
Sources: packages/deployer/src/server/openapi.json:390-403, packages/agent-builder/src/defaults.ts:1218-1221
OpenAPI Specification
Returns the OpenAPI 3.0 specification for the API in JSON format. This can be used to generate client libraries or for interactive documentation UIs.
- Endpoint:
GET /openapi.json - Description: Provides the full API specification.
- Responses:
200 OK: Returns theopenapi.jsoncontent.
Sources: packages/deployer/src/server/openapi.json, packages/agent-builder/src/defaults.ts:1017-1018
Agents
Endpoints for interacting with Mastra Agents.
Chat with an Agent
Streams a response from an agent based on a series of messages. This is the primary endpoint for interacting with an agent.
- Endpoint:
POST /api/agents/{agentId}/chat - Description: Initiates a chat session with a specified agent.
The following diagram shows the sequence of a chat request.
Path Parameters
| Name | Type | Description |
|---|---|---|
agentId | string | The unique ID of the agent. |
Sources: packages/deployer/src/server/openapi.json:40-44
Request Body
The request body must conform to the AgentChatRequestSchema.
| Field | Type | Description | Required |
|---|---|---|---|
messages | Array of Message | A list of messages representing the conversation history. | Yes |
threadId | string | The ID of the conversation thread. If not provided, a new thread may be created. | No |
stream | boolean | Whether to stream the response. Defaults to true. | No |
toolChoice | 'auto' | 'none' | 'required' | Strategy for tool usage. | No |
tools | Array of ToolDefinition | A list of tools available to the agent for this specific call. | No |
toolsets | Array of ToolsetDefinition | A list of toolsets available to the agent. | No |
maxToolRoundtrips | number | The maximum number of tool roundtrips to allow. | No |
Sources: packages/server/src/server/schemas/agents.ts:8-33, packages/deployer/src/server/openapi.json:62-69
List Agents
Retrieves a list of all available agents configured in the Mastra instance.
- Endpoint:
GET /api/agents - Description: Returns an array of agent IDs.
- Responses:
200 OK: An array of strings, where each string is an agent ID.
Sources: packages/deployer/src/server/openapi.json:25-38
Workflows
Endpoints for managing and executing Mastra Workflows.
Create and Start a Workflow Run
Creates a new run for a specified workflow and starts its execution.
- Endpoint:
POST /api/workflows/{workflowId}/runs - Description: Initiates a new execution of a workflow.
The diagram below illustrates the workflow run creation process.
Path Parameters
| Name | Type | Description |
|---|---|---|
workflowId | string | The unique ID of the workflow. |
Sources: packages/deployer/src/server/openapi.json:161-165
Request Body
The request body must conform to the WorkflowRunRequestSchema.
| Field | Type | Description | Required |
|---|---|---|---|
inputData | object | The input data for the workflow, matching its inputSchema. | Yes |
threadId | string | An optional thread ID to associate with the run. | No |
Sources: packages/server/src/server/schemas/workflows.ts:7-13, packages/deployer/src/server/openapi.json:179-186
Get Workflow Run Status
Retrieves the current status and result of a specific workflow run.
- Endpoint:
GET /api/workflows/{workflowId}/runs/{runId} - Description: Fetches the state of a workflow execution.
Path Parameters
| Name | Type | Description |
|---|---|---|
workflowId | string | The unique ID of the workflow. |
runId | string | The unique ID of the run. |
Sources: packages/deployer/src/server/openapi.json:209-218
Resume a Suspended Workflow Run
Resumes a workflow run that is in a suspended state.
- Endpoint:
POST /api/workflows/{workflowId}/runs/{runId}/resume - Description: Provides the necessary data to continue a paused workflow.
Path Parameters
| Name | Type | Description |
|---|---|---|
workflowId | string | The unique ID of the workflow. |
runId | string | The unique ID of the run. |
Sources: packages/deployer/src/server/openapi.json:253-262
Request Body
The request body must conform to the WorkflowResumeRequestSchema.
| Field | Type | Description | Required |
|---|---|---|---|
step | string | The ID of the suspended step to resume. | Yes |
resumeData | object | The data required by the step to continue, matching its resumeSchema. | Yes |
Sources: packages/server/src/server/schemas/workflows.ts:15-22, packages/deployer/src/server/openapi.json:276-283
Memory and Threads
Endpoints for managing conversational memory via threads.
List Threads
Retrieves a list of all memory threads.
- Endpoint:
GET /api/memory/threads - Description: Returns a paginated list of threads.
- Responses:
200 OK: An object containing an array of thread objects.
Sources: packages/deployer/src/server/openapi.json:419-437
Create Thread
Creates a new memory thread.
- Endpoint:
POST /api/memory/threads - Description: Creates a new, empty conversation thread.
- Request Body: An optional object containing
metadata. - Responses:
200 OK: The newly created thread object.
Sources: packages/deployer/src/server/openapi.json:439-463
Get Thread
Retrieves a specific memory thread by its ID.
- Endpoint:
GET /api/memory/threads/{threadId} - Description: Fetches a single thread, including its messages.
Path Parameters
| Name | Type | Description |
|---|---|---|
threadId | string | The ID of the thread. |