What are Contracts?
A contract is the single source of truth for an API endpoint. It describes everything about an HTTP endpoint in a type-safe way:- HTTP method and path (with path parameters)
- Path parameters, query parameters, and request body schemas
- Response schemas (per status code)
- Error schemas (per status code)
- Metadata for auth, rate limiting, idempotency, etc.
Why Contracts?
Contracts provide several key benefits:- Type Safety: Full TypeScript inference from contract to server to client
- Documentation: Self-documenting code that can generate OpenAPI specs
- Validation: Automatic request/response validation at runtime
- Consistency: Single definition ensures server and client stay in sync
- No Codegen: Type inference happens at compile time, no build steps needed
Creating a Contract Group
Contract groups let you share configuration across related endpoints:Defining Contracts
GET Requests
POST Requests
Query Parameters
Path Parameters
Multiple Response Status Codes
Error Schemas
Define expected error responses:Contract Metadata
Contracts can include metadata for various purposes:Authentication
Rate Limiting
Idempotency
Schema Libraries
Contract Kit works with any Standard Schema library:- Zod
- Valibot
- ArkType
Best Practices
Keep contracts focused
Keep contracts focused
Each contract should represent a single endpoint. Don’t try to handle multiple operations in one contract.
Use contract groups for related endpoints
Use contract groups for related endpoints
Define comprehensive schemas
Define comprehensive schemas
Include validation rules in your schemas (min/max length, regex patterns, etc.) to catch errors early.
Document with descriptions
Document with descriptions
Use your schema library’s description features to add documentation that will appear in OpenAPI specs.
Version your APIs
Version your APIs
Include version numbers in paths (e.g.,
/api/v1/todos) for long-term API stability.