Routes
A route is an explicit contract. Nitro does the routing; Fellow enforces the contract.
import { defineRoute, z } from '@fellowframework/core'
export default defineRoute({
id: 'records.documents.get',
method: 'GET',
path: '/api/records/documents/:id',
input: { params: z.object({ id: z.uuid() }) },
output: documentSchema,
access: { permission: 'records.document.read' },
tenant: 'required',
errors: ['records.document.not_found'],
handler: ({ input }) => getDocument(input.params.id),
})
Pipeline
For every request, in this order:
- Identity:
Authorization: Bearer <token>(HS256,FELLOW_SECRET) first, then the session cookie (seeauth). - Execution context is created (request id, identity, tenant, session).
- Middleware: the application’s global middleware (
app.ts), then the route’smiddleware. access:'public','authenticated'(any identity) or{ permission }. No identity → 401auth.unauthenticated. Missing permission → 403auth.forbidden.tenant: 'required'takes the tenant from the verified identity → otherwise 403tenant.required. The tenant is never read from headers, query or body.- CSRF: requests authenticated by a session cookie with a method other than GET/HEAD/OPTIONS need
x-csrf-token→ otherwise 403auth.csrf_mismatch. Bearer tokens are exempt. input.params,input.queryandinput.bodyare parsed → otherwise 422validation.failed. Bodies are JSON ormultipart/form-data(files arrive asFile; usez.file()). Bodies overmaxBodyBytes(default 1 MiB for JSON, 10 MiB for multipart) → 413.- The handler runs inside the execution context (
useTenant(),useIdentity(),sql). - The result is parsed through
output. Fields not in the schema are dropped; a mismatch is a 500contract.output_invalid.status: 204sends no body.
Every response carries x-request-id and the security headers (x-content-type-options: nosniff,
x-frame-options: SAMEORIGIN, referrer-policy: strict-origin-when-cross-origin).
Middleware
import { defineMiddleware, rateLimit } from '@fellowframework/core'
export const audit = defineMiddleware('records.audit', async ({ request, identity }, next) => {
const response = await next()
console.info(request.method, identity?.subject, response.status)
return response
})
export default defineRoute({ /* ... */ middleware: [rateLimit({ limit: 10, window: 60 }), audit] })
rateLimit({ limit, window, by?, name? }) is a fixed window on the cache store (Redis in production), keyed
by client IP by default; exceeding it gives 429 rate_limit.exceeded with Retry-After. The client IP comes
from the socket; x-forwarded-for is honoured only with FELLOW_TRUST_PROXY=true.
Errors
Every error response has the same shape:
{ "error": { "code": "records.document.not_found", "message": "Document not found.", "requestId": "..." } }
Throw domain errors with errors.notFound(code, message), errors.conflict(code, message) or
new FellowError(code, message, { status }). Unexpected errors are logged with the request id
and returned as a generic internal.error; internal details never reach the client.
Permissions
Granted permissions support wildcards: * and records.document.*.
Every permission used by a route must be declared in the module manifest.
Limitations
- Routes are registered when the server starts: restart
fellow devafter adding a route file.