@api Decorator
How do I use the @api decorator?
How do I use the @api decorator?
Attach the Auto-generated endpoints:With the default
@api decorator to Model or Frame class methods to create REST API endpoints.Basic usage: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.What are the options for @api decorator?
What are the options for @api decorator?
httpMethod (default: guards (authentication/authorization):path (custom path):
"GET"):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.How do I receive parameters in GET requests?
How do I receive parameters in GET requests?
Query parameters (single values):Query parameters (object):
How do I receive body in POST requests?
How do I receive body in POST requests?
Single object:Multiple parameters:
How do I create a file upload API?
How do I create a file upload API?
The Multiple files:
@upload decorator is used independently without @api.Single file:How do I return errors?
How do I return errors?
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
How do I get current logged-in user information?
How do I get current logged-in user information?
What information is in Context?
What information is in Context?
userId/ip/userAgent are not top-level properties on Context. Use
context.user?.id, context.request.ip, and context.headers["user-agent"]
respectively.How do I use sessions?
How do I use sessions?
Sessions are managed by better-auth. Enable it with Sign in / sign out: clients call the better-auth endpoints (
server.auth; there is no separate session
setting.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 ManagementAuthentication and Authorization
How do I control permissions with Guards?
How do I control permissions with Guards?
Guard implementation: guard keys (Using Guards:→ Protected Routes
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.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”.Response Format
How do I customize API responses?
How do I customize API responses?
Default response:Custom response:Setting headers:
How do I return paginated responses?
How do I return paginated responses?
Using ListResult:
Frontend Integration
How do I use auto-generated client code?
How do I use auto-generated client code?
Service files auto-generated:When Model files change, Sonamu regenerates Using in React:Using TanStack Query hooks:
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.