Skip to main content
Sonamu is a high-performance HTTP server based on Fastify. You can configure server port, host, plugins, lifecycle hooks, and more.

Basic Structure

baseUrl

Specifies the full URL where the server is externally accessible. Type: string (optional) Default: http://{listen.host}:{listen.port}
Using actual domain:
baseUrl is used for URLs generated during file uploads, SSR meta tags, etc.
With auth enabled and neither 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
Setting port via environment variable:

host

The host address the server binds to. Type: string (optional) Default: "localhost"
Listen on all network interfaces:
When using 0.0.0.0 in production, verify your firewall settings.
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

Additional Host 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 in listen.host and baseUrl stay 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 dev serves through Vite on the same domain, also add the domain to server.allowedHosts in the web project’s vite.config.
This list is a Host check against DNS rebinding. It does not restrict who can connect. List only domains you own, and do not use it to expose the server to other devices by binding to 0.0.0.0.

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

Parses application/x-www-form-urlencoded request bodies. Type: boolean | FastifyFormbodyOptions (optional)
With options:

qs

Parses query strings. Supports nested objects and arrays. Type: boolean | QsPluginOptions (optional)

multipart

Handles file uploads (multipart/form-data). Type: boolean | FastifyMultipartOptions (optional)
These limits also apply to @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)
Example: /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)
→ compress detailed settings

cors

Configures CORS (Cross-Origin Resource Sharing). Type: boolean | FastifyCorsOptions (optional)
Allow all origins in development:

sse

Supports Server-Sent Events. Type: boolean | SsePluginOptions (optional)
→ SSE usage

ws

Handles WebSocket connections (@fastify/websocket). Type: boolean | WebsocketPluginOptions (optional)
Without 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>
→ Context detailed explanation

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 that guardHandler 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
→ Cache-Control detailed explanation

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>
If every error is sent as 500, guard rejections (401/403) become 500 too. Use isSoException to keep the statusCode of Sonamu exceptions.

Practical Examples

Basic Development Server

Production Server

Docker Container Environment

Next Steps

After completing basic server settings:
  • auth - Authentication settings
  • storage - File storage settings
  • cache - Cache settings
  • compress - Response compression settings