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.
In this repository14 other files it gives its agents.
- .claude/CLAUDE.mdCLAUDE.md
- database-migrationsSkill
- local-dev-testingSkill
- testing-cqrs-securitySkill
- business-rule-additionSkill
- chrome-devtools-expertSkill
- code-reviewSkill
- context7-docsSkill
- domain-scaffoldingSkill
- markitdown-converterSkill
- playwright-automationSkill
- property-additionSkill
- serena-code-generatorSkill
- strategic-plannerSkill
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
- Distinctive: line 10
Every API endpoint in this repo follows CQRS:
- Very specific: line 113
Create app/Domain/{Domain}/DTOs/{Domain}DTO.php with fromEntity() factory.
- Distinctive: line 120
No business logic in controllers
- Very specific: line 124
Always run composer check after changes
≈ “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.
Files
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 readonlyDTOs - Handlers implement
HandlerInterface - No business logic in controllers
- API controllers extend
ResourceControlleror useResponseTrait - JWT auth filter on protected endpoints
- Rate limiting on public auth endpoints:
['filter' => 'ratelimit:5,300'] - Always run
composer checkafter changes
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
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.

