Exposing OpenAPI
Generating an OpenAPI document and browsing it with Swagger UI.
package @smitejs/openapi
@smitejs/openapi describes your app the way a spec describes an API: it walks the compiled routes and emits an OpenAPI 3.1 document. Because the generator runs against your app's live schemas, the req buckets you declared (query, params, headers, body) become accurate parameters and request bodies — no hand-written spec to keep in sync.
Generate the document
Add the openapi() plugin to your smite.config.ts:
const config = defineSmiteConfig({
entries: ["./src/app.ts"],
plugins: [openapi({ outfile: "./openapi.json", title: "My API" })],
});
"openapi"Then run smite generate openapi. With several serverless handler entries, list them all in entries — the document merges paths from every app into one spec.
This writes a JSON document with one paths entry per endpoint. :param segments become {param} templates, bucket schemas become parameters / requestBody, and ANY endpoints are skipped with a warning.
Beyond the routes, pass root-level OpenAPI configuration to openapi() — for example servers to point the document at a deployed host, or securitySchemes plus security to declare API-key auth:
await openapi({
outfile,
title: "Fixture API",
servers: [{ url: "https://api.example.com" }],
security: [{ apiKey: [] }],
securitySchemes: {
apiKey: { type: "apiKey", in: "header", name: "X-Api-Key" },
},
tags: [{ name: "users", description: "User management" }],
externalDocs: { url: "https://example.com/docs" },
additional: { "x-custom": { enabled: true } },
}).run({ apps: [app] });[{ url: "https://api.example.com" }]
[{ apiKey: [] }]
{
apiKey: { type: "apiKey", in: "header", name: "X-Api-Key" },
}
[
{ name: "users", description: "User management" },
]
{ url: "https://example.com/docs" }
{ enabled: true }Any of servers, security, securitySchemes (emitted under components.securitySchemes), tags, externalDocs, or the catch-all additional object is merged verbatim into the emitted document.
await openapi({ outfile, title: "Fixture API" }).run({ apps: [app] });"3.1.0"
"Fixture API"
expect.arrayContaining([
expect.objectContaining({ name: "id", in: "path", required: true }),
expect.objectContaining({ name: "q", in: "query", required: false }),
]),Browse it with Swagger UI
The generator ships a router that serves the document and an interactive UI, so you can explore and try your endpoints in the browser:
const router = swaggerUi({ doc, title: "Pets API" });
const spec = await router({
method: "GET",
path: "/openapi.json",
query: {},
headers: {},
cookies: {},
params: {},
body: undefined,
});
const page = await router({
method: "GET",
path: "/docs",
query: {},
headers: {},
cookies: {},
params: {},
body: undefined,
});200
doc
"application/json"
200
"text/html"
"swagger-ui"
"/openapi.json"
404The router responds to:
GET /openapi.json— the raw document as JSON.GET /docs— the Swagger UI page (assets loaded from a CDN by default; override with thecdnoption to self-host).
Compose it next to your app's serve() router so /docs and /openapi.json route to the UI while everything else goes to your API:
const router = compiled.serve();
const docs = swaggerUi({ doc, title: "Pets API" });
const serve = async (request) => {
const response = await docs(request);
return response.status === 404 ? router(request) : response;
};201
200
"swagger-ui"What is emitted
- One OAS path per endpoint template (
/users/:id→/users/{id}). parametersfromreq.query/req.params/req.headersschemas; path params are alwaysrequired.requestBodyfromreq.bodyasapplication/json.- A default
200response until response schemas land.
See also
@smitejs/client's "Creating clients" to generate a typed client for the same app.