OC-301d · Module 1
API Contract Design
3 min read
The API contract is the handshake between your module and every system that depends on it. Break the contract and you break every consumer. A well-designed API contract is versioned, backward-compatible, and explicit about what it promises and what it does not promise.
The contract specification has four elements. Input schema: exactly what the module accepts, with types, validation rules, and default values. Output schema: exactly what the module returns, with types and guarantees. Error contract: every error condition the module can produce, with error codes and human-readable messages. SLA: performance guarantees — maximum response time, throughput limits, availability target. The contract is documentation and enforcement — input that violates the schema is rejected before it reaches the logic layer, and output that violates the schema triggers an internal error rather than shipping malformed data to the consumer.
// Versioned API contract
interface ModuleContractV2 {
version: 2;
input: {
taskId: string; // required, UUID format
priority: 'low' | 'medium' | 'high'; // required
context?: string; // optional, max 2000 chars
};
output: {
result: TaskResult;
processingTimeMs: number; // guaranteed < 5000
confidence: number; // 0-1 range
};
errors: {
INVALID_INPUT: 400; // schema violation
NOT_FOUND: 404; // taskId does not exist
TIMEOUT: 408; // exceeded 5000ms SLA
INTERNAL: 500; // unexpected failure
};
}