Skip to content

OpenAPI and the API reference ​

Fusion Stack writes an OpenAPI 3.1 document of your API from the code itself: routes, resources, queries, form requests, scopes and error codes. Nothing is written by hand, so it cannot drift from what the API does. The same document drives the API reference at /docs, the generated TypeScript client and the MCP tools.

What it reads ​

FromIt documents
Routes in the fusion-stack middleware groupPaths and methods
#[Endpoint] on an actionIts summary, description and tags
The returned JsonApiResourceThe response's attributes and relationships, typed from the database columns and the model's casts
The action's ApiQueryinclude, fields, filter, sort and page, as the query allows
The form request's rulesThe request body
#[RequiresScope]The scopes each endpoint needs
Error code enums with #[Description]Every error code clients may get
The model's factoryExamples

Rust has no attributes or reflection, so the Rust crate reads what you declare instead: each endpoint with Config::endpoint, its query with Endpoint::query, its body with json_api_body, resources from Resource, scopes with Endpoint::scope and error codes from Config::error_codes.

Describe an action with #[Endpoint] (Config::endpoint in Rust), and leave one out with hidden: true (hidden()):

php
#[Endpoint('Cancel an order', 'Only pending and paid orders can be cancelled; others get a 409 order_not_cancellable.')]
public function cancel(Order $order): OrderResource { ... }

#[Endpoint(hidden: true)]
public function internalStats() { ... }
rust
use fusion_stack::openapi::Endpoint;

Config::new()
    .endpoint(
        "POST /v1/orders/{order}/cancel",
        Endpoint::new("Cancel an order")
            .description("Only pending and paid orders can be cancelled; others get a 409 order_not_cancellable.")
            .resource("orders"),
    )
    .endpoint("GET /v1/internal-stats", Endpoint::unnamed().hidden())

On a closure route, put it on the closure: Route::get('ping', #[Endpoint('Ping')] fn () => []). In Rust every route is declared the same way, by its method and path. Only declared routes are in the Rust document; openapi::undeclared(app.routes()) lists the ones without an Endpoint.

Field descriptions ​

Describe the fields of a resource, form request or query with #[Describe] on the class, one per field. In Rust, call describe or describe_values on the Resource, and describe on the ApiQuery for filters:

php
#[Describe('status', 'Where the order is in fulfilment.', values: [
    'pending' => 'Placed, awaiting payment',
    'paid' => 'Paid, not yet shipped',
])]
#[Describe('total_cents', 'The order total in the smallest unit of the currency.')]
final class OrderResource extends JsonApiResource { ... }
rust
Resource::new("orders", "orders")
    // ...
    .describe_values(
        "status",
        "Where the order is in fulfilment.",
        [("pending", "Placed, awaiting payment"), ("paid", "Paid, not yet shipped")],
    )
    .describe("total_cents", "The order total in the smallest unit of the currency.")

Or return them from a describe() method, which can also give an example. Rust has no describe() method: it chains describe calls, and a request body's fields are described in the JSON schema given to json_api_body, with the example beside it:

php
public function describe(): array
{
    return [
        'name' => ['description' => "The team's display name.", 'example' => 'Acme Ltd'],
        'plan' => 'The subscription plan. Defaults to free.',
    ];
}
rust
Endpoint::new("Create a team").resource("teams").json_api_body(
    json!({ "type": "object", "properties": { "data": { "type": "object", "properties": {
        "type": { "enum": ["teams"] },
        "attributes": { "type": "object", "properties": {
            "name": { "type": "string", "description": "The team's display name." },
            "plan": { "type": "string", "description": "The subscription plan. Defaults to free." },
        } },
    } } } }),
    json!({ "data": { "type": "teams", "attributes": { "name": "Acme Ltd" } } }),
)

Name fields as the latest version does. Older versions' documents show them under their old names, through the same version changes responses go through.

Examples ​

Response examples are made from the model's factory, so they look like real data. Faker is seeded while they are made, so the document is the same every time it is generated, and a diff of openapi.json only shows real changes.

Rust has no model factories, so its document carries the request body examples you give json_api_body or body, in each version's shape.

Error codes ​

The package's error codes are documented for you. List your own enums in openapi.error_codes (Config::error_codes in Rust), and explain each case with #[Description] (the ErrorCode::description method in Rust):

php
// config/api.php
'openapi' => [
    'error_codes' => [\App\Api\ErrorCode::class],
],
rust
Config::new().error_codes([Arc::new(OrderNotCancellable) as _])
php
#[Description('The order has shipped or was already cancelled, so it cannot be cancelled.')]
case OrderNotCancellable = 'order_not_cancellable';
rust
impl ErrorCode for OrderNotCancellable {
    // code(), status() and title() ...
    fn description(&self) -> &str {
        "The order has shipped or was already cancelled, so it cannot be cancelled."
    }
}

Generating the document ​

sh
php artisan api:openapi --output=openapi.json                           # the latest version
php artisan api:openapi --api-version=2026-06-01 --output=openapi.json  # as that version sees it
sh
laravel api:openapi --output openapi.json                          # the latest version
laravel api:openapi --api-version 2026-06-01 --output openapi.json # as that version sees it

Commit openapi.json with the change that alters it, so a pull request shows exactly what changed for clients. The starter kit's CI fails when it is out of date. openapi.title, openapi.description and openapi.servers (Config::openapi_info and Config::openapi_servers in Rust) set the document's info; they default to app.name and app.url.

The API reference ​

/docs shows the document with Scalar, with a switcher between the released versions that are not retired, the latest first. Deprecated versions show their sunset date. /docs/changelog lists the changes in each version.

In Rust, Config::docs takes the same settings, and fusion_stack::docs::routes mounts the pages: leave the call out to turn them off, or wrap it in a group with middleware for a private API.

php
// config/api.php
'docs' => [
    'enabled' => true,
    'path' => 'docs',
    'middleware' => [],          // public; ['web', 'auth'] for a private API
    'renderer' => 'scalar',      // or 'redoc'
    'scalar' => ['config' => ['theme' => 'purple']],
    'lint' => 'warn',
],
rust
use fusion_stack::docs::{Docs, Renderer};

Config::new().docs(
    Docs::new()
        .path("docs")
        .renderer(Renderer::Scalar) // or Renderer::Redoc
        .scalar_config(json!({ "theme": "purple" }).as_object().cloned().unwrap_or_default())
        .lint_fails(false), // warn
)

// In the API's routes:
fusion_stack::docs::routes(route);

The page loads its renderer's script from a CDN at a fixed version, and the browser checks the file against the integrity hash in the config, so a file that changed on the CDN never runs on your reference. To use another version, or a copy you host, set scalar.url and scalar.integrity (Docs::scalar_url and Docs::scalar_integrity in Rust); redoc has the same pair. A url without an integrity is loaded unchecked.

php artisan api:changelog (laravel api:changelog in Rust) writes the same changelog as Markdown, from the descriptions of the version changes.

Docs linting ​

Under deadline pressure, descriptions are the first thing to go. api:docs:lint lists every endpoint, field, filter, error code and scope without one:

sh
php artisan api:docs:lint
sh
laravel api:docs:lint
text
   WARN  13 items have no description. Run with --fail, or set api.docs.lint to fail, to fail CI.

Run it with --fail in CI, or set docs.lint to fail (Docs::lint_fails(true) in Rust), so the API reference stays complete. The starter kit does both.

Released under the MIT License.