Recipes
Strict agent schemas (no ids)
An agent does best with the words a person would use: a post's title, not its id. agentSchema() gives agents their own input, and fromAgent() turns it into the canonical input that schema() describes. The canonical input is then validated again, and authorize() and handle() see only canonical input, whichever door the call came through.
<?php
namespace App\Actions;
use AgenticActions\Action;
use AgenticActions\ActionContext;
use AgenticActions\Attributes\Expose;
use AgenticActions\Effect;
use AgenticActions\Refusal;
use App\Models\Post;
use App\Models\User;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Support\ValidatedInput;
#[Expose]
final class ArchivePost extends Action
{
protected string $description = 'Archive one of the author\'s posts, named by its exact title.';
protected ?Effect $effect = Effect::Write;
/**
* The canonical input, for the CLI and your own code.
*/
public function schema(JsonSchema $schema): array
{
return ['post' => $schema->integer()->required()];
}
/**
* What an agent is offered instead: no id.
*/
public function agentSchema(JsonSchema $schema): ?array
{
return ['title' => $schema->string()->required()->description('The exact title of the post.')];
}
/**
* Find the post among the author's own, or refuse with the titles they do have.
*/
public function fromAgent(ValidatedInput $input, ActionContext $context): array
{
$author = $context->actor(User::class);
$post = $author->posts()->where('title', $input->string('title')->toString())->first();
if ($post === null) {
throw Refusal::make(__('You have no post with that title.'))
->on('title')
->listing($author->posts()->pluck('title')->all());
}
return ['post' => $post->getKey()];
}
/**
* Only signed-in authors see the tool.
*/
public function shouldRegister(ActionContext $context): bool
{
return $context->actor instanceof User;
}
/**
* The post must be the actor's own, whoever named it.
*/
public function authorize(ActionContext $context, ValidatedInput $input): bool
{
return $context->actor(User::class)->posts()->whereKey($input->integer('post'))->exists();
}
/**
* Archive it.
*/
public function handle(ActionContext $context, ValidatedInput $input): void
{
$context->find(Post::class, $input->integer('post'))->update(['status' => 'archived']);
}
}- A missing title comes back to the model as the refusal's sentence, followed by the author's own titles framed as data, so it can pick the right one.
- Under a bare
#[Expose], an action with anagentSchema()gets no generated route: its canonical input carries ids an agent never saw, not what a form sends, so write that route by hand if a form needs one. Namingweb: trueon it is an error. When agents only need to give more fields than the web, keepschema()alone and name them inrequiredForAgents()instead: the route stays. fromAgent()runs beforeauthorize()sees any input. Whenauthorize()takesValidatedInput,actions:checkrequiresshouldRegister()too, so a caller who may not use the action never reachesfromAgent(), unless thatValidatedInputmay be null:authorize(ActionContext $context, ?ValidatedInput $input = null)also runs beforefromAgent(), with null, and must refuse there every caller who may not use the action.- To keep ids away from agents everywhere, add them to the forbidden keys in
config/agentic-actions.php. Any action that still offers one is then left out of every toolset, andactions:checknames it:
'agents' => [
'forbidden_keys' => ['*password*', '*secret*', '*token*', 'api_key', 'id', '*_id', 'uuid'],
],Livewire and Blade controllers
Any code already inside a request calls an action in-process with run(). It returns what handle() returned, throws a ValidationException for invalid input, and throws a Refusal for anything else that stopped the call. One catch turns a refusal into a form error:
public function store(Request $request): RedirectResponse
{
try {
$post = CreatePost::run($request->only('title', 'body', 'excerpt'), ActionContext::http($request->user()));
} catch (Refusal $r) {
throw $r->toValidationException();
}
return to_route('posts.show', $post);
}toValidationException() puts the message on the refusal's field, or on action, in the default bag; pass another bag as its first argument. The same try works in a Livewire component's method. run() reaches any action class, exposed or not, so a controller like this one needs no #[Expose].
A route whose controller is the action itself, Route::post('posts', CreatePost::class), works too: it runs the same pipeline as a generated route, and the route you wrote is its own allowlist. Do not also expose the action on the web; actions:check fails an action that has both a generated and a hand-written route.
Forms on Inertia React
useAction() is Inertia's useHttp for an action's typed definition, with run() and refusal added:
import { type FormEvent } from 'react';
import { useAction } from '@agentic-actions/client/react';
import { createPost } from '@/agentic/actions';
export function NewPost() {
const post = useAction(createPost(), { title: '', body: '' });
async function save(event: FormEvent) {
event.preventDefault();
if ((await post.run()) !== undefined) {
post.setData({ title: '', body: '' });
}
}
return (
<form onSubmit={save}>
<input value={post.data.title} onChange={(event) => post.setData('title', event.target.value)} onBlur={() => post.validate('title')} />
{post.errors.title && <p>{post.errors.title}</p>}
<textarea value={post.data.body} onChange={(event) => post.setData('body', event.target.value)} />
{post.refusal && <p role="alert">{post.refusal}</p>}
<button disabled={post.processing}>Save draft</button>
</form>
);
}run()sends anIdempotency-Key, resolves the output, and reloads what the action's$touchesname. It resolvesundefinedwhen the call did not go through: field errors land onerrors, and any other answer below 500 (a refusal, 403, 404) lands onrefusal.validate()is Precognition's. A 401, 403, 404, 409 or 423 lands onrefusalthere too, and the next check clears it.- After a successful
run(), Inertia makes the values it sent the form's new defaults, soreset()brings those values back instead of emptying the form. To start the next entry empty, set the fields yourself, assave()does above.
File uploads
A file is a string() field with the binary format. It arrives as multipart/form-data, and handle() reads it as an Illuminate\Http\UploadedFile. This action takes a CSV of posts for a team, stores it, and queues a job of your own to import it:
<?php
namespace App\Actions;
use AgenticActions\Action;
use AgenticActions\ActionContext;
use AgenticActions\Attributes\Expose;
use AgenticActions\Effect;
use App\Jobs\ImportPostsFromCsv;
use App\Models\Team;
use App\Models\User;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Support\ValidatedInput;
#[Expose(web: true)]
final class UploadPosts extends Action
{
protected string $description = 'Import draft posts from a CSV file: one post per line, its title, then its body.';
protected ?Effect $effect = Effect::Write;
/**
* The file.
*/
public function schema(JsonSchema $schema): array
{
return [
'file' => $schema->string()->format('binary')->required(),
];
}
/**
* What schema() does not say about a file: its type, and its size in kilobytes.
*/
public function rules(ActionContext $context): array
{
return ['file' => ['mimes:csv,txt', 'max:1024']];
}
/**
* Any member of the team: membership is checked before this runs.
*/
public function authorize(ActionContext $context): bool
{
return $context->actor instanceof User;
}
/**
* Keep the file, and import it in a worker.
*/
public function handle(ActionContext $context, ValidatedInput $input): void
{
$path = $input->input('file')->store('imports');
ImportPostsFromCsv::dispatch(
$context->actor(User::class)->getKey(),
$context->tenant(Team::class)->getKey(),
$path,
$context->locale,
);
}
}In the tenant group of mounting the routes, the action answers POST /teams/{team}/actions/upload-posts, named teams.actions.upload-posts. Every caller sends the file as multipart/form-data:
- A Blade form sets
enctype. A file the rules refuse comes back as an error onfile:
<form method="POST" action="{{ route('teams.actions.upload-posts', $team) }}" enctype="multipart/form-data">
@csrf
<input type="file" name="file" accept=".csv">
@error('file') <p>{{ $message }}</p> @enderror
<button>Import</button>
</form>- The TypeScript client types the field
File | Blob. Pass theFileitself, not aFormDataof your own:callAction()builds the multipart body once the input holds a file, anduseAction()sends one as multipart too, through Inertia'suseHttp. For a file the rules refuse,callAction()rejects with anActionValidationErrorwhoseerrors.fileholds the message, whileuseAction()puts the message on itserrors.fileand itsrun()resolvesundefined.
import { callAction } from '@agentic-actions/client';
import { uploadPosts } from '@/agentic/actions';
export async function uploadCsv(file: File): Promise<void> {
await callAction(uploadPosts({ team: 'acme' }), { file });
}- A token client posts the same form to the
routes/api.phpgroup, with a token that can write:
curl https://example.com/api/teams/acme/actions/upload-posts \
-H "Authorization: Bearer $TOKEN" -H "Accept: application/json" \
-F file=@posts.csvA JSON body cannot carry a file, so the field fails its file rule there. handle() runs while the request is open, so it only stores the file, on the default disk under a name Laravel picks, and queues the import with the path: a job's input must serialize, and an uploaded file does not.
An action whose schema() holds a file is not offered to agents or MCP clients. A model's tool call carries JSON arguments, never a file, and every call a model makes is validated against schema() too, so an agentSchema() without the file changes nothing. The package leaves the action out of every agent's tools and the MCP server's (your tests and local requests throw instead), and actions:check fails it while its #[Expose] opens agents or MCP, as a bare #[Expose] would. web: true keeps it to the route. To let an agent import posts, give it another action that takes what a model can send: CreatePost once for each post, or an action that takes a list of titles and bodies.
Only a call on the Http surface takes a file: a route, or your own code with ActionContext::http(). A call from the CLI, a queued run or ActionContext::system() that reaches validation fails with UnsupportedSchema, so actions:run exits with 4. Under system(), UploadPosts refuses before that, since its authorize() wants a person.
Your own jobs
A job you write runs actions with run(), as a controller does. Build the context for the person the work belongs to, ActionContext::http($user, $team, $locale), and each call goes through the whole pipeline as theirs. This job imports the file the upload stored, one post per line, with CreatePost made tenant-scoped as in a tenant-scoped action:
<?php
namespace App\Jobs;
use AgenticActions\ActionContext;
use AgenticActions\Refusal;
use App\Actions\CreatePost;
use App\Models\Team;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
final class ImportPostsFromCsv implements ShouldQueue
{
use Queueable;
public function __construct(
public int $authorId,
public int $teamId,
public string $path,
public string $locale,
) {}
/**
* Draft one post per line, as the author in the team, and keep the reason for each line it skips.
*/
public function handle(): void
{
$author = User::find($this->authorId);
$team = Team::find($this->teamId);
// Deleted since the upload, soft-deleted included: import nothing.
if ($author === null || $team === null) {
Storage::delete($this->path);
return;
}
$context = ActionContext::http($author, $team, $this->locale);
$csv = Storage::readStream($this->path);
$skipped = [];
$line = 0;
while (($row = fgetcsv($csv, escape: '')) !== false) {
$line++;
try {
CreatePost::run(['title' => $row[0] ?? null, 'body' => $row[1] ?? null], $context);
} catch (ValidationException $e) {
$skipped[$line] = $e->getMessage();
} catch (Refusal $e) {
$skipped[$line] = $e->translate($context->locale);
}
}
fclose($csv);
Storage::delete($this->path);
// Tell the author which lines were skipped and why, such as in a notification.
}
/**
* The last try crashed: remove the file all the same.
*/
public function failed(): void
{
Storage::delete($this->path);
}
}The job takes keys rather than models and finds the author and the team when it runs: a person or team deleted since the upload, soft-deleted included, imports nothing, and the file goes either way, by failed() once the last try has crashed. That is where it differs from the job dispatch() queues, which the package drops when its person or tenant is gone: a job of your own that took the models would run for a soft-deleted author, since Laravel restores one and run() does not refuse it, and would fail on one deleted for good, leaving the file behind.
The loop is yours, so the job can also count progress, stop early or send a summary. Each run() is a whole call, as the author in the team:
shouldRegister(), membership in the team (asked again for every line),authorize()and validation run, thenhandle(). Each call fires its own event, on theHttpsurface its context names, and its write reaches the team's open pages through the change feed.run()returns whathandle()returned. Invalid input throws aValidationException, and a refusal, a deniedauthorize()or a person no longer in the team throws aRefusal, so the job skips that line and goes on. Anything else is a crash: it firesActionFailedand fails the job, and a retry starts again at the first line, so keep each call safe to repeat (CreatePostrefuses a title the author already has).Actions::attempt()makes the same call and returns anOutcomeinstead, for a loop that should report a crash and go on.- A worker runs in
app.locale, so the job takes the person's locale from the context that queued it, and validation messages and refusals come back in that language.
What each call is not held to:
#[Expose]:run()reaches any action (doors).- A token's limits.
http()takes the default guard when you build the context, which in a worker isauth.defaults.guard: under the session guard (webin a new app) the calls have the person's full access, and under a token guard every call reads as not found. - A model's limits. In a worker, the calls are not model-driven, even when an agent's tool or an MCP client led to the job, directly or from a queued run.
So whoever may start the job gets every call it makes. Queue it from code whose own checks cover that work, as UploadPosts, a Write, queues a job that only writes. For one call, or for items that stand alone, queue the action itself with CreatePost::dispatch($input, $context) (queued runs): that job keeps the caller's token limits and model origin and checks them again in the worker, each call retries on its own, and a person or team deleted before the run drops it. Write a job of your own when the work needs one place: a file to read, lines in order, progress, a summary.
With no person behind the work, such as a nightly import, build the context with ActionContext::system($team): there is no actor, no token check and no membership, and authorize() decides, so the action must let $context->isSystem() through. Never build one with ActionContext::queued(): the package builds it for its own queued runs, and it is @internal.
State created the first time it is read
A team's inbound address, made the first time anyone opens the page that shows it, in an app whose tenant is a team, set up as in tenants. When every team should have one from the start, create it with the team instead and backfill the teams that exist once (a Read that creates its own state); this recipe is for state that should exist only once someone looks.
The table holds one row per team, and the unique key on team_id is what keeps it at one when two first reads arrive at once:
Schema::create('team_inboxes', function (Blueprint $table) {
$table->id();
$table->foreignId('team_id')->unique()->constrained()->cascadeOnDelete();
$table->string('address')->unique();
$table->timestamps();
});Team gains public function inbox(): HasOne { return $this->hasOne(TeamInbox::class); }, and TeamInbox lists address in $fillable. The action adds the row in initialize(), which may only add rows to the tables $initializes lists, and handle() only reads it:
<?php
namespace App\Actions;
use AgenticActions\Action;
use AgenticActions\ActionContext;
use AgenticActions\Attributes\Expose;
use AgenticActions\Effect;
use App\Models\Team;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Support\Str;
#[Expose]
final class ShowTeamInbox extends Action
{
protected string $description = 'The address that forwards email into this team.';
protected ?Effect $effect = Effect::Read;
protected array $initializes = ['team_inboxes'];
public function outputSchema(JsonSchema $schema): array
{
return ['address' => $schema->string()->required()];
}
public function authorize(ActionContext $context): bool
{
return $context->actor !== null;
}
/**
* Add the team's inbox when it has none. The address comes from the server, never from input.
*/
public function initialize(ActionContext $context): void
{
$context->tenant(Team::class)->inbox()->firstOrCreate([], fn (): array => ['address' => Str::lower(Str::random(24))]);
}
/**
* @return array{address: string}
*/
public function handle(ActionContext $context): array
{
return ['address' => $context->tenant(Team::class)->inbox()->sole()->address.'@in.example.com'];
}
}firstOrCreate() reads first and adds the row only when it finds none, so every later call sends nothing but reads. When two first reads arrive at once, both find none and the unique key refuses the second insert: createOrFirst(), where firstOrCreate() ends, then reads the row the first one added, and both calls answer the same address. Inside a transaction it inserts in a savepoint, so the transaction goes on. Without the unique key the team would get two inboxes.
initialize() runs after every check, membership and authorize() included, so a person outside the team never adds one. Whoever may read the inbox may cause it to exist, a token with only actions:read included, and the action stays a Read for every caller: the copilot's row reads "Looking it up…", MCP clients read it as read-only, and nothing reaches the change feed. Its other rules, and when to use a Write instead, are in a Read that creates its own state.
A test runs it twice and finds one row:
use AgenticActions\ActionContext;
use App\Actions\ShowTeamInbox;
use App\Models\Team;
use App\Models\User;
it('gives a team one inbox, made the first time it is read', function () {
$user = User::factory()->create();
$team = Team::factory()->create();
$user->teams()->attach($team);
$first = ShowTeamInbox::run([], ActionContext::http($user, $team));
$second = ShowTeamInbox::run([], ActionContext::http($user, $team));
expect($second)->toBe($first)
->and($team->inbox()->count())->toBe(1);
});laravel-data
The validated input is a plain array, so a data object is one line inside handle(): PostData::from($input->all()). schema() stays the one place the input is described and validated.
Discovery layouts
Actions are found under app by default. Set discovery.paths in config/agentic-actions.php for other layouts; globs and absolute paths both work:
Modules/*/appfor nwidart/laravel-modulesapp-modules/*/srcfor internachi/modularsrc/Domainfor domain folders outsideapp
The scanner only reads files that mention AgenticActions\. An action that extends your own base class and never names the package itself goes in discovery.classes. php artisan actions:list prints the directories it walked when it finds nothing.
Optional modules
When some modules are installed in one deployment and left out of another, give each one a discovery group. A group lists its own paths (and classes), and owns its own exposure snapshot and TypeScript file, so the files of the core never mention a module, and actions:check passes whether the module is there or not:
'discovery' => [
'groups' => [
'core' => [
'paths' => ['app'],
'snapshot' => 'actions.exposure.json',
'typescript' => 'resources/js/agentic/actions.ts',
],
'billing' => [
'paths' => ['modules/Billing/src'],
'snapshot' => 'modules/Billing/actions.exposure.json',
'typescript' => 'modules/Billing/resources/js/actions.ts',
'optional' => true,
],
],
],- A group is installed when one of its
pathsexists. A group whose paths are all missing adds nothing: none of its actions or agents is found, even ones it lists underclasses, so it has no route, no agent tool and no MCP tool, and its snapshot and TypeScript file are neither written nor checked. The core's two files stay byte for byte as they were. - Only an
optionalgroup may be missing. A group is required unless it sets'optional' => true. A required group none of whose paths exists, such as a core path with a typo, fails the Discovery row ofactions:checkand failsactions:typescript, with or without--check, instead of passing while checking nothing. So do no group being installed at all, and a--groupthat names a group that is not installed here. - Re-cache on every deploy that adds or removes a module. Removing
modules/Billingremoves billing from every door once the deploy runsphp artisan optimize(orroute:cacheandactions:cache), as Deploying asks of every deploy. The manifest records which groups were installed when it was written, so a manifest from before the module was added or removed is ignored and the process scans instead (and reportsStaleActionManifestonce an hour). A route cache is the framework's own, though: until it is rebuilt, it still holds the routes of the module it was built with. - Each group's files hold only its own actions.
actions:check --updatewrites each installed group's snapshot from that group's own scan, andactions:checkcompares each one.actions:typescriptwrites each installed group's file with the routes of its own actions. Every file imports only@agentic-actions/clientand declares its ownJsontype, so a module's file never imports the core's and the core's never imports a module's. A group withouttypescriptwrites no file. Export names stay unique across every group's file, so one frontend can import them all. --groupnarrows the commands.actions:check --update --group=billingwrites only billing's snapshot, andactions:typescript --check --group=corechecks only the core's file. Every other row ofactions:checkstill runs over every installed group.- Every other rule is unchanged. Discovery groups decide where the scanner looks and which file records what it found. They do not change what an action exposes:
#[Expose], the surface switches,shouldRegister(),authorize(), toolsets and confirmations behave exactly as without groups. Action names stay unique across all installed groups. - A toolset is shared across groups. A module action with
#[Toolset('support')]joins the core'ssupporttoolset, so an agent of the core that usessupportgets that tool while the module is installed. The module's snapshot records it, not the core's, so review a module's snapshot with the toolsets of the whole app in mind. - A group's paths stay inside the app. A path with a
..segment, an absolute path outside the base path or a root whose real path leaves the base path (a symbolic link out of the app) is refused. So is an output that is not a.jsonsnapshot or a.tsfile, that is itself a symbolic link, or whose nearest existing directory really lies outside the base path, an unknown key, two groups that write the same file (a.json,./a.jsonandA.jsonare one file) and one class two groups both find. Each throwsMisconfiguredDiscoveryfromactions:cacheandoptimize, so a deploy stops there, and fails the Discovery row ofactions:checkandactions:typescript. A web request goes through the same table as an exposure error: a local request throws, and production reports it once an hour while nothing of any group is nominated, so no surface opens until it is fixed.
discovery.groups replaces discovery.paths, discovery.classes, snapshot and typescript.path, which are ignored while it is set; actions:check warns while discovery.paths or discovery.classes differs from its default, since what they list is no longer found. An app that sets no group keeps one implicit group, default, built from those four keys, and writes the same files it always did. A route to an action that no group finds, such as a hand-written route to a class outside every group, is exported nowhere, and actions:typescript warns about it: list the class under a group's classes.
A token reader for a JWT or API-key guard
The default reader knows three credentials: Laravel's session (full access), Sanctum tokens (their abilities) and Passport tokens (their read and write scopes, on the MCP URL of their OAuth connection only). Any other guard reads as having no abilities, so every action is not found for it. Bind your own AgenticActions\Contracts\ReadsTokenGrants to read its grants. A reader of your own replaces the one that keeps each OAuth connection to its URL and tenant, so actions:check fails while it is bound beside a Passport guard in mcp.middleware:
<?php
namespace App\Auth;
use AgenticActions\Contracts\ReadsTokenGrants;
use Illuminate\Auth\SessionGuard;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
use Laravel\Sanctum\PersonalAccessToken;
use Laravel\Sanctum\TransientToken;
final class AppTokenGrants implements ReadsTokenGrants
{
/**
* Null for a session, the credential's abilities for a token, nothing for anything else.
*
* @return list<string>|null
*/
public function grants(?Authenticatable $actor, Guard $guard): ?array
{
if ($guard instanceof SessionGuard) {
return null;
}
if ($guard instanceof ApiKeyGuard) {
return $guard->key()?->abilities ?? [];
}
$token = $actor !== null && method_exists($actor, 'currentAccessToken') ? $actor->currentAccessToken() : null;
return match (true) {
$token instanceof TransientToken => null,
$token instanceof PersonalAccessToken => is_array($token->abilities) ? array_values($token->abilities) : [],
default => [],
};
}
}ApiKeyGuard stands for your own guard class. Register the reader in a service provider's register():
$this->app->singleton(ReadsTokenGrants::class, AppTokenGrants::class);The rules for what you return:
nullmeans a session: full access. Return it only for a credential that really is one.[]means nothing: every action reads as not found.- Otherwise, a list of abilities from
config('agentic-actions.abilities'):actions:read,actions:write,actions:destructive,actions:external,*for all of them over HTTP, andtenant:{key}to bind the credential to one tenant's primary key.
Your reader replaces the default one, so keep the session and Sanctum branches if the app still uses them. This one reads a Sanctum token's stored abilities only, so test Sanctum calls with real tokens from createToken() rather than Sanctum::actingAs(), whose token has none stored. php artisan actions:list then shows "custom reader" with your class.
A public REST API from your actions
The generated routes name the action (POST /api/actions/publish-post). A public API usually names the resource instead. #[Rest] gives an action a route of that shape, and Actions::rest() registers those routes inside your own group. Each one is a generated route, so it runs the same pipeline as /actions/publish-post: exposure, the token's ability, the tenant and membership, both authorize() steps, validation, rate limits from your group's middleware, the events and the output schema. A Precognition: true request validates a draft without running it, as on a generated route. As there, a GET route answers 405 to a request that carries a session, so mount Actions::rest() in a token group.
use AgenticActions\Attributes\Expose;
use AgenticActions\Attributes\Rest;
#[Expose(web: true)]
#[Rest('GET', 'posts')]
final class ListPosts extends Action { /* Effect::Read */ }
#[Expose(web: true)]
#[Rest('POST', 'posts', created: true, location: 'posts/{id}')]
final class CreatePost extends Action { /* Effect::Write, outputSchema() has id */ }
#[Expose(web: true)]
#[Rest('GET', 'posts/{post}', bind: ['post' => 'post_id'])]
final class ShowPost extends Action { /* Effect::Read, schema() has post_id */ }
#[Expose(web: true)]
#[Rest('PATCH', 'posts/{post}')]
final class UpdatePost extends Action { /* Effect::Write, schema() has post */ }
#[Expose(web: true)]
#[Rest('DELETE', 'posts/{post}')]
final class DeletePost extends Action { /* Effect::Destructive, no outputSchema() */ }
#[Expose(web: true)]
#[Rest('POST', 'posts/{post}/publish')]
final class PublishPost extends Action { /* Effect::Write: a verb as a sub-resource */ }Mount them in routes/api.php, under a version:
use AgenticActions\Facades\Actions;
use Illuminate\Support\Facades\Route;
Route::middleware(['auth:sanctum', 'throttle:api'])
->prefix('v1')
->name('api.v1.')
->group(fn () => Actions::rest(tenant: false));
Route::middleware(['auth:sanctum', 'throttle:api'])
->prefix('v1/teams/{team}')
->name('api.v1.teams.')
->group(fn () => Actions::rest(tenant: true));That gives GET /api/v1/posts, POST /api/v1/posts, GET|PATCH|DELETE /api/v1/posts/{post} and POST /api/v1/posts/{post}/publish, named api.v1.rest.list-posts and so on. A repeated #[Rest] is an alias, named rest.{action}.2 and on. Actions::rest() takes only and except, as Actions::routes() does (which actions a mount serves). An action needs the web open for its #[Rest] routes to serve it, and Actions::routes() still serves its generated route wherever you mount that.
Path parameters are input. {post} fills the input key post, or the key bind names (['post' => 'post_id']), and is validated with the rest of the input, so /posts/abc against an integer key is a 422. The path wins: a request that also sends that key in the query or the body is refused with 422 on the key, and nothing runs. A tenant-scoped action finds its row with $context->find(Post::class, $input->integer('post')), so another tenant's id is a 404, like an id that does not exist.
Bindings read the original path values: an identity binding such as ['post' => 'post'] preserves the value, and mappings may exchange two parameter names without overwriting either source. The resulting input keys remain fixed and cannot also be sent in the query or body.
The methods follow the effect. actions:check's REST row fails a GET on anything but a Read, a DELETE on anything but a Destructive action, a POST, PUT or PATCH on a Read, and two web-open actions on one method and path (posts/{post} and posts/{id} count as one path). It also fails a path parameter schema() does not declare, a bind entry for a parameter the path lacks, created on anything but a POST, and a location without created or naming a key neither the output nor the path holds. Actions::rest() itself refuses a method other than GET, POST, PUT, PATCH and DELETE, and two routes on one method and path in one group.
The statuses are REST's:
| Case | Status |
|---|---|
| A Read | 200 with its output; over GET with an ETag, and 304 for a matching If-None-Match (reads over GET) |
created: true | 201 with the output, and Location when location is set: the group's prefix, then the location with each {key} filled from the route's parameters, then the output's top-level values |
An action whose outputSchema() declares nothing | 204 with no body |
| Any other success | 200 with the output |
| Invalid input, a refusal on a field, a path key sent twice | 422 |
| A refusal without a field | 409, or the status it sets |
| No such row, another tenant's row, a token without the ability | 404 |
authorize() said no | 403 |
| No user | 401 |
| A method the path does not serve | 405 |
A success is always JSON. The refusals come from your exception handler, so send Accept: application/json, or make the handler answer JSON for api/* with ->shouldRenderJsonWhen().
Confirmations follow the doors. A Destructive or External action waits for a person's confirmation only when a model drives the call. A person's token calling DELETE /api/v1/posts/{post} runs it at once, as POST /api/actions/delete-post does, provided the token holds actions:destructive.
Keep it on a token guard. A GET skips Laravel's CSRF check, and a browser sends a SameSite=Lax session cookie on a link from another site, so mount Actions::rest() in a token group such as auth:sanctum, never in the web group. actions:check fails a GET route whose group starts a session or authenticates with a session guard.
Version the mount, not the action. Keep each public URL's meaning fixed: mount under api/v1, and add fields only as optional input or extra output. A breaking change gets a new action on the same path under a v2 mount, such as #[Rest('GET', 'posts/{post}')] on ShowPostV2, mounted with Actions::rest(only: ...) under prefix('v2'), while v1 keeps its action. An action's $name is in its route name, so pin it once the API is public.
Describe it as OpenAPI. Actions::restRoutes() returns one row per registered #[Rest] route, read from the router, so it holds the real prefix:
[
'name' => 'api.v1.rest.show-post',
'method' => 'GET',
'path' => '/api/v1/posts/{post}',
'action' => 'show-post',
'class' => ShowPost::class,
'description' => 'Show one post.',
'effect' => 'read',
'ability' => 'actions:read',
'tenant_scoped' => false,
'parameters' => [['name' => 'post', 'input' => 'post_id', 'required' => true, 'schema' => ['type' => 'integer']]],
'input' => ['type' => 'object', 'properties' => [/* schema() without the path keys */]],
'input_in' => 'query', // 'body' for every other method
'output' => ['type' => 'object', 'properties' => [/* outputSchema() */]],
'status' => 200, // 201 for created, 204 for no output
]input and output are JSON Schema, which OpenAPI 3.1 takes as it is, so a document needs no other package:
$paths = [];
foreach (Actions::restRoutes() as $route) {
$operation = [
'operationId' => $route['action'],
'summary' => $route['description'],
'security' => [['sanctum' => [$route['ability']]]],
'parameters' => array_map(fn (array $parameter): array => [
'name' => $parameter['name'], 'in' => 'path', 'required' => true, 'schema' => $parameter['schema'],
], $route['parameters']),
'responses' => [(string) $route['status'] => $route['status'] === 204
? ['description' => 'Done.']
: ['description' => 'OK', 'content' => ['application/json' => ['schema' => $route['output']]]]],
];
if ($route['input_in'] === 'query') {
foreach ($route['input']['properties'] ?? [] as $name => $schema) {
$operation['parameters'][] = ['name' => $name, 'in' => 'query', 'required' => in_array($name, $route['input']['required'] ?? [], true), 'schema' => $schema];
}
} else {
$operation['requestBody'] = ['content' => ['application/json' => ['schema' => $route['input']]]];
}
$paths[$route['path']][strtolower($route['method'])] = $operation;
}actions:typescript exports each #[Rest] route by its route name, with its method and path: api.v1.rest.show-post becomes apiV1RestShowPost({ post: 1 }), whose input leaves out the key the path fills (which route names the URL).
TypeScript and Wayfinder
A generated route is an ordinary route whose controller is the action class, so Laravel Wayfinder sees it as it sees any other controller route. php artisan actions:typescript adds what Wayfinder does not know: the input and output types from schema() and outputSchema(), the description, and the touches keys. The two can live side by side. Whatever regenerates Wayfinder's files in your dev loop can run php artisan actions:typescript too, and php artisan actions:typescript --check in CI fails when the committed file is stale.