API Routes
File-based API routing with HTTP method support and dynamic parameters.
api
hello
hello.ts
test.get.ts
test.post.ts
index.html
nitro.config.ts
package.json
tsconfig.json
vite.config.ts
import { defineHandler } from "nitro";
export default defineHandler(() => "Nitro is amazing!");Nitro supports file-based routing in the api/ or routes/ directory. Each file becomes an API endpoint based on its path.
#Basic Route
Create a file in the api/ directory to define a route. The file path becomes the URL path:
api/hello.ts
import { defineHandler } from "nitro";
export default defineHandler(() => "Nitro is amazing!");This creates a GET /api/hello endpoint.
#Dynamic Routes
Use square brackets [param] for dynamic URL segments. Access params via event.context.params:
api/hello/[name
import { defineHandler } from "nitro";
export default defineHandler((event) => `Hello (param: ${event.context.params!.name})!`);This creates a GET /api/hello/:name endpoint (e.g., /api/hello/world).
#HTTP Methods
Suffix your file with the HTTP method (.get.ts, .post.ts, .put.ts, .delete.ts, etc.):
#GET Handler
api/test.get.ts
import { defineHandler } from "nitro";
export default defineHandler(() => "Test get handler");#POST Handler
api/test.post.ts
import { defineHandler } from "nitro";
export default defineHandler(async (event) => {
const body = await event.req.json();
return {
message: "Test post handler",
body,
};
});