agentleFS
ErikaAX08ErikaAX08/erikas-skills/backend-api-standards/SKILL.md

backend-api-standards

Skill6 starsChanged 2 months agoApache-2.0View on GitHub

Defines mandatory standards for REST API contracts and data format, including unified response structure, OpenAPI documentation, ULID identifiers, JWT auth headers, mandatory entity fields, and HTTP status codes. Framework-agnostic — describes what the API must return, not how to build it.

The file

Show its rules and text

We split this skill into the rules it gives an agent

24 rules. About 0 are common, 19 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈19

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When designing or documenting a new API endpoint

    line 13
  2. Distinctive:

    When the user mentions "API", "endpoint", "REST", "response format"

    line 14
  3. Distinctive:

    When defining request/response schemas or data models

    line 15
  4. Distinctive:

    Every endpoint must be fully documented with:

    line 183
  5. Distinctive:

    All path, query, and header parameters

    line 185
  6. Distinctive:

    All possible response schemas (including errors)

    line 187
  7. Distinctive:

    All references between resources (foreign keys exposed in the API) must also use ULID strings.

    line 322
  8. Distinctive:

    Use structured logging (JSON) rather than plain text, so logs are machine-parseable.

    line 457
  9. Very specific:

    Use appropriate log levels: debug, info, warning, error, critical.

    line 470
  10. Distinctive:

    When an unhandled error occurs, log where it crashed (file, line, function) plus the full stack trace — server-side only:

    line 523
  11. Very specific:

    Response format: Every response MUST use { status, message, data, meta }

    line 556
  12. Distinctive:

    OpenAPI: Every endpoint MUST be documented with OpenAPI 3.0+

    line 557
  13. Distinctive:

    ULID: All resource IDs exposed by the API MUST be ULIDs

    line 558
  14. Very specific:

    Entity fields: Every resource MUST include created_at, updated_at, and metadata

    line 560
  15. Distinctive:

    Data formats: Follow the conventions table (snake_case, ISO 8601 UTC, decimals as strings, etc.)

    line 561
  16. Distinctive:

    HTTP codes: Use the correct status code for every response

    line 562
  17. Distinctive:

    Error format: Always follow the unified error structure — never expose internals

    line 563
  18. Distinctive:

    Sensitive data: Never return passwords, secrets, or stack traces

    line 564
  19. Distinctive:

    Logging: Every API MUST implement structured logging with a per-request correlation id, logging the full request and response and, on …

    line 566
  20. Distinctive:

    When designing a new API endpoint:

    line 570
  21. Very specific:

    Ensure the response uses the unified { status, message, data, meta } format

    line 573
  22. Very specific:

    Ensure every resource exposes id (ULID), created_at, updated_at, and metadata

    line 574
  23. Distinctive:

    Document all success and error responses in OpenAPI

    line 576
  24. Distinctive:

    Ensure requests and errors are logged with a correlation id (structured logging)

    line 578

≈ “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

Backend API Standards Skill

This skill establishes mandatory standards for REST API contracts and data format. It defines what the API must return and how data must be shaped — it does not prescribe backend structure, folders, or implementation details.

When to Use This Skill

  • When designing or documenting a new API endpoint
  • When the user mentions "API", "endpoint", "REST", "response format"
  • When defining request/response schemas or data models
  • To ensure all APIs follow the same contract conventions

Core Standards

1. Unified Response Format

Every single API response MUST follow this exact structure:

{
  "status": "success",
  "message": "Human-readable description of the result",
  "data": {},
  "meta": null
}

Response Fields Specification

FieldTypeRequiredDescription
status"success" | "error"AlwaysIndicates if the request succeeded
messagestringAlwaysHuman-readable description
dataobject | array | nullAlwaysResponse payload. Use null when there's no data to return
metaobject | nullAlwaysPagination, filters, or additional metadata. Use null when not needed

Success Response Examples

Resource retrieved:

{
  "status": "success",
  "message": "Credit cards retrieved successfully.",
  "data": [
    {
      "id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A1",
      "alias": "Mercado Pago",
      "color": "#007bff",
      "total_debt": "837.83",
      "payment_due_day": 18,
      "statement_close_day": 7,
      "credit_limit": "2900.00",
      "metadata": {},
      "created_at": "2025-01-15T10:30:00Z",
      "updated_at": "2025-01-15T10:30:00Z"
    }
  ],
  "meta": null
}

Resource created:

{
  "status": "success",
  "message": "Credit card created successfully.",
  "data": {
    "id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A1",
    "alias": "New Card",
    "credit_limit": "5000.00",
    "metadata": {},
    "created_at": "2025-01-15T10:30:00Z",
    "updated_at": "2025-01-15T10:30:00Z"
  },
  "meta": null
}

Resource deleted (no content to return):

{
  "status": "success",
  "message": "Transaction deleted successfully.",
  "data": null,
  "meta": null
}

Paginated list:

{
  "status": "success",
  "message": "Products retrieved successfully.",
  "data": [{ "id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A2", "name": "Product 1" }],
  "meta": {
    "current_page": 1,
    "per_page": 20,
    "total_items": 150,
    "total_pages": 8
  }
}

Error Response Examples

Validation error:

{
  "status": "error",
  "message": "Validation failed.",
  "data": {
    "errors": {
      "email": ["The email field is required."],
      "password": ["Password must be at least 8 characters."]
    }
  },
  "meta": null
}

Authentication error:

{
  "status": "error",
  "message": "Unauthenticated.",
  "data": null,
  "meta": null
}

Authorization error:

{
  "status": "error",
  "message": "You do not have permission to perform this action.",
  "data": null,
  "meta": null
}

Resource not found:

{
  "status": "error",
  "message": "Credit card not found.",
  "data": null,
  "meta": null
}

Server error (no details exposed):

{
  "status": "error",
  "message": "Internal server error.",
  "data": null,
  "meta": null
}

2. Mandatory OpenAPI Documentation

Every API endpoint MUST be documented using OpenAPI 3.0+ specification.

Delivery Requirements

  • Each project must expose an OpenAPI specification endpoint (e.g., /api/docs/openapi.json)
  • Swagger UI or Scalar must be available at /api/docs
  • Every endpoint must be fully documented with:
    • Summary and description
    • All path, query, and header parameters
    • Request body schema (when applicable)
    • All possible response schemas (including errors)
    • Security requirements

OpenAPI Example

openapi: 3.0.3
info:
  title: Credit Cards API
  version: 1.0.0
  description: API for managing credit cards and transactions
servers:
  - url: /api
    description: Base API path

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

  schemas:
    ApiResponse:
      type: object
      required: [status, message, data, meta]
      properties:
        status:
          type: string
          enum: [success, error]
        message:
          type: string
        data:
          nullable: true
        meta:
          nullable: true
          type: object
          properties:
            current_page: { type: integer }
            per_page: { type: integer }
            total_items: { type: integer }
            total_pages: { type: integer }

    CreditCard:
      type: object
      properties:
        id:
          type: string
          format: ulid
          example: "01JQ5B3K7MNP9R8V2WX4Y6Z0A1"
        alias:
          type: string
          example: "Mercado Pago"
        color:
          type: string
          pattern: "^#[0-9a-fA-F]{6}$"
          example: "#007bff"
        total_debt:
          type: string
          format: decimal
          example: "837.83"
        payment_due_day:
          type: integer
          minimum: 1
          maximum: 31
        statement_close_day:
          type: integer
          minimum: 1
          maximum: 31
        credit_limit:
          type: string
          format: decimal
          example: "2900.00"
        metadata:
          type: object
          additionalProperties: true
        created_at:
          type: string
          format: date-time
        updated_at:
          type: string
          format: date-time

paths:
  /credit-cards:
    get:
      tags: [Credit Cards]
      summary: List user's credit cards
      security:
        - bearerAuth: []
      parameters:
        - name: page
          in: query
          schema: { type: integer, default: 1 }
        - name: per_page
          in: query
          schema: { type: integer, default: 20 }
      responses:
        "200":
          description: Credit cards retrieved successfully
          content:
            application/json:
              schema:
                allOf:
                  - $ref: "#/components/schemas/ApiResponse"
                  - type: object
                    properties:
                      data:
                        type: array
                        items:
                          $ref: "#/components/schemas/CreditCard"
        "401":
          $ref: "#/components/responses/Unauthenticated"

3. ULID for Resource Identifiers

All resource IDs exposed by the API MUST use ULID format instead of auto-increment integers or UUIDs.

ULID Format

  • 26 characters, Crockford's Base32 encoded
  • Example: 01JQ5B3K7MNP9R8V2WX4Y6Z0A1
  • Sortable by generation time
  • URL-safe (no special characters)

Use in API

{
  "id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A1",
  "user_id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A2"
}

All references between resources (foreign keys exposed in the API) must also use ULID strings.

4. JWT Authentication

All protected endpoints MUST authenticate via JWT sent in the Authorization header.

Request Header

Authorization: Bearer <token>

Token Lifetime

  • Access token: 15–60 minutes
  • Refresh token: 7–30 days
  • Algorithm: HS256 (single service) or RS256 (microservices)

Token Payload Structure

{
  "sub": "01JQ5B3K7MNP9R8V2WX4Y6Z0A1",
  "email": "user@example.com",
  "iat": 1687700000,
  "exp": 1687703600,
  "type": "access"
}

Login Response Format

{
  "status": "success",
  "message": "Authenticated successfully.",
  "data": {
    "access_token": "eyJhbGciOiJIUzI1NiIs...",
    "refresh_token": "eyJhbGciOiJIUzI1NiIs...",
    "token_type": "Bearer",
    "expires_in": 900
  },
  "meta": null
}

5. Mandatory Entity Fields

Every resource exposed by the API MUST include these three fields in its representation:

FieldTypeDescription
created_atstring (ISO 8601 UTC)Timestamp when the resource was created. Never changes.
updated_atstring (ISO 8601 UTC)Timestamp of the last modification.
metadataobject (free-form key/val)Optional extra information not covered by the main schema.

Format

  • Timestamps: ISO 8601 in UTC with Z suffix (e.g., 2025-01-15T10:30:00Z).
  • metadata: always an object. Defaults to {} when empty, never null.

Example

{
  "id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A1",
  "alias": "Mercado Pago",
  "credit_limit": "2900.00",
  "metadata": {
    "source": "mobile_app",
    "imported_from": "legacy_system",
    "custom_color": "#ff5733"
  },
  "created_at": "2025-01-15T10:30:00Z",
  "updated_at": "2025-01-16T14:20:15Z"
}

Purpose of metadata

The metadata field allows sending additional context without altering the resource schema or introducing new fields for every small variation. Clients should never rely on a specific metadata key being present unless explicitly documented for that endpoint.

6. Data Format Conventions

Consistent data shapes across every resource in the API:

Data TypeFormatExample
IdentifiersULID string"01JQ5B3K7MNP9R8V2WX4Y6Z0A1"
TimestampsISO 8601 UTC string"2025-01-15T10:30:00Z"
Dates (only)YYYY-MM-DD"2025-01-15"
DecimalsString (to preserve precision)"2900.00"
IntegersNumber18
Booleanstrue / falsetrue
Enumslowercase snake_case string"pending_review"
Colors (hex)#rrggbb"#007bff"
Field namingsnake_casepayment_due_day
CurrencyAmount as string + separate ISO 4217 currency field"100.00", "USD"
Empty lists[] (never null)[]
Empty objects{} (never null, e.g., for metadata){}
Unknown valuenull (used only when the value is genuinely absent)null

7. HTTP Status Codes

CodeWhen to Use
200Successful GET, PUT, PATCH
201Successful POST (resource created)
204Successful DELETE (no content to return, data: null)
400Malformed request
401Missing or invalid authentication
403Authenticated but insufficient permissions
404Requested resource not found
422Validation errors
429Rate limit exceeded
500Unexpected server error

8. Sensitive Data

The API MUST NEVER return:

  • Password hashes or raw passwords
  • Internal tokens, secret keys, or private credentials
  • Stack traces, raw exceptions, or internal error details
  • Internal database IDs (use ULIDs instead)

Error responses in production must use the generic error format defined in Section 1 — never leak internal information.

9. Logging System (Mandatory)

Every API MUST implement a structured logging system. Logs are essential for observability, debugging, auditing, and incident response in production.

Requirements

  • Use structured logging (JSON) rather than plain text, so logs are machine-parseable.
  • Assign a correlation/request ID (e.g., request_id, ideally a ULID) to every incoming request and include it in all log entries for that request. Return it in a response header (e.g., X-Request-Id) so clients and servers can be cross-referenced.
  • Log at minimum, for every request: HTTP method, path, status code, latency (ms), authenticated user id (when present), and the request_id.
  • Log the complete request (method, path, query params, headers, and body) and the complete response (status code, headers, and body) for each request — with sensitive fields masked/redacted (see security note below).
  • Log all errors and unhandled exceptions with full stack traces server-side only — never expose them in the API response (see Section 8). When something crashes, the log MUST indicate the exact code location (file, line, function/method, and the full stack trace) so the failure can be traced back to its source.
  • Use appropriate log levels: debug, info, warning, error, critical.

Standard Log Levels

LevelWhen to Use
debugDetailed diagnostic info, disabled in production by default
infoNormal events: request received, resource created, job completed
warningUnexpected but recoverable situations (deprecated usage, retries)
errorA request failed or an operation could not be completed
criticalSystem-level failures requiring immediate attention

Structured Log Entry Example

{
  "timestamp": "2025-01-15T10:30:00.123Z",
  "level": "info",
  "request_id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A1",
  "method": "POST",
  "path": "/api/credit-cards",
  "status_code": 201,
  "latency_ms": 42,
  "user_id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A2",
  "message": "Credit card created successfully."
}

Full Request/Response Log Example

{
  "timestamp": "2025-01-15T10:30:00.123Z",
  "level": "info",
  "request_id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A1",
  "request": {
    "method": "POST",
    "path": "/api/credit-cards",
    "query": {},
    "headers": { "content-type": "application/json", "authorization": "Bearer ***" },
    "body": { "alias": "New Card", "credit_limit": "5000.00" }
  },
  "response": {
    "status_code": 201,
    "headers": { "content-type": "application/json" },
    "body": { "status": "success", "message": "Credit card created successfully.", "data": { "id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A3" }, "meta": null }
  },
  "latency_ms": 42
}

Crash / Exception Log Example

When an unhandled error occurs, log where it crashed (file, line, function) plus the full stack trace — server-side only:

{
  "timestamp": "2025-01-15T10:31:12.880Z",
  "level": "error",
  "request_id": "01JQ5B3K7MNP9R8V2WX4Y6Z0A4",
  "method": "POST",
  "path": "/api/credit-cards",
  "status_code": 500,
  "error": {
    "type": "NullPointerException",
    "message": "credit_limit is null",
    "location": {
      "file": "src/services/credit_card_service.py",
      "line": 87,
      "function": "create_credit_card"
    },
    "stack_trace": "Traceback (most recent call last):\n  File \"src/services/credit_card_service.py\", line 87, in create_credit_card\n    ..."
  }
}

Security: Never Log Sensitive Data

Logs MUST NOT contain passwords, tokens, JWTs, secret keys, full card numbers, or any PII beyond what is strictly necessary. Mask or redact sensitive fields before logging (e.g., "authorization": "Bearer ***"). The same restrictions from Section 8 apply to logs.

Non-Negotiable Rules

  1. Response format: Every response MUST use { status, message, data, meta }
  2. OpenAPI: Every endpoint MUST be documented with OpenAPI 3.0+
  3. ULID: All resource IDs exposed by the API MUST be ULIDs
  4. JWT: Protected endpoints MUST use JWT via Authorization: Bearer <token>
  5. Entity fields: Every resource MUST include created_at, updated_at, and metadata
  6. Data formats: Follow the conventions table (snake_case, ISO 8601 UTC, decimals as strings, etc.)
  7. HTTP codes: Use the correct status code for every response
  8. Error format: Always follow the unified error structure — never expose internals
  9. Sensitive data: Never return passwords, secrets, or stack traces
  10. Documentation: OpenAPI spec must be accessible and kept in sync with the real API
  11. Logging: Every API MUST implement structured logging with a per-request correlation id, logging the full request and response and, on crashes, the exact code location and stack trace — never logging sensitive data

Quick Start Checklist

When designing a new API endpoint:

  1. Define the OpenAPI schema first (contract-first approach)
  2. Ensure the response uses the unified { status, message, data, meta } format
  3. Ensure every resource exposes id (ULID), created_at, updated_at, and metadata
  4. Apply the correct HTTP status code
  5. Document all success and error responses in OpenAPI
  6. Protect the endpoint with JWT when required
  7. Ensure requests and errors are logged with a correlation id (structured logging)

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.