Overview
This guide walks you through setting up Contract Kit in a Next.js application from scratch. You’ll learn how to structure your project, configure the server, set up routing patterns, and integrate with the Next.js App Router.Prerequisites
- Node.js 18+ or Bun
- Basic knowledge of TypeScript and Next.js
- Familiarity with the App Router (recommended)
Installation
Create a new Next.js project if you haven’t already:Project Structure
A typical Contract Kit + Next.js project follows this structure:Step 1: Define Your Contracts
Contracts are the source of truth for your API. They define endpoints, validation rules, and response types. Create your first contract:Step 2: Define Ports (Dependency Injection)
Ports define the interfaces for your application’s dependencies. They enable dependency injection and make your code testable.Step 3: Implement Providers
Providers are concrete implementations of your ports. Start with an in-memory provider for development:For production, replace this with a real database provider like @contract-kit/provider-drizzle-turso.
Step 4: Create the Server
Configure your Contract Kit server with context, error handling, and providers:Step 5: Set Up API Routes
Contract Kit supports two routing patterns. Choose the one that fits your needs.Option A: Catch-All Route (Recommended)
This pattern uses a single catch-all route to handle all contracts registered inlib/server.ts.
- Minimal boilerplate
- Centralized route configuration in
lib/server.ts - Easy to add new endpoints
Option B: Per-Route Files
This pattern creates individual route files for each endpoint. Routes are NOT registered inlib/server.ts.
- Clear file structure matching URL paths
- Easier to locate specific endpoints
- Good for code splitting
Step 6: Create a Typed Client
Generate a fully typed client for your frontend:Step 7: Use the Client in Your Frontend
Use your typed client in React components:Environment Variables
Create a.env.local file for local development:
Testing Your API
Run your development server:Advanced Configuration
Adding Providers
Replace the in-memory provider with real services:Adding Middleware
Middleware runs before your route handlers:Using Use Cases
Separate business logic from HTTP handlers:Server Component Integration
You can call use cases directly from React Server Components usingcreateContextFromNext():
- Eliminates unnecessary API routes for server-side data fetching
- Maintains type safety and business logic separation
- Automatically handles headers and cookies from Next.js
- Reuses your existing context creation logic
OpenAPI Documentation
Generate OpenAPI documentation for your API:/api/openapi to see your generated OpenAPI spec. See the OpenAPI Generation guide for more details.
Deployment
Contract Kit works seamlessly with Next.js deployment platforms:Vercel
- Push your code to GitHub
- Import your project in Vercel
- Add environment variables
- Deploy
Docker
Troubleshooting
TypeScript Errors
Ensure you’re using TypeScript 5.0 or higher:“Module not found” errors
Make sure yourtsconfig.json includes path aliases:
Runtime errors with providers
Ensure providers are properly initialized. Check your provider setup without exposing sensitive credentials:Next Steps
Error Handling
Implement robust error handling patterns
Middleware
Add authentication, logging, and rate limiting
React Integration
Use React Query and React Hook Form
Providers
Integrate databases, caching, and more