| name | api-contracts |
| description | API contract validation and Zod-first design patterns. Use when designing, implementing, or validating API routes and schemas. |
API Contract Patterns
All API routes MUST follow Zod-first contract design. Request bodies are validated before processing, and responses follow a consistent envelope pattern.
Instructions
- Before creating any API route, define the Zod schema first
- Run the validation script to verify routes match their schemas:
python scripts/validate_api.py <project_root>
- Follow the response envelope pattern shown below
Core Principles
- Zod-First: Define the schema, then write the handler — never the reverse
- Envelope Pattern: All responses wrapped in
{ success, data?, error? }
- Flat Errors: Client receives
{ error: "message" } — never stack traces
- Type Safety: Export schemas for use in both API routes and client code
- Versioning: If breaking changes needed, discuss with user first
Response Envelope
Success Response
{
"success": true,
"data": { ... }
}
Error Response
{
"success": false,
"error": "Human-readable error message"
}
Validation Error Response
{
"success": false,
"error": "Validation failed",
"details": {
"fieldErrors": { "email": ["Invalid email format"] },
"formErrors": []
}
}
Schema Pattern
import { z } from "zod";
export const CreateFeedbackSchema = z.object({
type: z.enum(["bug", "content", "feature", "rating"]),
message: z.string().min(10).max(2000),
rating: z.number().int().min(1).max(5).optional(),
});
export type CreateFeedbackInput = z.infer<typeof CreateFeedbackSchema>;
const parsed = CreateFeedbackSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json({
success: false,
error: "Validation failed",
details: parsed.error.flatten(),
}, { status: 400 });
}
API Route Checklist
[ ] Zod schema defined BEFORE the handler
[ ] Input validated with safeParse (not parse)
[ ] Success returns { success: true, data }
[ ] Error returns { success: false, error }
[ ] Auth checked for protected routes
[ ] No raw error details exposed
Auto-Clone to Project
When you first detect this project has API routes:
- Create
.agent/skills/api-contracts/ in the project root
- Copy this SKILL.md as the base
- Read the project's API routes and existing schemas
- Append a
## Project-Specific section listing all routes, their schemas, and methods
- Inform the user: "I've set up the local api-contracts skill with your project's API route inventory."