# OpenAPI

- [Introduction](#introduction)
- [Printing the document](#printing-the-document)
- [Serving the document](#serving-the-document)
- [Describing a model's attributes](#describing-a-models-attributes)
- [What the document contains](#what-the-document-contains)
- [What it leaves out](#what-it-leaves-out)

## Introduction

Restify can describe its HTTP endpoints as an [OpenAPI 3.1](https://spec.openapis.org/oas/v3.1.0) document. API clients, SDK generators and AI agents read it to learn every endpoint, parameter, request body and response.

The document is built from the same facts as the MCP tools and `restify:list`: the repositories, their fields, and the `#[derive(Schema)]` of their input structs. There is no second description to keep in sync.

The document is the crate's `openapi` feature, on by default. Without it, `.openapi(..)` and the `restify:openapi` command don't exist: see [Choosing features](installation.md#choosing-features).

## Printing the document

```shell
laravel restify:openapi
laravel restify:openapi --output openapi.json
```

The first command prints the document as JSON. The second writes it to a file instead. Both work whether or not the application serves the document.

## Serving the document

The document isn't served by default. To serve it, name a path inside the prefix:

```rust
Restify::new()
    .authenticate(Authenticate::via::<Sanctum<User>>())
    .openapi("/openapi.json")
    .openapi_version(env!("CARGO_PKG_VERSION"))
    .repository::<PostRepository>()
```

The application then answers `GET /api/restify/openapi.json`. The provider's middleware runs first, as it does for every other endpoint, so with the example above a client needs an API token to read it.

The document's settings come from the application:

| Field | Value |
| --- | --- |
| `info.title` | `APP_NAME` |
| `info.version` | `.openapi_version(..)`. Default `1.0.0` |
| `servers` | `APP_URL` |

## Describing a model's attributes

There is nothing to write. The document lists a resource's `attributes` from the repository's [fields](fields.md): each field's name, and the JSON Schema of its column's type.

| In `Repository::fields` | In `attributes` |
| --- | --- |
| `Field::new(Post::title)` | A property with the column's type |
| A column of `Option<T>` | The type or `null` |
| `Field::computed("excerpt", ..)`, `Field::computed_with(..)` | A property with the type the function returns |
| `.label("headline")` | The property's name |
| `.description("..")` | `description` |
| `.as_integer()`, `.decimals(2)` | The type the field then writes: an integer, a string |
| `Field::query_only(..)` | Left out |
| A column no field lists | Left out |

A field of a type the document doesn't know needs `restify::Property`. See [Fields](fields.md#how-each-type-is-written).

`attributes` requires no field. A list shows fewer fields than one record, and a field with `can_see` is left out for the requests that don't pass it, so the document lists every field that any HTTP view shows and promises none. A client may rely on a field's type whenever the field is there.

## What the document contains

For a repository named `posts`:

| Operation | `operationId` | Request | Success |
| --- | --- | --- | --- |
| `GET /api/restify/posts` | `posts.index` | The list parameters | `200`, `posts.page` |
| `POST /api/restify/posts` | `posts.store` | `posts.store` | `201`, `posts.document`, with a `Location` header |
| `GET /api/restify/posts/{id}` | `posts.show` | | `200`, `posts.document` |
| `PUT /api/restify/posts/{id}` | `posts.update` | `posts.update` | `200`, `posts.document` |
| `PATCH /api/restify/posts/{id}` | `posts.patch` | `posts.update` | `200`, `posts.document` |
| `DELETE /api/restify/posts/{id}` | `posts.delete` | | `204` |

Every repository has the bulk endpoints. Its actions and getters appear when it lists them:

| When the repository | Operation | `operationId` | Request | Success |
| --- | --- | --- | --- | --- |
| Lists any action | `GET /api/restify/posts/actions` and `/{id}/actions` | `posts.actions.index`, `posts.actions.show` | | `200`, the listing |
| Lists an action on the list, or a standalone one | `POST /api/restify/posts/actions` | `posts.actions.perform` | `?action=` and one of the actions' bodies | `200`, `Output` |
| Lists an action on one model, or a standalone one | `POST /api/restify/posts/{id}/actions` | `posts.actions.perform-show` | `?action=` and one of the actions' bodies | `200`, `Output` |
| Lists any getter | `GET /api/restify/posts/getters` and `/{id}/getters` | `posts.getters.index`, `posts.getters.show` | | `200`, the listing |
| Lists the getter `stats` on the list | `GET /api/restify/posts/getters/stats` | `posts.getter.stats` | | `200`, `Output` |
| Lists the getter `word-count` on one model | `GET /api/restify/posts/{id}/getters/word-count` | `posts.getter.word-count` | | `200`, `Output` |
| Always | `POST /api/restify/posts/bulk` | `posts.bulk-store` | An array of `posts.store` | `200`, `{ "data": [..] }` of models |
| Always | `POST /api/restify/posts/bulk/update` | `posts.bulk-update` | An array of `posts.update`, each with an `id` | `200`, `{}` |
| Always | `DELETE /api/restify/posts/bulk/delete` | `posts.bulk-delete` | An array of ids | `200`, `[]` |

The document also has these, each with its own `operationId`:

- `GET /api/restify/posts/filters` and `POST /api/restify/posts/apply-restify-advanced-filters`, for every repository. See [Advanced filters](filtering.md#advanced-filters).
- `DELETE /api/restify/posts/{id}/field/{field}`, when the repository's update input takes a file. See [Files and images](files.md).
- The nested routes of each relationship shown with `through` or `many`, such as `GET /api/restify/posts/{id}/comments/{relatedId}`, and the attach, detach and sync routes of each `Related::many`. See [Relationships](relationships.md#nested-routes).
- `GET /api/restify/search`, once, when a repository was added with `global_search`. See [Global search](filtering.md#global-search).

The account endpoints appear when the provider serves them, with the tag `accounts`. Their bodies come from the input structs their handlers check, so the document shows the same fields and rules, and `password_confirmation` where the `confirmed` rule asks for it. See [Authentication](authentication.md).

| When the provider calls | Operation | `operationId` | Success |
| --- | --- | --- | --- |
| `Restify::auth` | `POST /api/register`, `POST /api/login` | `register`, `login` | `200`, the user with `meta.token` |
| `Restify::auth` | `POST /api/logout` | `logout` | `200`, `{ "message" }` |
| `Restify::password_reset` | `POST /api/forgotPassword`, `POST /api/resetPassword` | `forgotPassword`, `resetPassword` | `200`, `{ "message" }` |
| `Restify::email_verification` | `POST /api/verify/{id}/{hash}` | `verify` | `200`, the user with `meta.message`, or a `302` to the frontend |
| `Restify::profile` | `GET` and `PUT /api/restify/profile` | `profile.show`, `profile.update` | `200`, `users.document` |

The answers refer to the user repository's schemas, such as `users.attributes`, when the provider serves that repository too. Register is the exception: it answers the new user as it was built, before the database filled in its defaults, so such a column shows `null`, as in Restify for PHP. Its `attributes` are described as any object. Each throttled route also lists its `429`.

Each operation has the tag `posts` and lists the errors it can answer, as in [Reference](reference.md#errors):

- Every operation can answer `401` and `403`.
- An operation on one record, with `{id}` in its path, can answer `404`. So can a bulk update or delete, when an item's id isn't found.
- An operation that reads input or list parameters can answer `422`. A getter takes no input, so it never does.

`?action=` lists the keys of the actions that run there. The request body is `anyOf` the actions' inputs, from their `#[derive(Schema)]`, with a required `repositories` for an action on the list. The body is never marked required, because an action whose input has no required field accepts an empty body. The operation's `description` lists each action's key with its `DESCRIPTION`. The `/action` paths, PHP's other name for `/actions`, aren't listed.

A bulk body is an array of 1 to `MAX_BULK` items (`minItems` and `maxItems`). Its `422` response says how the errors name an item: by its position, from `0`, such as `1.title`, and `items` for errors about the array itself. See [Bulk operations](bulk.md#errors).

The list parameters are: `search` when the repository has searchable fields, `sort` with the columns and scopes it may sort by, `page`, `perPage`, `related` when the repository lists relationships, and each filter column and scope with and without a leading `-`. Show takes `related` too.

The schemas live in `components.schemas`:

| Schema | Holds |
| --- | --- |
| `posts.store`, `posts.update` | The `#[derive(Schema)]` of `Store` and `Update` |
| `posts.attributes` | The repository's fields, none required |
| `posts.resource` | `id` (a string), `type` (the repository's `resource_type`), `attributes`, and `relationships` when the repository lists relationships |
| `posts.document` | `{ "data": resource }` |
| `posts.page` | `meta`, `links` and `data`, as in [Reference](reference.md#a-page) |
| `posts.list` | `{ "data": [resource, ..] }`, what a bulk create or update answers |
| `Relationship` | One `RelatedResource`, a list of them, or `null` |
| `RelatedResource` | A related model: `id`, `type` (its table) and `attributes` |
| `Output` | What an action or getter answered. Restify can't see what `handle` returns, so it may be any JSON |
| `Error`, `ValidationError` | `{ "message" }` and `{ "message", "errors" }` |

`relationships` lists each name from `Repository::related`, and none is required, because a response has only the ones the request named. Restify doesn't know whether a relationship holds one record or many, so each is a `Relationship`: one record, a list, or `null`. See [Relationships](relationships.md#what-a-response-contains).

The example app's tests check the document against the official OpenAPI 3.1 schema, and check real responses against the document's own schemas.

## What it leaves out

- **The MCP server.** It speaks JSON-RPC, not REST. MCP clients read its tools with `tools/list`. See [MCP](mcp.md).
- **Authentication.** Restify can't see what a middleware checks, so the document has no `securitySchemes`.
- **Filter value types.** A filter is described as text, the way the query string carries it.
- **What an action or getter returns.** `handle` returns a `Reply` of any JSON, which has no schema.
- **Numeric ids in bulk bodies.** A bulk update or delete also accepts an id as a number, but the document lists ids as strings.
- **Extra fields in a body.** The input schemas refuse properties they don't declare. An HTTP endpoint and an MCP tool ignore such fields instead.
