|
| 1 | +# Contractual |
| 2 | +**Contractual is an open-source, developer-first ecosystem for managing API contracts, versioning, and testing.** By unifying the entire API lifecycle under one tool, |
| 3 | +it ensures type safety, consistency, and automation across all projects. |
| 4 | + |
| 5 | +## 🚀 Core Features |
| 6 | + |
| 7 | +Managing APIs is hard. Definitions are scattered, breaking changes sneak into production, and shared types get duplicated across services and projects. Contractual solves these challenges with a unified workflow for defining, versioning, and testing APIs: |
| 8 | + |
| 9 | +- **Unified API Workflows**: Define your API once and generate specs, server contracts, and clients. |
| 10 | +- **Client Libraries**: Share ready-to-use, type-safe API clients with your consumers. |
| 11 | +- **Server Contracts**: Enforce endpoint implementation with contracts for popular frameworks. |
| 12 | +- **E2E Testing**: Generate strictly-typed Playwright tests for end-to-end validation. |
| 13 | +- **Version Control**: Automatically detect and manage API changes with snapshots. |
| 14 | + |
| 15 | +## 📋 Getting Started |
| 16 | + |
| 17 | +### Initialize a Project |
| 18 | +Set up a new project with boilerplate TypeSpec and configurations: |
| 19 | + |
| 20 | +```bash |
| 21 | +npx @contractual/cli init |
| 22 | +``` |
| 23 | + |
| 24 | +This scaffolds the following structure: |
| 25 | +``` |
| 26 | +my-project/ |
| 27 | +├── contractual |
| 28 | +├──── api.tsp # The TypeSpec DSL for defining your API |
| 29 | +├──── snapshots # Stores OpenAPI specs for each version |
| 30 | +├── client # Your client / frontend |
| 31 | +├── server # Your server implementation |
| 32 | +└── e2e # Your API e2e tests |
| 33 | +``` |
| 34 | +> You can decide where to place generated artifacts. Contractual works with any project structure, including monorepos, monoliths, and distributed repos. |
| 35 | +
|
| 36 | +### Define Your API |
| 37 | +Use TypeSpec to define your API. Example: |
| 38 | + |
| 39 | +```typescript |
| 40 | +@route("/users") |
| 41 | +model User { |
| 42 | + id: string; |
| 43 | + name: string; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Generate Contracts |
| 48 | +Generate OpenAPI specs, server stubs, clients, and e2e artifacts with one command: |
| 49 | + |
| 50 | +```bash |
| 51 | +contractual generate --framework=express |
| 52 | +``` |
| 53 | + |
| 54 | +This generates: |
| 55 | +- **OpenAPI specs** (e.g., `openapi-v1.0.0.yaml`). |
| 56 | +- **Server contracts** for Express, Fastify, or NestJS. |
| 57 | +- **Client libraries** with Zod schemas and React Query integration. |
| 58 | +- **E2E testing fixtures** for Playwright. |
| 59 | + |
| 60 | +### Snapshot and Validate |
| 61 | +Save the current OpenAPI spec for version tracking: |
| 62 | + |
| 63 | +```bash |
| 64 | +contractual snapshot |
| 65 | +``` |
| 66 | + |
| 67 | +Validate API changes for backward compatibility: |
| 68 | + |
| 69 | +```bash |
| 70 | +contractual validate |
| 71 | +``` |
| 72 | + |
| 73 | +## 🖥️ CLI Commands |
| 74 | + |
| 75 | +| Command | Description | Example | |
| 76 | +|------------------------|-----------------------------------------------------------------------------|--------------------------------------| |
| 77 | +| `contractual init` | Initialize a new project with TypeSpec and boilerplate setup. | `contractual init` | |
| 78 | +| `contractual generate` | Generate OpenAPI specs, clients, and server stubs from TypeSpec contracts. | `contractual generate --framework=express` | |
| 79 | +| `contractual snapshot` | Save the current OpenAPI spec version for tracking and validation. | `contractual snapshot` | |
| 80 | +| `contractual validate` | Validate backward compatibility of API changes. | `contractual validate` | |
| 81 | + |
| 82 | +## 🎯 Use Cases |
| 83 | + |
| 84 | +### Microservice Development |
| 85 | +- Define reusable contracts for APIs and ensure consistency across services. |
| 86 | +- Generate server stubs for **Express**, **Fastify**, or **NestJS**. |
| 87 | + |
| 88 | +### Frontend-Backend Alignment |
| 89 | +- Generate type-safe API clients with **Zod** validation. |
| 90 | +- Simplify frontend integration with **React Query hooks**. |
| 91 | + |
| 92 | +### Versioning and Evolution |
| 93 | +- Automate version tracking with breaking change detection. |
| 94 | +- Enforce backward compatibility with diff-based validation. |
| 95 | + |
| 96 | +### End-to-End Testing |
| 97 | +- Use Playwright-ready fixtures generated directly from contracts. |
| 98 | +- Ensure e2e tests reflect the latest API state. |
| 99 | + |
| 100 | +## 🌐 Built on Strong Foundations |
| 101 | + |
| 102 | +### **TypeSpec** |
| 103 | +> **"A language for defining cloud service APIs and shapes."** |
| 104 | +> TypeSpec powers Contractual's API definition capabilities, serving as the single source of truth for your contracts. [Learn More](https://microsoft.github.io/typespec/) |
| 105 | +
|
| 106 | +### **ts-rest** |
| 107 | +> **"A simple way to define a contract for your API, giving you end-to-end type safety without the hassle of code generation."** |
| 108 | +> Contractual leverages ts-rest for generating type-safe client and server contracts, integrating seamlessly into React Query, Express, Fastify, and NestJS. [Learn More](https://ts-rest.com) |
| 109 | +
|
| 110 | +## 🛠 How It Works |
| 111 | + |
| 112 | +### 1. TypeSpec: The DSL for API Definitions |
| 113 | +At the core of Contractual is **TypeSpec**, a language designed for defining API shapes and services. With its extensibility and reusable patterns, TypeSpec serves as the single source of truth for your API. |
| 114 | + |
| 115 | +> **TypeSpec Highlights**: |
| 116 | +> - Describe APIs for REST, gRPC, and other protocols. |
| 117 | +> - Generate OpenAPI specs, client/server code, and more. |
| 118 | +> - Enforce best practices with built-in linting and guardrails. |
| 119 | +
|
| 120 | +### 2. ts-rest: Type-Safe Contract Generation |
| 121 | +Contractual integrates **ts-rest** to bring type-safe contracts to your applications. ts-rest simplifies API interactions with: |
| 122 | +- **End-to-End Type Safety**: Consistent types across server and client. |
| 123 | +- **Zod Validation**: Runtime validation of API inputs and outputs. |
| 124 | +- **React Query Integration**: Auto-generated hooks for frontend development. |
| 125 | + |
| 126 | +## 📚 Documentation |
| 127 | + |
| 128 | +Explore the full documentation, including guides, examples, and advanced workflows: |
| 129 | + |
| 130 | +📖 [Read the Documentation](https://contractual.dev/) |
| 131 | + |
| 132 | +## ❤️ Contribute |
| 133 | +Help shape the future of Contractual! Check out [our roadmap](https://github.com/contractual/contractual) and contribute today. |
| 134 | + |
| 135 | +## 🔒 License |
| 136 | +Licensed under [Apache 2.0](LICENSE). |
0 commit comments