registerApiRoute()
The registerApiRoute() function creates custom HTTP routes that integrate with the Mastra server. Routes can include OpenAPI metadata to appear in the Swagger UI documentation.
ImportDirect link to Import
import { registerApiRoute } from '@mastra/core/server'
ParametersDirect link to Parameters
pathDirect link to path
The URL path for the route. Supports path parameters using :param syntax.
registerApiRoute("/items/:itemId", { ... })
Note: Custom route paths can't start with the server's configured apiPrefix (default: /api), as that prefix is reserved for built-in Mastra routes. If you set a custom apiPrefix, only that prefix is reserved — for example, with apiPrefix: '/mastra/api', paths like /api/my-endpoint are allowed.
optionsDirect link to options
method:
handler?:
createHandler?:
middleware?:
cors?:
openapi?:
OpenAPI optionsDirect link to OpenAPI options
The openapi property accepts standard OpenAPI 3.1 operation fields from hono-openapi. Routes without an openapi property aren't included in Swagger UI.
summary?:
description?:
deprecated?:
parameters?:
requestBody?:
responses?:
security?:
Return valueDirect link to Return value
Returns an ApiRoute object to be passed to server.apiRoutes in the Mastra configuration.
Handler contextDirect link to Handler context
The handler receives a Hono Context object with access to:
handler: async c => {
// Get the Mastra instance
const mastra = c.get('mastra')
// Get request context
const requestContext = c.get('requestContext')
// Access path parameters
const itemId = c.req.param('itemId')
// Access query parameters
const filter = c.req.query('filter')
// Access request body
const body = await c.req.json()
// Return JSON response
return c.json({ data: 'value' })
}
ExamplesDirect link to Examples
Basic GET RouteDirect link to Basic GET Route
import { Mastra } from '@mastra/core'
import { registerApiRoute } from '@mastra/core/server'
export const mastra = new Mastra({
server: {
apiRoutes: [
registerApiRoute('/health-check', {
method: 'GET',
handler: async c => {
return c.json({ status: 'ok' })
},
}),
],
},
})
Route with Path ParametersDirect link to Route with Path Parameters
registerApiRoute('/users/:userId/posts/:postId', {
method: 'GET',
handler: async c => {
const userId = c.req.param('userId')
const postId = c.req.param('postId')
return c.json({ userId, postId })
},
})
POST Route with BodyDirect link to POST Route with Body
registerApiRoute('/items', {
method: 'POST',
handler: async c => {
const body = await c.req.json()
const mastra = c.get('mastra')
// Process the request...
return c.json({ id: 'new-id', ...body }, 201)
},
})
Route with MiddlewareDirect link to Route with Middleware
registerApiRoute('/protected', {
method: 'GET',
middleware: [
async (c, next) => {
const token = c.req.header('Authorization')
if (!token) {
return c.json({ error: 'Unauthorized' }, 401)
}
await next()
},
],
handler: async c => {
return c.json({ data: 'protected content' })
},
})
Route with CORSDirect link to Route with CORS
Use route-specific CORS when one custom route needs cross-origin credentials, but the rest of the server should keep the global CORS policy.
registerApiRoute('/customer-webhook', {
method: 'POST',
cors: {
origin: ['https://customer-saas.example'],
credentials: true,
},
handler: async c => {
return c.json({ ok: true })
},
})
Route with OpenAPI DocumentationDirect link to Route with OpenAPI Documentation
import { z } from 'zod'
const ItemSchema = z.object({
id: z.string(),
name: z.string(),
price: z.number(),
})
registerApiRoute('/items/:itemId', {
method: 'GET',
openapi: {
summary: 'Get item by ID',
description: 'Retrieves a single item by its unique identifier',
tags: ['Items'],
parameters: [
{
name: 'itemId',
in: 'path',
required: true,
description: 'The item ID',
schema: { type: 'string' },
},
],
responses: {
200: {
description: 'Item found',
content: {
'application/json': {
schema: ItemSchema, // Zod schemas are converted to JSON Schema during OpenAPI generation
},
},
},
404: {
description: 'Item not found',
},
},
},
handler: async c => {
const itemId = c.req.param('itemId')
return c.json({ id: itemId, name: 'Example', price: 9.99 })
},
})
Using createHandler()Direct link to using-createhandler
For routes that need async initialization:
registerApiRoute('/dynamic', {
method: 'GET',
createHandler: async c => {
// Perform async setup
const config = await loadConfig()
return async c => {
return c.json({ config })
}
},
})
Error handlingDirect link to Error handling
Throw errors with status codes using Hono's HTTPException:
import { HTTPException } from 'hono/http-exception'
registerApiRoute('/items/:itemId', {
method: 'GET',
handler: async c => {
const itemId = c.req.param('itemId')
const item = await findItem(itemId)
if (!item) {
throw new HTTPException(404, { message: 'Item not found' })
}
return c.json(item)
},
})
RelatedDirect link to Related
- Custom API Routes Guide: Usage guide with examples
- Server Middleware: Global middleware configuration
- createRoute(): Type-safe route creation for server adapters
- Server Routes: Built-in Mastra server routes