A CLI tool that validates your API responses against OpenAPI/Swagger specifications. Ensure your API implementation matches your documented contract with automated schema validation, status code checks, and comprehensive error reporting.
- OpenAPI 3.0 Support: Full support for OpenAPI 3.0 specifications
- Schema Validation: Validates response bodies against JSON schemas using AJV
- Status Code Checking: Ensures responses use documented status codes
- Batch Testing: Validate multiple endpoints from a configuration file
- Detailed Reporting: Get comprehensive error reports with expected vs. received values
- CI/CD Integration: Perfect for automated testing pipelines
- Export Reports: Generate JSON reports for further analysis
npm install -g api-contract-validatorOr use directly with npx:
npx api-contract-validatorValidate a single endpoint:
api-contract-validator validate \
--spec ./openapi.yaml \
--url http://localhost:3000 \
--path /api/users \
--method GETList all endpoints from your spec:
api-contract-validator list --spec ./openapi.yamlValidate a single API endpoint against your OpenAPI spec.
api-contract-validator validate \
--spec <path-to-spec> \
--url <api-base-url> \
--path <endpoint-path> \
--method <http-method> \
[--data <json-body>] \
[--headers <json-headers>]Options:
-s, --spec <path>- Path to OpenAPI/Swagger spec file (required)-u, --url <url>- Base URL of the API (required)-p, --path <path>- API endpoint path, e.g.,/api/users(required)-m, --method <method>- HTTP method: GET, POST, PUT, DELETE, etc. (required)-d, --data <json>- Request body as JSON string (optional)-h, --headers <json>- Request headers as JSON string (optional)
Example:
api-contract-validator validate \
--spec ./openapi.yaml \
--url http://localhost:3000 \
--path /api/users \
--method POST \
--data '{"name":"John Doe","email":"john@example.com"}' \
--headers '{"Authorization":"Bearer token123"}'Validate multiple endpoints from a test configuration file.
api-contract-validator validate-all \
--spec <path-to-spec> \
--url <api-base-url> \
--config <test-config-path> \
[--output <report-path>]Options:
-s, --spec <path>- Path to OpenAPI/Swagger spec file (required)-u, --url <url>- Base URL of the API (required)-c, --config <path>- Path to test configuration JSON file (required)-o, --output <path>- Output report to file (optional)
Test Configuration Format:
[
{
"path": "/api/users",
"method": "GET"
},
{
"path": "/api/users",
"method": "POST",
"data": {
"name": "John Doe",
"email": "john@example.com"
},
"headers": {
"Authorization": "Bearer token123"
}
}
]Example:
api-contract-validator validate-all \
--spec ./openapi.yaml \
--url http://localhost:3000 \
--config ./test-config.json \
--output ./validation-report.jsonList all endpoints defined in your OpenAPI specification.
api-contract-validator list --spec <path-to-spec>Example:
api-contract-validator list --spec ./openapi.yamlValidate your API during development to catch contract violations early:
npm run dev & # Start your API server
api-contract-validator validate-all --spec ./openapi.yaml --url http://localhost:3000 --config ./tests.jsonAdd to your GitHub Actions workflow:
- name: Validate API Contract
run: |
npm start &
sleep 5
npx api-contract-validator validate-all \
--spec ./openapi.yaml \
--url http://localhost:3000 \
--config ./api-tests.json \
--output ./contract-report.jsonValidate staging environment before production deployment:
api-contract-validator validate-all \
--spec ./openapi.yaml \
--url https://staging.api.example.com \
--config ./production-tests.json \
--output ./staging-validation.jsonThe validator reports several types of errors:
- schema: Response body doesn't match the defined JSON schema
- status: Response status code not documented in the spec
- header: Required response headers are missing
- missing: Endpoint or method not found in the spec
- Commander.js: CLI framework
- Swagger Parser: OpenAPI spec parsing and validation
- AJV: JSON schema validation with format support
- Axios: HTTP client for API calls
- Chalk: Colorful terminal output
- TypeScript: Type-safe development
Validating 3 endpoints...
✓ PASS GET /api/users (200)
✗ FAIL POST /api/users (422)
└─ [schema] must have required property 'email'
✓ PASS GET /api/users/123 (200)
──────────────────────────────────────────────────
Total: 3 | Pass: 2 | Fail: 1
Clone and install:
git clone https://github.com/patrickboxfordpartners/api-contract-validator.git
cd api-contract-validator
npm installBuild:
npm run buildRun in development:
npm run dev -- validate --spec ./openapi.yaml --url http://localhost:3000 --path /api/users --method GETMIT