Guides 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.
Download SKILL.md or inspect the source before installing.
Step 1
Copy the install command
Copy the command or download SKILL.md, then add it to your AI coding environment.
Step 2
Check source and behavior
Open the source repo and confirm the skill behavior, scope, and fit for the task.
Step 3
Overview
# API and Interface Design
Overview
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.
When to Use
Designing new API endpoints
Defining module boundaries or contracts between teams
Creating component prop interfaces
Establishing database schema that informs API shape
Changing existing public interfaces
Core Principles
Hyrum's Law
> 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.
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:
**Be intentional about what you expose.** Every observable behavior is a potential commitment.
**Don't leak implementation details.** If users can observe it, they will depend on it.
**Plan for deprecation at design time.** See `deprecation-and-migration` for how to safely remove things users depend on.
**Tests are not enough.** Even with perfect contract tests, Hyrum's Law means "safe" changes can break real users who depend on undocumented behavior.
Validate with a real task
Run one small real task before keeping it in your long-term workflow.
The One-Version Rule
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.
1. Contract First
Define the interface before implementing it. The contract is the spec — implementation follows.
```typescript
// Define the contract first
interface TaskAPI {
// Creates a task and returns the created task with server-generated fields
External service response parsing (third-party data -- **always treat as untrusted**)
Environment variable loading (configuration)
> **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.
Where validation does NOT belong:
Between internal functions that share type contracts
In utility functions called by already-validated code
On data that just came from your own database
4. Prefer Addition Over Modification
Extend interfaces without breaking existing consumers:
| { type: 'cancelled'; reason: string; cancelledAt: Date };
// Consumer gets type narrowing
function getStatusLabel(status: TaskStatus): string {
switch (status.type) {
case 'pending': return 'Pending';
case 'in_progress': return `In progress (${status.assignee})`;
case 'completed': return `Done on ${status.completedAt}`;
case 'cancelled': return `Cancelled: ${status.reason}`;
}
}
```
Input/Output Separation
```typescript
// Input: what the caller provides
interface CreateTaskInput {
title: string;
description?: string;
}
// Output: what the system returns (includes server-generated fields)
interface Task {
id: string;
title: string;
description: string | null;
createdAt: Date;
updatedAt: Date;
createdBy: string;
}
```
Use Branded Types for IDs
```typescript
type TaskId = string & { readonly __brand: 'TaskId' };
type UserId = string & { readonly __brand: 'UserId' };
// Prevents accidentally passing a UserId where a TaskId is expected
function getTask(id: TaskId): Promise<Task> { ... }
```
Common Rationalizations
| Rationalization | Reality |
|---|---|
| "We'll document the API later" | The types ARE the documentation. Define them first. |
| "We don't need pagination for now" | You will the moment someone has 100+ items. Add it from the start. |
| "PATCH is complicated, let's just use PUT" | PUT requires the full object every time. PATCH is what clients actually want. |
| "We'll version the API when we need to" | Breaking changes without versioning break consumers. Design for extension from the start. |
| "Nobody uses that undocumented behavior" | Hyrum's Law: if it's observable, somebody depends on it. Treat every public behavior as a commitment. |
| "We can just maintain two versions" | Multiple versions multiply maintenance cost and create diamond dependency problems. Prefer the One-Version Rule. |
| "Internal APIs don't need contracts" | Internal consumers are still consumers. Contracts prevent coupling and enable parallel work. |
Red Flags
Endpoints that return different shapes depending on conditions
Inconsistent error formats across endpoints
Validation scattered throughout internal code instead of at boundaries
Breaking changes to existing fields (type changes, removals)
List endpoints without pagination
Verbs in REST URLs (`/api/createTask`, `/api/getUsers`)
Third-party API responses used without validation or sanitization
Verification
After designing an API:
[ ] Every endpoint has typed input and output schemas
[ ] Error responses follow a single consistent format
[ ] Validation happens at system boundaries only
[ ] List endpoints support pagination
[ ] New fields are additive and optional (backward compatible)
[ ] Naming follows consistent conventions across all endpoints
[ ] API documentation or types are committed alongside the implementation