Overview
@contract-kit/core is the foundation of Contract Kit. It provides the tools to define type-safe HTTP contracts that can be used across your entire stack.
Prefer the
contract-kit meta package for new projects. It re-exports
@contract-kit/core along with client, ports, application, and more.Installation
Key Exports
createContractGroup
Creates a group of related contracts that share configuration:Contract Builders
Build contracts using a fluent API:API Reference
Contract Group Methods
.namespace(name: string)
Set a namespace for OpenAPI tags:
.meta(metadata: Record<string, unknown>)
Attach shared metadata for all contracts in the group:
.errors(schemas: Record<number, Schema>)
Define shared error responses:
HTTP Method Builders
.get(path: string)
Define a GET endpoint:
.post(path: string)
Define a POST endpoint:
.put(path: string)
Define a PUT endpoint:
.patch(path: string)
Define a PATCH endpoint:
.delete(path: string)
Define a DELETE endpoint:
Schema Methods
.path(schema: Schema)
Define path parameters:
.query(schema: Schema)
Define query parameters:
.body(schema: Schema)
Define request body:
.response(status: number, schema: Schema)
Define response for a status code:
.errors(schemas: Record<number, Schema>)
Define error response:
Metadata Methods
.meta(metadata: Record<string, unknown>)
Attach custom metadata (auth, rate limit, idempotency, etc.):
Examples
Simple GET Endpoint
POST with Validation
List with Pagination
Protected Endpoint
TypeScript Support
The core package provides full TypeScript inference:Best Practices
Group related endpoints
Group related endpoints
Define comprehensive schemas
Define comprehensive schemas
Include all validation rules in your schemas for automatic validation.
Use descriptive paths
Use descriptive paths
Make paths self-documenting with clear resource names and RESTful conventions.
Document with comments
Document with comments
Add JSDoc comments to contracts for better IDE support and OpenAPI generation.