Skip to content

OpenAPI ​

You can serve OpenAPI specifications and mount an interactive API reference with zero code, just enable a feature and add configuration. You can choose between Swagger UI and Scalar.

Configuration ​

Add the [web.openapi] section to config/config.toml:

toml
[web.openapi]
title = "My API"
version = "1.0.0"
spec-file-paths = ["config/openapi.yaml", "config/openapi-test.yaml"]

Note on the specification path

If you have a router-prefix configured, it will affect the specification path.

Multiple Specs ​

You can serve multiple specification files, each one gets its own entry in the URL and is selectable from the UI dropdown.

Swagger UI ​

Swagger UI renders the OpenAPI specification as an interactive web interface that lets you visualize and try every endpoint directly from the browser. Enable the web-swagger-ui feature:

toml
[dependencies]
sword = { version = "0.7", features = ["web", "web-swagger-ui"] }

See the Swagger UI documentation for its customization options.

Scalar ​

Scalar renders the same OpenAPI specification with a modern interactive interface, customizable themes and a built-in client to try the requests. Enable the web-scalar-ui feature:

toml
[dependencies]
sword = { version = "0.7", features = ["web", "web-scalar-ui"] }

See the Scalar documentation for its configuration and theming options.

Routes ​

The reference UI is served at /docs. If you enable web-swagger-ui and web-scalar-ui at the same time, each one is mounted on its own path. Swagger is at /docs/swagger-ui and Scalar at /docs/scalar-ui.