The API document
An API document describes the routes of a web program. It lists each path, its HTTP method, its parameters and its responses. Other teams read it, and many tools generate a client from it.
nvs build --openapi writes this document for a Novis program. The document uses OpenAPI 3.1, a
common format for API documents. You do not write the document by hand. It comes from the code, so it
always matches the code.
Write the document
Section titled “Write the document”nvs build --openapi reads a program and prints its API document as JSON. The program does not run.
This shop.nvs has one route, GET /orders/{id}. Its method show has the parameter uint $id and
returns a string. The comment /// Shows one order. is above the method.
nvs build --openapi shop.nvs{ "info": { "title": "shop", "version": "0.0.0" }, "openapi": "3.1.0", "paths": { "/orders/{id}": { "get": { "operationId": "Orders.show", "parameters": [ { "in": "path", "name": "id", "required": true, "schema": { "minimum": 0, "type": "integer" } } ], "responses": { "200": { "content": { "application/json": { "schema": { "type": "string" } } }, "description": "success" } }, "summary": "Shows one order." } } }}The document has one entry in paths for each route. Each entry has these parts:
operationIdis the class and the method, with a dot:Orders.show. In the namespaceApp, it isApp.Orders.show.parameterslists the parameters in the path and the query. Auintis anintegerwith aminimumof0.responseshas the200response. Its schema is the return type when that is a simple type, such asstringorint.summaryis the first sentence of the///comment above the method.
Some parameter types have their own schema:
| Parameter | Schema |
|---|---|
#[Core\Query] ?int $page = null |
"required": false, typed by int |
a bool query value |
{"type": "integer", "enum": [0, 1]} |
Core\Time\Date |
{"type": "string", "format": "date"} |
Core\Time\Instant |
{"type": "string", "format": "date-time"} |
Core\Time\TimeOfDay, Core\Time\DateTime |
{"type": "string"} |
Core\Uuid |
{"type": "string", "format": "uuid"} |
A bool query value is written 1 or 0, so its schema is an integer. A TimeOfDay has no offset,
and a DateTime has a time zone name. The JSON Schema formats time and date-time do not allow
that text, so these two have no format.
A property of a #[Core\Json\Derive] class uses the same schemas. A Core\Time\Duration or a
Core\Time\Zone property is {"type": "string"}.
title is the name of the file without .nvs. version is the version in the nearest
package.toml, found from the folder where you run the command. Without a package.toml, it is
0.0.0.
The command prints to standard output. To keep the document, write it to a file:
nvs build --openapi shop.nvs > openapi.jsonA program with no #[Core\Route] writes no document. The command prints a message, and the exit code
is 0. A program with a compile error writes no document either. The command prints the errors, and
the exit code is 1.
Two routes can have the same path in the document. An example is /a/{id} and /a/{key}. Then the
command prints an error that names both routes, and writes no document.
Describe a route with #[Core\Api]
Section titled “Describe a route with #[Core\Api]”Some facts are not in the code. Examples are the group of a route, the errors it returns and how a
client proves who it is. #[Core\Api] adds these facts to the document. It goes on a method that has
a #[Core\Route]. After use Core\Api;, you can write it as #[Api(...)].
#[Core\Api] has five fields, and every field is optional:
| Field | What it adds to the document |
|---|---|
tags |
names that put routes into groups |
errors |
error responses, each with a status code and a class |
security |
the security schemes that a client must use |
example |
an example of a response |
operationId |
your own operationId, in place of Orders.show |
Novis checks each field when it compiles the program. A class in errors must exist. Each key in
example must be a property of the return type.
A security scheme says how a client proves who it is, for example with a token. You declare it with
#[Core\Api\Scheme] on a class. Its name: is the name that security uses. A name in security
that no scheme declares does not compile.
The editor completes these names. Inside #[Core\Api(...)], it suggests the fields. In a string
in the security list, it suggests the security schemes of the program. Here it suggests bearer and
key:
What this recording shows
- Type
b. A list of suggestions opens. It showsbearer. - Press Enter to choose
bearer.
This is the document for the same program:
nvs build --openapi shop.nvs{ "components": { "securitySchemes": { "bearer": { "scheme": "bearer", "type": "http" }, "key": { "in": "header", "name": "X-Api-Key", "type": "apiKey" } } }, "info": { "title": "shop", "version": "0.0.0" }, "openapi": "3.1.0", "paths": { "/orders/{id}": { "get": { "operationId": "Orders.show", "parameters": [ { "in": "path", "name": "id", "required": true, "schema": { "minimum": 0, "type": "integer" } } ], "responses": { "200": { "content": { "application/json": { "schema": { "type": "string" } } }, "description": "success" }, "404": { "description": "RuntimeError" } }, "security": [ { "bearer": [] } ], "summary": "Shows one order.", "tags": [ "Orders" ] } } }}Four parts are new:
componentslists the two security schemes.responseshas a404response. Itsdescriptionis the name of the class.securitynames the schemebearer.tagshas the groupOrders.
Find changes that break clients
Section titled “Find changes that break clients”A client is a program that calls your API. When a route changes, an old client may stop working.
nvs api diff compares two API documents and prints each change. Give it the document of your last
release first, and then the new document from nvs build --openapi.
Each line starts with one of three words:
| Word | Meaning | Examples |
|---|---|---|
breaking |
A client may stop working. | A path is removed. A parameter is removed or becomes required. A status code is removed. |
additive |
Something is new, and clients still work. | A new route. A new optional parameter. |
cosmetic |
The change does not affect a client. | A changed title, version, summary or tag. |
The last line counts the changes. When the two documents are the same, the command prints
no change.
The three documents below are for the route above. In v2.json, the method has a new parameter:
#[Core\Query] string $currency = 'EUR'. A client may leave it out, because it has a default value.
In v3.json, the parameter has no default value, so every client must send it:
nvs api diff v1.json v2.jsonadditive: paths./orders/{id}.get.query.currency — optional parameter added1 change(s): 0 breaking, 1 additive, 0 cosmeticnvs api diff v2.json v3.jsonbreaking: paths./orders/{id}.get.query.currency — optional parameter became required1 change(s): 1 breaking, 0 additive, 0 cosmeticexit 1nvs api diff v3.json v3.jsonno changeThe exit code tells you what nvs api diff found:
| Exit code | Meaning |
|---|---|
0 |
No change is breaking. |
1 |
A change is breaking, or a file is not an API document that can be read. |
On a build server, run nvs api diff with the document of your last release. Most build servers stop
the build when a command exits with a code that is not 0. A change that breaks clients then stops the
build.
Read more in Reference: nvs build –openapi and Reference: nvs api diff.
