Installation

The REST API is an optional package. Install it only in projects that need it:
Token routes need the crudadmin/helpers authentication package as well. Without it, only the Swagger UI of signed in administrators is registered.

Endpoints

The API reads and writes rows through the same code as the administration. It applies the model permissions (read, insert, update, delete), scopeAdminRows(), validation, request mutators and uploads, history, admin rules and model events. A row hidden in the administration is not found by the API either. Models with $insertable, $editable or $deletable set to false refuse the same actions as in the administration. File fields are returned as URLs, multiple file fields as a list of URLs.

Tokens

POST /admin/api/v1/auth/login issues a Sanctum token. Send it as Authorization: Bearer {token}.
Any other value answers 422 before the credentials are checked. The response lists the token abilities in data.token.abilities, e.g. ["api", "api:read"]. Clients which create, update or delete rows have to ask for "access": "write". The permissions of the administrator apply on top of the token: a write token of an administrator without the update permission of a model can not update it. /admin/api/v1/models and the OpenAPI description show only the operations the token and the permissions allow. Tokens of the REST API are valid only for /admin/api/v1/*. The administration refuses them with 403, so a leaked API token does not open the admin interface. Tokens with the * ability, created before v6 or by createToken() without abilities, keep access to both the REST API and the administration. Issue a token for your own integration with explicit abilities:

OpenAPI and Swagger

/admin/api/v1/openapi.json describes every model the token may read: its columns and types, required fields, relations, and only the operations the administrator is allowed to use. Give it to API clients and AI agents, so they learn the models and the endpoints without further documentation. Limit it to some models with ?models=products,orders. Signed in administrators browse the same description in the Swagger UI at /admin/api/v1/docs. It loads the description from /admin/api/v1/docs/openapi.json with the admin session.

Responses

Responses follow Laravel API Resources. The list is paginated:
GET /admin/api/v1/models/orders?per_page=2
Rows contain the API columns of the model (id, foreign keys and fields) and the existing created_at, updated_at, published_at and _order. Password fields, inaccessible fields, $hidden attributes and table columns which are not fields (e.g. remember_token, logout_date, deleted_at) are never returned, selected, sorted or filtered. Passwords can only be written.

Query parameters

The parameters follow the conventions of spatie/laravel-query-builder (the JSON:API style). include and fields also apply to one row, and to the row returned after create and update. The parameters reach only what the API describes, the same for every token:
  • include accepts relations of the model: its belongsTo / belongsToMany fields, child models and parent models of $belongsToModel, by their camelCase names listed in /admin/api/v1/models. The administrator needs the read permission of the related model, and scopeAdminRows() of the related model limits the included rows as in the administration.
  • filter, sort and fields accept only the API columns and the timestamps above.
  • Scope filters apply only scopes declared on the model itself, internal scopes of CrudAdmin are not available.
Other values are refused with 400.

Customizing the response

app/Models/Order.php
Requests are logged by default, see the api configuration.