agentleFS
ci4meci4me/CQRSTemplate/.agents/skills/api-endpoint-creation/SKILL.md

api-endpoint-creation

Skill0 starsChanged 6 months agoMITView on GitHub

Add a new REST API endpoint following the CQRS pattern. Use when creating new API routes with commands, queries, handlers, and DTOs.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every API endpoint in this repo follows CQRS:

    line 10
  2. Very specific:

    Create app/Domain/{Domain}/DTOs/{Domain}DTO.php with fromEntity() factory.

    line 113
  3. Distinctive:

    No business logic in controllers

    line 120
  4. Very specific:

    Always run composer check after changes

    line 124

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Adding a New API Endpoint (CQRS Pattern)

Overview

Every API endpoint in this repo follows CQRS:

  • Write operations (POST/PUT/DELETE) → Command + CommandHandler
  • Read operations (GET) → Query + QueryHandler → returns DTO

Step-by-Step

1. Create Command/Query DTO

For writes — app/Domain/{Domain}/Commands/{Action}/{Action}Command.php:

<?php
declare(strict_types=1);
namespace App\Domain\{Domain}\Commands\{Action};

final readonly class {Action}Command
{
    public function __construct(
        public string $field1,
        public int $field2,
    ) {}
}

For reads — app/Domain/{Domain}/Queries/{Query}/{Query}Query.php:

<?php
declare(strict_types=1);
namespace App\Domain\{Domain}\Queries\{Query};

final readonly class {Query}Query
{
    public function __construct(
        public int $id,
    ) {}
}

2. Create Handler

app/Domain/{Domain}/Commands/{Action}/{Action}Handler.php:

<?php
declare(strict_types=1);
namespace App\Domain\{Domain}\Commands\{Action};

use App\Infrastructure\Bus\HandlerInterface;

final class {Action}Handler implements HandlerInterface
{
    public function __construct(
        private {Domain}RepositoryInterface $repository,
        private EventDispatcher $eventDispatcher,
        private LoggerInterface $logger,
    ) {}

    public function handle({Action}Command $command): mixed
    {
        // Validate, create entity, persist, dispatch events
    }
}

3. Register in ServiceProvider

app/Domain/{Domain}/{Domain}ServiceProvider.php — add to registerCommands():

$commandBus->register(
    {Action}Command::class,
    new {Action}Handler($repository, $eventDispatcher, $logger)
);

4. Add API Route

app/Config/Routes.php — inside the api/v1 group:

$routes->group('{domain}', ['filter' => ['jwt', 'role:admin']], static function ($routes) {
    $routes->get('', '{Domain}Controller::index');
    $routes->post('', '{Domain}Controller::create');
    $routes->get('(:num)', '{Domain}Controller::show/$1');
    $routes->post('(:num)', '{Domain}Controller::update/$1');
    $routes->post('(:num)/delete', '{Domain}Controller::delete/$1');
});

5. Create/Update API Controller

app/Controllers/Api/{Domain}Controller.php — thin controller that dispatches:

public function create(): ResponseInterface
{
    $data = $this->request->getJSON(true);
    $command = new {Action}Command(
        field1: $data['field1'],
        field2: (int) $data['field2'],
    );
    $result = $this->commandBus->dispatch($command);
    return $this->respond(['id' => $result], 201);
}

6. Return DTOs from Query Handlers

Create app/Domain/{Domain}/DTOs/{Domain}DTO.php with fromEntity() factory. Query handlers must return DTOs, never domain entities.

Conventions

  • Commands/Queries are final readonly DTOs
  • Handlers implement HandlerInterface
  • No business logic in controllers
  • API controllers extend ResourceController or use ResponseTrait
  • JWT auth filter on protected endpoints
  • Rate limiting on public auth endpoints: ['filter' => 'ratelimit:5,300']
  • Always run composer check after changes

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.