All skills
asyrafhussin avatar

/api-design-patterns

@1de3a7a

RESTful API design, error handling, versioning, and best practices. Use when designing APIs, reviewing endpoints, implementing error responses, or setting up API structure. Triggers on "design API", "review API", "REST best practices", or "API patterns".

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/api-design-patterns

This session only. Nothing lands on disk.

rulessort-flexible.md

≈629 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Flexible Sorting Options

Impact: HIGH (Empowers clients to retrieve data in the exact order they need without server-side changes)

APIs that return data in a single hardcoded order force clients to re-sort in memory, which is wasteful and breaks pagination. A flexible sorting API lets clients request the order they need, and the database handles it efficiently using indexes.

Incorrect

# No sort parameter — always returns by id ASC
GET /api/v1/products
[
  { "id": 1, "name": "Alpha", "price": 29.99, "created_at": "2023-01-01T00:00:00Z" },
  { "id": 2, "name": "Beta", "price": 9.99, "created_at": "2024-06-15T00:00:00Z" }
]
# Or: inconsistent sort parameters across endpoints
GET /api/v1/products?order_by=price&direction=desc
GET /api/v1/users?sortField=name&sortOrder=asc

Problems:

  • Client must fetch all data and sort in memory, defeating the purpose of pagination
  • No way to get "newest first" or "cheapest first" without client-side processing
  • Inconsistent sort parameter names across endpoints increase integration complexity
  • Hardcoded order may not match any client's primary use case

Correct

# Ascending sort (default direction)
GET /api/v1/products?sort=price

# Descending sort with "-" prefix
GET /api/v1/products?sort=-created_at

# Multiple sort fields (comma-separated)
GET /api/v1/products?sort=category,-price

# Combined with filtering and pagination
GET /api/v1/products?status=active&sort=-created_at&page=1&per_page=20
{
  "data": [
    { "id": 47, "name": "New Widget", "price": 59.99, "created_at": "2024-06-15T00:00:00Z" },
    { "id": 32, "name": "Another Widget", "price": 39.99, "created_at": "2024-05-10T00:00:00Z" }
  ],
  "meta": {
    "current_page": 1,
    "per_page": 20,
    "total_count": 84,
    "sort": "-created_at"
  }
}

Benefits:

  • - prefix convention for descending is compact and widely adopted (JSON:API, many major APIs)
  • Multiple sort fields let clients express complex ordering like "category ascending, then price descending"
  • Sorting happens at the database level where indexes make it efficient
  • Consistent sort parameter name across all endpoints simplifies client SDKs

Reference: JSON:API - Sorting

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill is a comprehensive documentation library for RESTful API design patterns. It provides best practices for resource design, error handling, security, and documentation. The skill contains no executable code, malicious instructions, or hidden functionality, and its references are restricted to well-known technical documentation sources.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    4/28 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 1de3a7a. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
metadata
{
  "author": "agent-skills",
  "version": "2.0.0"
}

README badge

README badge for asyrafhussin/agent-skills/api-design-patterns