OpenAPI
Introduction
A documentation site, a Try-it sandbox and an OpenAPI export, built from your code.
Good APIs are documented, and documentation that is written by hand goes out of date. api-docs (@nestjs/swagger's counterpart) builds the reference from your code: the processor already knows your routes, parameters and types at build time, so the reference needs almost no annotations and cannot drift from the real API.
app.http(NettyHttpAdapter(8080)) {
useDocs {
title = "Shop API"; version = "1.0.0"; description = "Orders and products"
page("Getting started", "# Getting started\n\nGet a token from `POST /auth/login`...") // extra markdown pages in the sidebar
}
}| Address | What it is |
|---|---|
/docs | the site: a GitBook-style layout with search (⌘K), a Try it panel on every endpoint, Authorize for bearer tokens, and light and dark modes |
/docs/spec.json | the raw model |
/docs/openapi.json | OpenAPI 3, which Postman, Insomnia, Swagger UI and code generators read |
The docs routes skip your global guards, so they open without a token; turn them off outside development with useDocs { enabled = System.getenv("ENV") != "production" } if the API is private.
What you get with no annotations
- every route with its method, path and parameters (path, query, header);
- request and response models from the handler's types, including nested objects, lists, enums and the validation constraints (
@Size(min = 2)appears asminLength,@Patternaspattern); - which endpoints need a token (
@Authenticated,@Roles); - the error shape (one shared
ErrorResponsemodel) and a401response for secured endpoints.
Make it friendlier with annotations
| Annotation | On | Adds |
|---|---|---|
@ApiTag(name, description) | controller | the section of the sidebar |
@ApiOperation(summary, description, deprecated) | handler | a title and text; a KDoc comment on the handler is used when there is none |
@ApiResponse(status, description, type) | handler | another documented response (repeatable) |
@ApiProperty(description, example) | a DTO property | text and an example in the model |
@ApiParam, @ApiBody | parameter | text and examples |
@ApiBearerAuth | controller or handler | marks it as needing a token |
@ApiHidden | controller or handler | leaves it out |
Details: Types and parameters, Operations, Decorators, Security.
Make it look like yours
The site is built with Next.js, shadcn/ui and Tailwind, and the bundle ships inside the jar, so it needs no Node.js on the server. Its look comes from a theme block in the same useDocs call. Each value is checked when it is set, so a mistake fails at startup, and nothing in a theme value can break out of the page's stylesheet.
useDocs {
title = "Shop API"
theme {
mode = ThemeMode.SYSTEM // SYSTEM follows the reader's OS; LIGHT or DARK start there
radius = "0.5rem" // corners of cards, buttons and inputs
sansFont("Brand Sans", "classpath:fonts/brand-sans.woff2", weight = "100 900") // shipped with the site
monoFont("Brand Mono", "/opt/fonts/brand-mono.woff2") // a path on the server's disk
logo = "/static/logo.svg" // a path on this server, or an https URL, shown in the header
colors { primary = "oklch(0.6 0.2 300)" } // both schemes
dark { background = "#0b0d12" } // dark mode only; light { } works the same way
css = ".kest-glow { display: none }" // last resort: raw CSS, written as it is
}
}| Colour | Used for |
|---|---|
background, foreground, card, border, sidebar, codeBackground | surfaces and text |
primary, primaryForeground, accent, accentForeground, muted, mutedForeground | buttons, links, highlights |
methodGet, methodPost, methodPut, methodPatch, methodDelete | the method badges |
Colours are any CSS colour: a hex value, oklch(...), hsl(...) or rgb(...). The reader's choice of light or dark is remembered in their browser and wins over mode after the first visit.
Fonts
sansFont and monoFont ship a font file with the site, so the page makes no request to a font provider. The file is a WOFF2, WOFF, TTF or OTF, read from the server's disk or, with classpath:, from the application's jar. It is checked when the docs start: the name, the size (5 MB at most) and the file's first bytes must match its format, so a renamed HTML page fails at startup, not on the page. Pass weight for a variable font, as "400" or a range such as "100 900". The stack the site uses is built from the font's name and a system fallback. Use fontSans or fontMono instead when the reader's browser already has the font and you only want to name it.
What readers get
The site has the same reading aids as the documentation sites people use most:
- Search with ⌘K or
/across endpoints, models and guides. - Try it on every endpoint: edit the inputs, see the request as cURL, JavaScript or Python, send it and read the response. The language a reader picks is remembered for the next endpoint.
- On this page for each guide, which marks the section being read as you scroll, and Previous and Next at the foot of every page, in the order the sidebar lists them.
- Copy as Markdown on every guide and endpoint, for pasting into a ticket, a chat or a prompt.
- Reduced motion: when the reader's system asks for less motion, the site drops its smooth scrolling and transitions.
Build the site yourself
Only needed when you change the site itself. packages/api-docs/ui/build.sh installs the packages, runs the Next.js build and copies the static export into the module's resources, with a manifest of its files. Commit the result, because the jar is built from those resources.
Use the OpenAPI file
- Postman / Insomnia: import
/docs/openapi.jsonto get a ready collection. - Typed clients:
npx openapi-typescript http://localhost:8080/docs/openapi.json -o api.ts(TypeScript), oropenapi-generatorfor Java, Swift and more. - Contract checks in CI: fetch the file in a test and compare it to a saved copy, so an accidental breaking change is visible in review (Devtools: CI/CD shows the same idea for routes).
Tested
examples/store-api and the Java example. The theme settings, the shipped fonts and their checks are covered by DocsThemeTest in examples/platform-tests.