A module writes no controller, no route and no documentation page. It registers
what it has, and paging, sorting, permissions, the error shape, the admin’s
reference page and openapi.json are applied to it from one place.
Registering
Answer the api.resources hook in your provider’s boot():
use Torgash\Api\ApiResources;
hook_listen('api.resources', function () {
ApiResources::register('admin.widgets', [
'label' => 'Widgets', // English; the translation goes into the module's lang/ru.json
'description' => 'What a widget is, in a sentence or two.',
'permission' => 'widgets.manage', // a permission from manifest.json
'query' => fn () => Widget::query()->with('parts'), // eager-load here, not in serialize
'serialize' => fn (Widget $w) => ['id' => $w->id, 'sku' => $w->sku, 'stock' => $w->stock],
'sortable' => ['id', 'sku', 'created_at', 'updated_at'],
'lookup' => ['external_id', 'sku'],
'filters' => [
'status' => fn ($q, $v) => $q->where('status', $v),
'updated_after' => [
'description' => 'Everything touched since a moment (ISO 8601).',
'example' => '2026-09-01T00:00:00+03:00',
'apply' => fn ($q, $v) => $q->where('updated_at', '>=', $v),
],
],
'update' => fn (Widget $w, array $data) => $w->update(array_intersect_key($data, array_flip(['stock']))),
'writable' => ['stock' => 'integer'],
'schema' => [
'id' => 'integer',
'sku' => 'string',
'stock' => ['type' => 'integer|null', 'description' => 'Units on hand.'],
],
]);
});
The resource is at once available at /api/v2/admin/widgets and described in
openapi.json and in Data exchange → API reference.
The definition’s keys
| Key | What it is |
|---|---|
| the name | admin.<resource> or shop.<resource> — the half and the address |
label, description | English strings; translated by the module’s lang files |
permission | the permission the token’s owner must hold (admin. only) |
query | fn (): Builder — where the rows come from; visibility limits and with() go here |
serialize | fn (Model): array — what a row is answered with |
sortable | columns for order[…]; anything else is ignored |
lookup | columns a row can be named by in the address (besides the id) |
filters | name => fn ($query, $value) or ['apply' => fn, 'description' => …, 'example' => …] |
update | fn (Model, array $data) — a partial write; absent — the resource is read-only (405) |
writable | what PATCH takes — for the description |
create, creatable, create_description | fn (array $data): Model — creation through POST; only for rows the shop itself makes on request |
actions | name => ['description', 'body', 'apply' => fn (Model, array $data)] — POST …/{key}/actions/{name} |
schema | the answer’s fields — for the description |
A field’s type in schema / writable / body is a string ('integer',
'string|null') or an array with type, description, example, enum,
format and of (an object’s fields or each array item’s). Declare nothing
and the description honestly says “an open object” rather than inventing
fields.
schema, writable and the descriptions are read by nothing at runtime: the
documentation is written from them. A description beside the code does not go
stale the way a separate document does.
Two halves — two resources
admin.widgets sits behind a token and a permission and answers with
everything. shop.widgets is public and read-only however it is registered
(only GET is routed for the storefront half, and currently only for
products and categories). When the storefront needs a narrower version of
the same table, register two resources rather than one that hides fields
conditionally — that is how a cost price eventually reaches a storefront.
Actions instead of writing state
Everything that is a decision is an action:
'actions' => [
'ship' => [
'description' => 'Hand it to the carrier, running the shop’s own code.',
'body' => ['tracking' => ['type' => 'string', 'example' => '20450012345678']],
'apply' => fn (Widget $w, array $data) => app(Shipper::class)->ship($w, $data['tracking'] ?? null),
],
],
An action that returns a model is answered with it; otherwise with the row’s
fresh(). State errors are a ValidationException (422), not 403: the
problem is the row, not who is asking.
No N+1 in lists
Before a list page is serialised the api.resource.listed hook is announced
with ['resource' => …, 'items' => Collection]. If a serializer needs a lookup
per row, answer that hook with one query for the whole page and keep the result
in a per-request singleton (not a static: under Octane it would outlive the
request).
Adding to another module’s answer
A module can add fields to another module’s answer without touching it:
api.shop.product.serialize— the storefront’s product (['product' => $p]);api.order.serialize— an order in the admin half (['order' => $order]).
hook_listen('api.order.serialize', fn (array $row, array $ctx) => $row + [
'loyalty_tier' => app(Tiers::class)->for($ctx['order']->user_id),
]);
An address that is not a resource
The cart and the checkout are not a table with a serializer. A module registers
such addresses itself and describes them through the api.openapi.paths hook:
hook_listen('api.openapi.paths', fn (array $paths) => $paths + [
'/pay/acme/callback' => ['post' => [
'tags' => ['Shop · Payments'],
'summary' => 'Acme’s callback',
'security' => [],
'responses' => ['200' => ['description' => 'Recorded.']],
]],
]);
Routes that need a token go behind the same middleware as the API:
throttle:torgash-api, Torgash\Api\Http\Authenticate,
Torgash\Api\Http\ResolveContext.
More about the module on its page.