Route Handling

Nitro supports file-based routing for your API routes.

Handler files inside routes/ and api/ directory will be automatically mapped to unjs/h3 routes.

Note: api/ is a shortcut for routes/api as a common prefix. However, please note that some deployment providers use app/ directory for their API format. You can simply use the routes/api or srcDir option to move everything under src/ or server/ directory.


Check out h3 JSDocs for all available utilities.


Example: Simple API route

// routes/test.tsexport default eventHandler(() => 'Hello World!')

Example: API route with params

// routes/hello/[name].tsexport default eventHandler(event => `Hello ${}!`)

Example: API route with a specific HTTP request method (get, post, put, delete, options and so on)

// routes/user.get.tsexport default eventHandler(async event => {  return `User profile!`})
// routes/ default eventHandler(async event => {  const body = await readBody(event)  // TODO: Handle body and update user  return `User updated!`})

Example: Catch all page

// routes/[...].tsexport default eventHandler(event => `Default page`)