Skip to main content

@api Decorator

Attach the @api decorator to Model or Frame class methods to create REST API endpoints.Basic usage:
Auto-generated endpoints:
With the default api.route.prefix of /api, the paths are /api/{model}/{method}. GET parameters are read from the query string; other methods read the JSON body.
httpMethod (default: "GET"):
guards (authentication/authorization):
Creating Public APIs: Omit the guards option to create a public API accessible to anyone without authentication. Use this for endpoints that don’t require login or permission checks.
path (custom path):
Query parameters (single values):
Query parameters (object):
Single object:
Multiple parameters:
The @upload decorator is used independently without @api.Single file:
Multiple files:
BadRequestException (400):
UnauthorizedException (401):
Permission error (403):
NotFoundException (404):The generated findById already throws NotFoundException when the record does not exist. Use findOne, which returns null, when you need your own handling:

Context and Session

userId/ip/userAgent are not top-level properties on Context. Use context.user?.id, context.request.ip, and context.headers["user-agent"] respectively.
Sessions are managed by better-auth. Enable it with server.auth; there is no separate session setting.
Sign in / sign out: clients call the better-auth endpoints (POST /api/auth/sign-in/email, POST /api/auth/sign-out), usually through authClient from better-auth/react. You do not write login APIs that store values in a session.Read the session in any API:
session is read-only session metadata (expiresAt, ipAddress, userAgent, …). Store per-user data in your own entities instead of the session.→ Session Management

Authentication and Authorization

Guard implementation: guard keys (user, admin, query) are checked in server.apiConfig.guardHandler. It runs inside the request context, so Sonamu.getContext() returns the current user. Throw to reject the request.
Using Guards:
When several guards are listed, guardHandler runs for each of them in order, so the request must pass all of them. guards: ["user", "admin"] means “logged in and admin”, not “user or admin”.
→ Protected Routes

Response Format

Default response:
Custom response:
Setting headers:
Using ListResult:

Frontend Integration

Service files auto-generated:When Model files change, Sonamu regenerates services.generated.ts in each sync target. Each Model gets a {Entity}Service namespace with a fetch function and, for tanstack-query clients, a hook.
Using in React:
Using TanStack Query hooks: