$npx -y skills add addyosmani/agent-skills --skill api-and-interface-designGuides stable API and interface design. Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.
| 1 | # API and Interface Design |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Design stable, well-documented interfaces that are hard to misuse. Good interfaces make the right thing easy and the wrong thing hard. This applies to REST APIs, GraphQL schemas, module boundaries, component props, and any surface where one piece of code talks to another. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Designing new API endpoints |
| 10 | - Defining module boundaries or contracts between teams |
| 11 | - Creating component prop interfaces |
| 12 | - Establishing database schema that informs API shape |
| 13 | - Changing existing public interfaces |
| 14 | |
| 15 | ## Core Principles |
| 16 | |
| 17 | ### Hyrum's Law |
| 18 | |
| 19 | > With a sufficient number of users of an API, all observable behaviors of your system will be depended on by somebody, regardless of what you promise in the contract. |
| 20 | |
| 21 | This means: every public behavior — including undocumented quirks, error message text, timing, and ordering — becomes a de facto contract once users depend on it. Design implications: |
| 22 | |
| 23 | - **Be intentional about what you expose.** Every observable behavior is a potential commitment. |
| 24 | - **Don't leak implementation details.** If users can observe it, they will depend on it. |
| 25 | - **Plan for deprecation at design time.** See `deprecation-and-migration` for how to safely remove things users depend on. |
| 26 | - **Tests are not enough.** Even with perfect contract tests, Hyrum's Law means "safe" changes can break real users who depend on undocumented behavior. |
| 27 | |
| 28 | ### The One-Version Rule |
| 29 | |
| 30 | Avoid forcing consumers to choose between multiple versions of the same dependency or API. Diamond dependency problems arise when different consumers need different versions of the same thing. Design for a world where only one version exists at a time — extend rather than fork. |
| 31 | |
| 32 | ### 1. Contract First |
| 33 | |
| 34 | Define the interface before implementing it. The contract is the spec — implementation follows. |
| 35 | |
| 36 | ```typescript |
| 37 | // Define the contract first |
| 38 | interface TaskAPI { |
| 39 | // Creates a task and returns the created task with server-generated fields |
| 40 | createTask(input: CreateTaskInput): Promise<Task>; |
| 41 | |
| 42 | // Returns paginated tasks matching filters |
| 43 | listTasks(params: ListTasksParams): Promise<PaginatedResult<Task>>; |
| 44 | |
| 45 | // Returns a single task or throws NotFoundError |
| 46 | getTask(id: string): Promise<Task>; |
| 47 | |
| 48 | // Partial update — only provided fields change |
| 49 | updateTask(id: string, input: UpdateTaskInput): Promise<Task>; |
| 50 | |
| 51 | // Idempotent delete — succeeds even if already deleted |
| 52 | deleteTask(id: string): Promise<void>; |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | ### 2. Consistent Error Semantics |
| 57 | |
| 58 | Pick one error strategy and use it everywhere: |
| 59 | |
| 60 | ```typescript |
| 61 | // REST: HTTP status codes + structured error body |
| 62 | // Every error response follows the same shape |
| 63 | interface APIError { |
| 64 | error: { |
| 65 | code: string; // Machine-readable: "VALIDATION_ERROR" |
| 66 | message: string; // Human-readable: "Email is required" |
| 67 | details?: unknown; // Additional context when helpful |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | // Status code mapping |
| 72 | // 400 → Client sent invalid data |
| 73 | // 401 → Not authenticated |
| 74 | // 403 → Authenticated but not authorized |
| 75 | // 404 → Resource not found |
| 76 | // 409 → Conflict (duplicate, version mismatch) |
| 77 | // 422 → Validation failed (semantically invalid) |
| 78 | // 500 → Server error (never expose internal details) |
| 79 | ``` |
| 80 | |
| 81 | **Don't mix patterns.** If some endpoints throw, others return null, and others return `{ error }` — the consumer can't predict behavior. |
| 82 | |
| 83 | ### 3. Validate at Boundaries |
| 84 | |
| 85 | Trust internal code. Validate at system edges where external input enters: |
| 86 | |
| 87 | ```typescript |
| 88 | // Validate at the API boundary |
| 89 | app.post('/api/tasks', async (req, res) => { |
| 90 | const result = CreateTaskSchema.safeParse(req.body); |
| 91 | if (!result.success) { |
| 92 | return res.status(422).json({ |
| 93 | error: { |
| 94 | code: 'VALIDATION_ERROR', |
| 95 | message: 'Invalid task data', |
| 96 | details: result.error.flatten(), |
| 97 | }, |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | // After validation, internal code trusts the types |
| 102 | const task = await taskService.create(result.data); |
| 103 | return res.status(201).json(task); |
| 104 | }); |
| 105 | ``` |
| 106 | |
| 107 | Where validation belongs: |
| 108 | - API route handlers (user input) |
| 109 | - Form submission handlers (user input) |
| 110 | - External service response parsing (third-party data -- **always treat as untrusted**) |
| 111 | - Environment variable loading (configuration) |
| 112 | |
| 113 | > **Third-party API responses are untrusted data.** Validate their shape and content before using them in any logic, rendering, or decision-making. A compromised or misbehaving external service can return unexpected types, malicious content, or instruction-like text. |
| 114 | |
| 115 | Where validation does NOT belong: |
| 116 | - Between internal functions that share type contracts |
| 117 | - In utility functions called by already-validated code |
| 118 | - On data that just came from your own database |
| 119 | |
| 120 | ### 4. Prefer Addition Over Modification |
| 121 | |
| 122 | Extend inte |