OpenAPI
@navios/openapi is a plugin that builds an OpenAPI 3.1 document from the application's routes and serves it as JSON, optionally as YAML, together with a Scalar API reference page. The endpoint declarations already say what each route accepts and returns, so most of the document needs no extra code.
The plugin's routes are ordinary Navios routes, so it works the same on every adapter.
Adding the plugin
The document is built once, while the application initialises, from app.routes(). It contains every route, including those added by other plugins, whether the handler is an @Endpoint method or a handler() field.
Paths in the document are full route paths, with the global prefix and module prefixes. The plugin's own routes get the global prefix too (/api/docs with globalPrefix: '/api') and are left out of the document.
What is documented
For each route, from its declaration:
Schemas
Declarations hold Standard Schemas, and OpenAPI needs JSON Schema. The plugin gets it in this order:
-
From the schema itself, when the library implements the Standard JSON Schema extension (
~standard.jsonSchema). The plugin asks for the input form of request parts and the output form of responses, in thedraft-2020-12dialect that OpenAPI 3.1 uses. Zod 4 and ArkType implement the extension, so they need no configuration. -
From the
toJsonSchemaoption, for libraries that do not:iois'input'for request parts and'output'for responses. Returnundefinedfor a schema the converter does not know. -
Otherwise the part is documented as an unconstrained schema, and one warning naming the endpoint is logged during
init().
Descriptions and examples come from whatever the library puts into its JSON Schema. With Zod that is .meta():
A schema with an id becomes a shared entry in components.schemas, and every use of it is a reference. In general, the $defs of a converted schema move to components.schemas and its $schema key is removed.
The zod-openapi form .meta({ openapi: { description } }) is understood as well: the keys under openapi are merged into the schema.
Types that JSON Schema cannot express, such as z.date(), z.instanceof(File) and z.custom(), are documented as unconstrained instead of failing. In a multipart body an unconstrained field is taken to be a file, which is how z.instanceof(File) and arrays of it end up documented as uploads.
Attributes
What a declaration cannot say (tags, summaries, security) is added with attributes. Each is a decorator for a handler, a controller or a module.
Three uses worth knowing:
The attributes only describe. ApiSecurity does not protect a route; a guard does. An operationId is present only when you set one.
With handler() fields, pass the values in the options: handler(createUser, { attributes: [ApiSummary('Create a user')] }, fn). Projects compiled with experimentalDecorators import the attributes from @navios/openapi/legacy-compat.
Options
transformDocument is the place for anything the options do not cover. Mutate the document or return a new one: transformDocument(document) { document.info.description = buildInfo }.
To keep the documentation out of production, leave the plugin out of plugins there.
Generating the document without serving it
generateOpenApiDocument builds the same document from a route list. Use it to write the file in a build step, for a client generator or a contract check in CI:
The in-memory adapter opens no port, so the script needs nothing but the application's modules.
Other exports
@navios/server, with the @navios/core it comes with, is the only peer dependency; no schema library is needed at run time.