Basic Structure
baseUrl
Specifies the full URL where the server is externally accessible. Type:string (optional)
Default: http://{listen.host}:{listen.port}
server.auth.baseURL nor an environment variable such as BETTER_AUTH_URL set, Sonamu also uses the origin of baseUrl plus auth.basePath as the auth baseURL, as long as baseUrl is an absolute http(s) URL. See auth settings for details.
listen
Configures the port and host the server listens on. Type: (optional)port
The port number the server listens on. Type:number
host
The host address the server binds to. Type:string (optional)
Default: "localhost"
On local dev servers (
development and test), the Sonamu UI and DevRunner routes accept only requests whose Host is localhost, *.localhost, an IP address, the hostname in listen.host or baseUrl, or a hostname listed in devAllowedHosts; any other Host is rejected with 403. Binding to 0.0.0.0 does not change this check, so to connect through an alias domain or a tunnel, add that hostname to devAllowedHosts. App APIs are not affected.devAllowedHosts
AdditionalHost hostnames that the Sonamu UI and DevRunner accept on local dev servers (development and test). Set it when you open Sonamu UI through a development domain whose public DNS points to 127.0.0.1, an /etc/hosts alias, or a tunnel domain.
Type: string[] (optional)
localhost,*.localhost, IP addresses, and the hostnames inlisten.hostandbaseUrlstay allowed without being listed. This list adds to them.- List hostnames only, without a port or scheme. Matching is case-insensitive, and wildcards are not supported. An entry such as
"local.example.com:34900"or"http://local.example.com"makes the server fail at startup. - Sonamu UI and DevRunner are not registered in staging or production, so the option has no effect there.
- The option applies only to Sonamu UI and DevRunner. To open the web app pages that
sonamu devserves through Vite on the same domain, also add the domain toserver.allowedHostsin the web project’svite.config.
fastify
Configures Fastify server options. Type:Omit<FastifyServerOptions, "logger"> (optional)
Logging is configured separately through Sonamu’s
logging settings.plugins
Enables and configures Fastify plugins.formbody
Parsesapplication/x-www-form-urlencoded request bodies.
Type: boolean | FastifyFormbodyOptions (optional)
qs
Parses query strings. Supports nested objects and arrays. Type:boolean | QsPluginOptions (optional)
multipart
Handles file uploads (multipart/form-data).
Type: boolean | FastifyMultipartOptions (optional)
@upload() without arguments, and @upload({ limits }) overrides only the fields it sets. Uploads over a limit are rejected (with 413 under Fastify’s default error handling).
Allowing larger files:
static
Serves static files. Type:boolean | FastifyStaticOptions (optional)
/api/public/images/logo.png → public/images/logo.png file
If you also serve uploaded files from this path, set setHeaders to createUploadStaticHeaders({ uploadDir }) from sonamu/storage. New project templates include it, but existing projects do not get it automatically.
→ Static file serving
Sessions and authentication
server.plugins has no session plugin. Authentication and sessions are configured with
server.auth (Better Auth). For each request, Sonamu resolves the Better Auth session and passes
user and session to contextProvider through defaultContext; both are null when the
request is unauthenticated or server.auth is not configured.
→ auth settings
compress
Compresses responses. See separate documentation for details. Type:boolean | FastifyCompressOptions (optional)
cors
Configures CORS (Cross-Origin Resource Sharing). Type:boolean | FastifyCorsOptions (optional)
sse
Supports Server-Sent Events. Type:boolean | SsePluginOptions (optional)
ws
Handles WebSocket connections (@fastify/websocket).
Type: boolean | WebsocketPluginOptions (optional)
options.maxPayload, a single client frame is limited to 64 KiB (65536 bytes), and a larger frame closes the connection with close code 1009.
→ Inbound frame size limit
custom
Registers custom Fastify plugins. Type:(server: FastifyInstance) => void (optional)
apiConfig
Configures API behavior.contextProvider
A function that creates Context for each API call. Type:(defaultContext, request, reply) => Context | Promise<Context>
guardHandler
A function called when Guard decorators execute. Type:(guard, request, api) => void | boolean | Promise<void | boolean>
The handler runs inside the request context, so Sonamu.getContext() returns the current user
and session. Throw or return false to reject the request; false becomes an
UnauthorizedException (401). Async handlers are supported: the returned Promise is awaited before
the next guard or the API runs.
guardKeys
The guard keys thatguardHandler actually checks.
Type: readonly GuardKey[] (optional)
When set, a guard key outside the list is rejected with UnauthorizedException (401) without calling
guardHandler, and server startup stops with a list of APIs that use such keys. When unset, every key is
passed to guardHandler as before, so a key the handler does not check is allowed; in that case the
server prints a startup warning if any API uses guards.
guardKeys: [] rejects every guard key. To make an API public, set guards: [] on that API instead.
cacheControlHandler
A function that sets Cache-Control headers. Type:(req) => string | undefined
lifecycle
Registers hooks for server lifecycle events.onStart
Executes when the server starts. Type:(server: FastifyInstance) => void | Promise<void>
onShutdown
Executes when the server shuts down (graceful shutdown). Type:(server: FastifyInstance) => void | Promise<void>
onError
Executes when an unhandled error occurs. Type:(error, request, reply) => void | Promise<void>
isSoException to keep the statusCode of Sonamu exceptions.