Backend Service Guidelines
Purpose
Provide a practical, provider-agnostic checklist and working patterns for building and refactoring backend HTTP services with a layered architecture.
When to use
Use the apply-backend-service-guidelines skill when you are:
- Adding or changing HTTP endpoints (routes, controllers)
- Implementing business logic (services) or data access (repositories)
- Introducing middleware (auth, validation, auditing, rate limits)
- Standardizing error handling, logging, and observability
- Designing configuration loading and validation
- Writing or fixing backend tests (unit and integration)
Avoid using the skill when:
- you are making a narrow, single-concern change (for example, only adjusting one validation rule) and you do not need a cross-layer consistency check
- you are doing one-off debugging with a known root cause and you only need a small targeted fix
Inputs
You SHOULD have:
- The endpoint(s) being added/changed (method + path) and expected request/response shapes
- The business rules (what MUST be enforced, what MAY be optional)
- Data model information (entities, persistence requirements)
- Existing conventions in the codebase (framework, validation library, ORM/query layer)
Outputs
The skill produces one or more of:
- A clear layering plan: route → controller → service → repository
- Implementation scaffolding (controller/service/repository skeletons)
- A validation schema and error contract
- A minimal test plan (unit + integration) with verifiable acceptance criteria
Architecture rules
These rules prevent “everything everywhere” backends.
Routes MUST only wire HTTP to handlers
- Register middleware and delegate to a controller.
- Routes MUST NOT contain business logic.
Controllers MUST only handle HTTP concerns
- Parse/validate inputs, translate errors to HTTP responses, and call services.
- Controllers SHOULD be thin and testable.
Services MUST contain business logic
- Services MUST be free of HTTP concepts (no
req, res).
- Services SHOULD orchestrate repositories and other services.
Repositories MUST isolate data access
- Repositories SHOULD hide ORM/query details from services.
- If the project is simple, repositories MAY be omitted; once queries become non-trivial, add a repository layer.
Error handling contract
- You MUST define an error taxonomy (validation, not found, forbidden, conflict, internal).
- You MUST return a consistent error shape across endpoints.
- Unknown errors MUST be logged and mapped to
5xx without leaking sensitive details.
Validation
- All external inputs (body, params, query) MUST be validated.
- Validation SHOULD happen at the controller boundary.
- Validation errors MUST map to
4xx with actionable messages.
Configuration
- Runtime configuration MUST be centralized and typed.
- Configuration SHOULD be validated at startup.
- Secrets MUST NOT be logged and SHOULD be injected via the runtime environment or a secrets manager.
Testing expectations
- Services MUST have unit tests for core business rules.
- Endpoints SHOULD have integration tests for request/response + persistence behavior.
- Tests SHOULD cover:
- Happy path
- One representative validation failure
- One representative authorization/permission failure (if applicable)
Steps
Clarify the API contract
- Define request schema and response schema.
- Decide status codes for success and known failure modes.
Choose the layering
- Add/extend: route → controller → service → repository (as needed).
Implement validation
- Define a schema for inputs.
- Convert schema failures to your standard error response.
Implement business logic
- Place business rules in the service.
- Keep functions small; prefer pure helpers for tricky logic.
Implement data access
- Use repositories for complex queries and persistence workflows.
- Add transactions where “all-or-nothing” semantics are required.
Add observability
- Add structured logs around boundaries (start/end, key identifiers).
- Capture exceptions in your error tracker and include correlation IDs if available.
Add tests
- Unit tests for service rules.
- Integration tests for the endpoint behavior.
Verify
- Run the service locally or in CI.
- Validate response shapes and database side effects (if any).
Verification
Boundaries
- MUST NOT hardcode project-specific paths, scripts, or environment layouts in shared skills
- MUST NOT include credentials, secrets, or real tokens in examples
- MUST NOT log secrets or sensitive data
- SHOULD NOT introduce new patterns if the codebase already has an established convention unless there is a clear benefit and migration plan
- SHOULD NOT skip validation for any external input
- SHOULD NOT bypass error handling contract
Included assets
- Templates: see
./templates/ for controller/service/repository/error scaffolds.
- Examples: see
./examples/ for complete request flows and refactoring patterns.
1---2name: apply-backend-service-guidelines3description: Apply cross-cutting backend service patterns across routing, services, repositories, validation, configuration, errors, and tests.4---56# Backend Service Guidelines78## Purpose9Provide a practical, provider-agnostic checklist and working patterns for building and refactoring backend HTTP services with a layered architecture.1011## When to use12Use the apply-backend-service-guidelines skill when you are:13- Adding or changing HTTP endpoints (routes, controllers)14- Implementing business logic (services) or data access (repositories)15- Introducing middleware (auth, validation, auditing, rate limits)16- Standardizing error handling, logging, and observability17- Designing configuration loading and validation18- Writing or fixing backend tests (unit and integration)192021Avoid using the skill when:22- you are making a narrow, single-concern change (for example, only adjusting one validation rule) and you do not need a cross-layer consistency check23- you are doing one-off debugging with a known root cause and you only need a small targeted fix2425## Inputs26You SHOULD have:27- The endpoint(s) being added/changed (method + path) and expected request/response shapes28- The business rules (what MUST be enforced, what MAY be optional)29- Data model information (entities, persistence requirements)30- Existing conventions in the codebase (framework, validation library, ORM/query layer)3132## Outputs33The skill produces one or more of:34- A clear layering plan: **route → controller → service → repository**35- Implementation scaffolding (controller/service/repository skeletons)36- A validation schema and error contract37- A minimal test plan (unit + integration) with verifiable acceptance criteria3839## Architecture rules40These rules prevent “everything everywhere” backends.41421. **Routes MUST only wire HTTP to handlers**43 - Register middleware and delegate to a controller.44 - Routes MUST NOT contain business logic.45462. **Controllers MUST only handle HTTP concerns**47 - Parse/validate inputs, translate errors to HTTP responses, and call services.48 - Controllers SHOULD be thin and testable.49503. **Services MUST contain business logic**51 - Services MUST be free of HTTP concepts (no `req`, `res`).52 - Services SHOULD orchestrate repositories and other services.53544. **Repositories MUST isolate data access**55 - Repositories SHOULD hide ORM/query details from services.56 - If the project is simple, repositories MAY be omitted; once queries become non-trivial, add a repository layer.5758## Error handling contract59- You MUST define an error taxonomy (validation, not found, forbidden, conflict, internal).60- You MUST return a consistent error shape across endpoints.61- Unknown errors MUST be logged and mapped to `5xx` without leaking sensitive details.6263## Validation64- All external inputs (body, params, query) MUST be validated.65- Validation SHOULD happen at the controller boundary.66- Validation errors MUST map to `4xx` with actionable messages.6768## Configuration69- Runtime configuration MUST be centralized and typed.70- Configuration SHOULD be validated at startup.71- Secrets MUST NOT be logged and SHOULD be injected via the runtime environment or a secrets manager.7273## Testing expectations74- Services MUST have unit tests for core business rules.75- Endpoints SHOULD have integration tests for request/response + persistence behavior.76- Tests SHOULD cover:77 - Happy path78 - One representative validation failure79 - One representative authorization/permission failure (if applicable)8081## Steps821. **Clarify the API contract**83 - Define request schema and response schema.84 - Decide status codes for success and known failure modes.85862. **Choose the layering**87 - Add/extend: route → controller → service → repository (as needed).88893. **Implement validation**90 - Define a schema for inputs.91 - Convert schema failures to your standard error response.92934. **Implement business logic**94 - Place business rules in the service.95 - Keep functions small; prefer pure helpers for tricky logic.96975. **Implement data access**98 - Use repositories for complex queries and persistence workflows.99 - Add transactions where “all-or-nothing” semantics are required.1001016. **Add observability**102 - Add structured logs around boundaries (start/end, key identifiers).103 - Capture exceptions in your error tracker and include correlation IDs if available.1041057. **Add tests**106 - Unit tests for service rules.107 - Integration tests for the endpoint behavior.1081098. **Verify**110 - Run the service locally or in CI.111 - Validate response shapes and database side effects (if any).112113## Verification114115- [ ] Routes delegate to controllers without business logic116- [ ] Controllers validate inputs and map errors consistently117- [ ] Services contain business rules and are HTTP-free118- [ ] Repositories isolate data access (when used)119- [ ] Error responses follow the standard shape120- [ ] Unit tests cover core business rules121- [ ] Integration tests cover at least one happy path per endpoint122123## Boundaries124125- MUST NOT hardcode project-specific paths, scripts, or environment layouts in shared skills126- MUST NOT include credentials, secrets, or real tokens in examples127- MUST NOT log secrets or sensitive data128- SHOULD NOT introduce new patterns if the codebase already has an established convention unless there is a clear benefit and migration plan129- SHOULD NOT skip validation for any external input130- SHOULD NOT bypass error handling contract131132## Included assets133- Templates: see `./templates/` for controller/service/repository/error scaffolds.134- Examples: see `./examples/` for complete request flows and refactoring patterns.