Provides API design guidance for REST, GraphQL, and gRPC services covering naming conventions, resource modeling, versioning strategies, error handling (RFC 9457), pagination patterns, OpenAPI 3.1 specification authoring, security (OAuth 2.1, rate limiting), and agent-friendly design patterns including MCP tool integration. Use when designing new APIs, reviewing API contracts, choosing between REST/GraphQL/gRPC, implementing error handling or pagination, writing OpenAPI specs, or making APIs consumable by AI agents. Triggers: API design, REST, GraphQL, gRPC, OpenAPI, error handling, pagination, versioning, API security, agent-friendly API, MCP tools, API-first.
التثبيت
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
Provides API design guidance for REST, GraphQL, and gRPC services covering naming conventions, resource modeling, versioning strategies, error handling (RFC 9457), pagination patterns, OpenAPI 3.1 specification authoring, security (OAuth 2.1, rate limiting), and agent-friendly design patterns including MCP tool integration. Use when designing new APIs, reviewing API contracts, choosing between REST/GraphQL/gRPC, implementing error handling or pagination, writing OpenAPI specs, or making APIs consumable by AI agents. Triggers: API design, REST, GraphQL, gRPC, OpenAPI, error handling, pagination, versioning, API security, agent-friendly API, MCP tools, API-first.
Protobuf: PascalCase messages/services, snake_case fields, ENUM_PREFIX_VALUE enums, Service suffix.
4. Error Handling
Use RFC 9457 Problem Details for all REST error responses:
{"type":"https://api.example.com/errors/validation-failed","title":"Validation Failed","status":422,"detail":"The email field must be a valid email address.","errors":[{"field":"email","code":"INVALID_FORMAT","message":"Must be a valid email"}]}
Rules:
Content-Type: application/problem+json
Define an error code registry and publish it
Include field-level validation errors for 400/422 responses
Include retry_after_seconds for 429 and transient 5xx errors
Use offset-based only when random page access is required
7. Security
Authentication: OAuth 2.1 with Authorization Code + PKCE for user context. Client Credentials for service-to-service. API keys for application identification only (not user auth).
Authorization: Scope-based (read:users, write:users) checked at middleware layer. RBAC or ABAC for fine-grained control.
Rate limiting: Per-consumer (API key or token), not global. Return X-RateLimit-* headers on every response. Return 429 with Retry-After header when exceeded.
Transport: TLS 1.2+ required. HSTS header. CORS configured per-origin (never wildcard for authenticated APIs).
8. Agent-Friendly Design
Design every API as if an AI agent is a first-class consumer:
OpenAPI spec at stable URL -- Serve at /openapi.json. Agents auto-generate tools from it.
operationId as tool name -- Use clear verb-noun IDs: listUsers, createOrder. These become function names.
Examples in every schema -- Agents use examples to construct valid requests.
HATEOAS links -- Include links and allowed_actions so agents can navigate without hardcoding URLs.
Idempotency keys -- Accept Idempotency-Key header on POST/PATCH for safe agent retries.
Prefixed IDs -- Use usr_123, ord_456 so agents can identify resource types from IDs alone.
Consistent response shapes -- Same envelope for success, error, and collection responses.
Document workflows -- Show multi-step API call chains in docs; agents use these to plan.
9. MCP Tool Exposure
When wrapping APIs as MCP tools for AI agents:
One tool per distinct action (not one mega-tool)
Simple parameter types (str, int, bool) over complex objects
Descriptions under 50 tokens each
Include valid values and defaults in the description
Return structured Markdown, not raw JSON
Quick Reference
REST Endpoint Template
GET /api/v1/{resource} -> List (200, paginated)
POST /api/v1/{resource} -> Create (201 + Location header)
GET /api/v1/{resource}/{id} -> Read (200)
PUT /api/v1/{resource}/{id} -> Replace (200)
PATCH /api/v1/{resource}/{id} -> Partial update (200)
DELETE /api/v1/{resource}/{id} -> Remove (204)
POST /api/v1/{resource}/{id}/{action} -> Custom action (200)
200 OK - GET/PUT/PATCH success
201 Created - POST success (+ Location)
202 Accepted - Async operation queued
204 No Content - DELETE success
400 Bad Request - Malformed input
401 Unauthorized - Missing/invalid auth
403 Forbidden - Insufficient permissions
404 Not Found - Resource doesn't exist
409 Conflict - State conflict
422 Unprocessable - Validation failure
429 Too Many - Rate limited (+ Retry-After)
500 Internal Error - Server failure
503 Unavailable - Temporary outage (+ Retry-After)
Checklist
Before shipping an API, verify:
API contract (OpenAPI/GraphQL SDL/Protobuf) written and reviewed before implementation
All endpoints have operationId, summary, description, and examples
Naming conventions are consistent (plural nouns, kebab-case URLs, appropriate casing per paradigm)
Error responses follow RFC 9457 with stable error codes and field-level validation details
Pagination implemented with maximum page size enforced and navigation metadata included
Versioning strategy chosen and documented; deprecation policy defined
Authentication and authorization implemented (OAuth 2.1 / API keys / scopes)
Rate limiting active with X-RateLimit-* headers and 429 + Retry-After responses
TLS 1.2+ enforced; CORS configured per-origin
Idempotency keys supported on POST/PATCH endpoints
OpenAPI spec published at a stable URL and passes Spectral linting
Response shapes are consistent across all endpoints (same envelope for data, errors, collections)
Agent-friendly: prefixed IDs, HATEOAS links, documented workflows, examples in every schema
Input validation: max lengths, type checking, required fields, sanitization
When to Escalate
Choosing between paradigms -- If the system has both public consumers and internal microservices, consider a multi-paradigm approach (gRPC internal + REST external). Consult with the architecture team.
Breaking change to a public API -- Requires a versioning plan, migration guide, and stakeholder sign-off. Never break a public API without a deprecation period.
Complex authorization requirements -- ABAC policies, multi-tenant isolation, or cross-service permission propagation may need a dedicated authorization service (e.g., OPA, Cedar, Zanzibar-based).
Real-time requirements -- WebSocket, SSE, or gRPC streaming architectures need infrastructure review for connection management, scaling, and failover.
Regulatory compliance -- APIs handling PII, financial data, or health data may require additional security review, audit logging, and data residency considerations.
Agent-specific API design -- If agents are the primary consumer, consider whether MCP tool exposure, function-calling schemas, or a dedicated agent API layer is more appropriate than a general-purpose API.