Skip to content

HK-hub/AgentSkills / api-design

api-design

Verified

Professional API design skill covering RESTful APIs, GraphQL, API versioning, authentication, idempotency, and API documentation best practices. Use this skill when designing RESTful APIs, creating GraphQL schemas, implementing API versioning strategies, or need guidance on authentication, error handling, and API documentation.

references/pagination-rate-limiting.md

View on GitHub
# Pagination & Rate Limiting Patterns

## Pagination Patterns

### Offset-Based Pagination (Simple)

```
GET /api/v1/posts?page=2&size=20

Pros: Simple, works with SQL LIMIT/OFFSET
Cons: Performance degrades with large offsets, inconsistent with concurrent writes

SQL: SELECT * FROM posts ORDER BY id LIMIT 20 OFFSET 20;
```

### Cursor-Based Pagination (Recommended for real-time feeds)

```
GET /api/v1/posts?cursor=post_123&limit=20

Pros: Consistent, efficient, works with real-time data
Cons: Cannot jump to arbitrary page

Response:
{
  "data": [...],
  "cursor": {
    "next": "post_143",
    "previous": "post_103"
  },
  "has_more": true
}

SQL: SELECT * FROM posts WHERE id > 'post_123' ORDER BY id LIMIT 20;
```

### Keyset Pagination (Best performance)

```
GET /api/v1/posts?after_id=123&after_created_at=2025-01-01T00:00:00Z&limit=20

Pros: Excellent performance, consistent
Cons: Requires indexed columns, more complex

SQL:
SELECT * FROM posts
WHERE (created_at, id) > ('2025-01-01', 123)
ORDER BY created_at, id
LIMIT 20;
```

---

## Rate Limiting Strategies

### Token Bucket Algorithm

```
Bucket capacity: 100 tokens
Refill rate: 10 tokens/second
Request cost: 1 token per request

Headers:
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 45
X-RateLimit-Reset: 1735689600

When exceeded:
HTTP 429 Too Many Requests
Retry-After: 60
```

### Implementation

```python
from redis import Redis
import time

redis = Redis()

def check_rate_limit(user_id, limit=100, window=60):
    key = f"rate_limit:{user_id}"
    current = time.time()

    # Remove old entries
    redis.zremrangebyscore(key, 0, current - window)

    # Count current requests
    count = redis.zcard(key)

    if count >= limit:
        return False, 0

    # Add current request
    redis.zadd(key, {str(current): current})
    redis.expire(key, window)

    remaining = limit - count - 1
    return True, remaining
```
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall