api-service-pattern

Structures backend API endpoints with thin handlers, service layers, and repository data access.

1|Updated Jun 3, 2026
One-click install
npx skills add https://github.com/tanveerriaz/Skillz --skill api-service-pattern-tanveerriaz
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: api-service-pattern
Source: https://github.com/tanveerriaz/Skillz/tree/main/skills/api-service-pattern
Command: npx skills add https://github.com/tanveerriaz/Skillz --skill api-service-pattern-tanveerriaz

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Backend endpoints rot when route handlers accumulate business logic and raw SQL, making APIs untestable, insecure, and hard to extend. This Skill enforces a clean layering pattern so endpoints stay maintainable as the codebase grows. ## Core Features & Use Cases - Layered Architecture: Separates concerns into thin route handlers, framework-agnostic service layers, and repository-based data access with dependency injection. - Boundary Validation & Error Handling: Validates and types input at the boundary, maps domain errors to consistent HTTP status codes, and prevents leaking stack traces or secrets. - Use Case: When adding a new POST /orders endpoint, the handler validates the request body, delegates to an order service containing business rules, and the service calls a repository for storage — letting you unit test the logic with a fake repository and no web framework. ## Quick Start Refactor my route handler so it only validates input and delegates to a service layer with a repository for data access.

Frequently Asked Questions about api-service-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a backend API with a service layer?▼

Keep the route handler thin: it validates and types the request, delegates to a framework-agnostic service holding business logic, and formats the response. The service accesses storage only through a repository interface injected as a dependency.

How to refactor a fat route handler into clean layers?▼

Move business rules out of the handler into service functions that are unit-testable without HTTP, and move all storage calls behind a repository. The handler then only validates input, enforces authorization, calls the service, and shapes the response.

When should I skip the service and repository layers?▼

For trivial CRUD endpoints, a lighter version is acceptable — do not add layers that earn nothing. However, always keep input validation and parameterized queries regardless of how thin the endpoint is.

Where should authorization checks go in a layered API?▼

Enforce authorization in the route handler or middleware before invoking the service, never only deep inside business logic. Placing it deep in the code lets a new caller bypass the check entirely.

How do I handle errors consistently across API endpoints?▼

Map domain errors to appropriate HTTP status codes and return a uniform error shape, such as mapping an OutOfStockError to HTTP 409. Never return stack traces, secrets, or internal details to clients.