KScript docs
EN

Routes and APIs

API docs: OpenAPI

The routes already say everything an API document needs, so KScript writes one for you. While kscript serve runs, open /_api for a page that lists every endpoint with its parameters, body and answers, with a form to try each one. Tools such as Postman, client generators and gateways read the same document at /_openapi.json or /_openapi.yaml. kscript openapi writes it to the terminal or a file:

CommandWhat it does
kscript openapiPrint the document as JSON.
kscript openapi --output yaml --out openapi.yamlWrite it as YAML to openapi.yaml.

The document is OpenAPI 3.1, and each part comes from your code:

In the document From
paths and methods The routes. {id?} and {path*} become {id} and {path}. A route for any method is listed under GET, POST, PUT, PATCH and DELETE. Redirects are left out.
path parameters The controller's typed parameter (id: int is an integer), else the where rule (int, number, uuid, or a pattern for slug, alpha and regular expressions).
query parameters query: Search: each field of the type is a query parameter, required unless it is optional.
request body json: NewPerson or body: NewPerson as application/json, form: T as a form, input: T as either.
success answer /// @returns Person[] (a type, then any words) for the schema. The status is 200, or the 2xx that a @response(..., status: 201) gives. Views are text/html. Without @returns, an API route's answer is JSON of any shape, and a page route's has no declared body.
error answers @abort(404) and status: 409 in the controller. 422 for typed input that doesn't fit (the body lists the problems), 400 for a JSON body that isn't JSON, 404 for a path parameter with a type or rule, 401 for auth, 429 for throttle, 304 for etag. API routes' errors share the Error schema, and a page route's errors are text/html.
summary, description The controller's /// doc comment (the first line is the summary, the rest the description), or one on the route itself. @param id ... describes a parameter.
deprecated @deprecated in the doc comment.
security The auth middleware, as a bearer token scheme. Every middleware is also listed under x-middleware.
schemas Each type and enum the routes use, with field descriptions and required fields, in components.schemas. Fields a type doesn't name are left out of the record without an error, so a schema says additionalProperties: false only for type T(extra: error) (which answers 422), and true for extra: keep.
tags The controller's import name (people for people.show).
info http.openapi.title and version, else the project's name and app.version.

Only the API routes (from api.* files) are included when there are any, otherwise every route. Set http.openapi.routes to "all" or "api" to choose. "openapi": false stops kscript serve answering /_api, /_openapi.json and /_openapi.yaml, for example on a public server, while kscript openapi still works.

To keep the document in your project, for example to commit it or hand it to a client generator, set http.openapi.file:

JSON
{ "http": { "openapi": { "file": "openapi.json" } } }

kscript watch, kscript serve and the editor (on save) then rewrite the file whenever the routes, controllers or types change. Name it openapi.yaml (or .yml) for YAML. The path is relative to kscript.json. kscript watch --check fails when the file is out of date, so CI can check it was committed.