api-contract-sync
Keeps API contracts synchronized between frontend and backend services. Detects contract drift, generates client SDKs, and manages API versioning.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Keeps API contracts synchronized between frontend and backend services. Detects contract drift, generates client SDKs, and manages API versioning.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Generate customized .claude/ harness framework with Fusion Architecture (GAN-inspired + Domain Specialists). TRIGGER when user wants to set up AI-assisted development structure, create a new project harness, add structure to existing projects, or mentions 'harness', 'scaffold', 'framework setup'. Supports all domains through template + dynamic generation.
Negotiates 'done' criteria between Generator, Evaluator, and architect-lead before implementation begins. Converts subjective goals into testable, gradable criteria with clear domain responsibilities.
Guided database migration workflow — from schema design to rollback documentation.
Production incident response workflow — triage, root cause analysis, and resolution.
Track, categorize, and prioritize technical debt across the codebase.
Save current progress with structured handoff artifact. Enables context reset for long-running tasks and session recovery.
| name | api-contract-sync |
| description | Keeps API contracts synchronized between frontend and backend services. Detects contract drift, generates client SDKs, and manages API versioning. |
| user-invocable | true |
You are facilitating API contract synchronization between frontend and backend services. Your role is to ensure that API contracts remain consistent across all consumers and producers.
API contract drift causes:
This skill prevents drift by:
Identify All Contracts
Map Contract Sources
## Contract Registry
| Contract | Producer | Consumers | Version | Location |
|----------|----------|-----------|---------|----------|
| User API | user-service | frontend-app, mobile-app | v1.2.0 | /specs/user-api.yaml |
| Order API | order-service | frontend-app, analytics | v2.0.0 | /specs/order-api.yaml |
| User Events | user-service | notification, analytics | v1.0.0 | /schemas/user-events.avsc |
Validate Contract Completeness
Compare Specification vs Implementation
## Drift Detection Report
### User API (user-service)
| Endpoint | Spec | Implementation | Status |
|----------|------|----------------|--------|
| GET /users | Documented | Exists | OK |
| POST /users | Documented | Exists | DRIFT |
| DELETE /users/{id} | Not documented | Exists | MISSING_IN_SPEC |
| GET /users/search | Documented | Not found | MISSING_IN_IMPL |
### DRIFT Details: POST /users
- Spec: request.body.email (required)
- Impl: request.body.email (optional)
- Impact: Frontend validation may be too strict
Detect Breaking Changes
## Breaking Change Detection
### user-service: v1.1.0 -> v1.2.0
| Change | Breaking | Affected Consumers |
|--------|----------|-------------------|
| Added required field `phone` to User | YES | frontend-app |
| Removed endpoint GET /users/legacy | YES | analytics-service |
| Added optional field `avatar` to User | NO | - |
| Changed `/users/search` to `/users?q=` | YES | mobile-app |
### Action Required
1. Notify frontend-app about required field
2. Verify analytics-service doesn't use legacy endpoint
3. Update mobile-app for search endpoint change
Generate Drift Report
## Contract Drift Summary
- **Total Contracts**: 5
- **With Drift**: 2
- **Breaking Changes**: 3
- **Missing Documentation**: 4
### Priority Fixes
1. [HIGH] user-service: Breaking change affects 2 consumers
2. [MEDIUM] order-service: Missing documentation for 3 endpoints
3. [LOW] notification-service: Minor schema drift
Resolve Drift
## Resolution Options
### Option 1: Update Specification (Prefer for Missing Documentation)
- Update spec to match implementation
- Notify consumers of documented behavior
- Update SDKs
### Option 2: Update Implementation (Prefer for Breaking Changes)
- Revert breaking changes in implementation
- Add new endpoint/version instead
- Maintain backward compatibility
### Option 3: Version Bump (Prefer for Intentional Changes)
- Create new API version
- Support both versions during transition
- Migrate consumers incrementally
- Deprecate old version
Generate Client SDKs
## SDK Generation
### Frontend SDK (TypeScript)
```bash
openapi-generator generate \
-i specs/user-api.yaml \
-g typescript-axios \
-o clients/frontend/user-api
openapi-generator generate \
-i specs/user-api.yaml \
-g swift5 \
-o clients/ios/user-api
openapi-generator generate \
-i specs/user-api.yaml \
-g kotlin \
-o clients/android/user-api
Validate SDK Compatibility
## SDK Validation Checklist
- [ ] Generated types match spec
- [ ] All endpoints callable
- [ ] Error types correct
- [ ] Authentication headers included
- [ ] TypeScript types compile
- [ ] Example requests work
Version Strategy
## API Versioning Strategy
### URL Path Versioning (Preferred)
- /api/v1/users
- /api/v2/users
### Header Versioning (Alternative)
- Accept: application/vnd.api+json;version=1
### Version Lifecycle
| Phase | Duration | Support Level |
|-------|----------|---------------|
| Current | Active | Full support, new features |
| Deprecated | 6 months | Security fixes only |
| Retired | N/A | No support |
Deprecation Process
## Deprecation Checklist
### Announce Deprecation
- [ ] Add `deprecated: true` to spec
- [ ] Add `Deprecation` header in responses
- [ ] Add `Sunset` header with removal date
- [ ] Notify all consumers
### Migration Period
- [ ] Provide migration guide
- [ ] Offer both versions in parallel
- [ ] Monitor usage of deprecated version
- [ ] Support consumers during migration
### Removal
- [ ] Verify no consumers using deprecated version
- [ ] Remove deprecated endpoints
- [ ] Update documentation
- [ ] Archive old SDKs
# api-contract.yaml
openapi: 3.0.3
info:
title: User API
version: 1.2.0
description: User management service
servers:
- url: https://api.example.com/v1
description: Production
- url: https://api-staging.example.com/v1
description: Staging
paths:
/users:
get:
summary: List users
operationId: listUsers
tags:
- Users
parameters:
- name: limit
in: query
schema:
type: integer
default: 20
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/UserList'
'401':
$ref: '#/components/responses/Unauthorized'
components:
schemas:
User:
type: object
required:
- id
- email
properties:
id:
type: string
format: uuid
email:
type: string
format: email
responses:
Unauthorized:
description: Authentication required
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
#!/bin/bash
# drift-check.sh - Check for API drift
CONTRACTS_DIR="specs"
IMPLEMENTATIONS_DIR="src"
for spec in "$CONTRACTS_DIR"/*.yaml; do
service=$(basename "$spec" .yaml)
echo "Checking $service..."
# Compare spec with implementation
diff-output=$(diff \
<(yq eval '.paths | keys' "$spec") \
<(grep -r "router\." "$IMPLEMENTATIONS_DIR/$service" | grep -oP '(?<=["'"'"']).+(?=["'"'"'])' | sort -u)
)
if [ -n "$diff-output" ]; then
echo "DRIFT DETECTED in $service:"
echo "$diff-output"
fi
done
/api-contract-sync [service-name]
This will:
# .github/workflows/api-contract-check.yml
name: API Contract Check
on:
pull_request:
paths:
- 'specs/**'
- 'src/**/routes/**'
jobs:
drift-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install tools
run: npm install -g @apidevtools/swagger-cli
- name: Validate specs
run: |
for spec in specs/*.yaml; do
swagger-cli validate "$spec"
done
- name: Check for drift
run: ./scripts/drift-check.sh
- name: Generate SDKs
run: ./scripts/generate-sdks.sh
- name: Upload SDKs
uses: actions/upload-artifact@v3
with:
name: generated-sdks
path: clients/
#!/bin/bash
# .git/hooks/pre-commit
# Check for API drift before committing
if git diff --cached --name-only | grep -q "specs/"; then
echo "API specs changed, running drift check..."
./scripts/drift-check.sh
if [ $? -ne 0 ]; then
echo "API drift detected. Please resolve before committing."
exit 1
fi
fi