A fast API contract validation and regression testing tool that validates live API responses against OpenAPI/Swagger specifications.
- π OpenAPI/Swagger Support: Read and validate against OpenAPI 3.0+ specifications
- β Schema Validation: Validate API responses against defined schemas
- π HTTP Client: Built-in HTTP client using Go's
net/http - π Environment Variables: Support for environment variable substitution and secret injection
- π Multiple Output Formats: Colored CLI output or machine-readable JSON
- π― Diff Reporting: Detailed error reporting with validation differences
- π Fast & Lightweight: Written in Go for maximum performance
git clone https://github.com/BaseMax/go-api-contract.git
cd go-api-contract
go build -o apicontract ./cmd/apicontractgo install github.com/BaseMax/go-api-contract/cmd/apicontract@latestapicontract -spec openapi.yaml -endpoint https://api.example.com/users/1 -method GETapicontract -spec openapi.yaml \
-endpoint https://api.example.com/posts \
-method POST \
-body '{"title":"Test","body":"Content","userId":1}'apicontract -spec openapi.yaml \
-endpoint https://api.example.com/protected \
-method GET \
-headers '{"Authorization":"Bearer token123","Content-Type":"application/json"}'apicontract -spec openapi.yaml \
-endpoint https://api.example.com/users/1 \
-method GET \
-jsonapicontract -spec openapi.yaml \
-endpoint https://api.example.com/users/1 \
-method GET \
-verboseThe tool supports environment variable substitution in URLs, headers, and request bodies:
# Set environment variables
export API_BASE_URL=https://api.example.com
export API_TOKEN=your_secret_token
# Use in commands
apicontract -spec openapi.yaml \
-endpoint '${API_BASE_URL}/users/1' \
-headers '{"Authorization":"Bearer ${API_TOKEN}"}'
# Alternative syntax
apicontract -spec openapi.yaml \
-endpoint '$API_BASE_URL/users/1' \
-headers '{"Authorization":"Bearer $API_TOKEN"}'Both ${VAR_NAME} and $VAR_NAME formats are supported.
| Flag | Description | Required | Default |
|---|---|---|---|
-spec |
Path to OpenAPI/Swagger specification file | Yes | - |
-endpoint |
API endpoint URL to validate | Yes | - |
-method |
HTTP method to use | No | GET |
-body |
Request body (for POST/PUT/PATCH) | No | - |
-headers |
Request headers as JSON object | No | - |
-json |
Output results as JSON | No | false |
-verbose |
Enable verbose output | No | false |
-version |
Print version information | No | - |
See examples/openapi.yaml for a complete example.
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/users/{id}:
get:
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
'200':
description: Success
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
required:
- id
- nameβββββββββββββββββββββββββββββββββββββββββββββββββ
API Contract Validation Report
βββββββββββββββββββββββββββββββββββββββββββββββββ
β All validations passed
Timestamp: 2024-01-15T10:30:00Z
Status Code: 200
Errors Found: 0
βββββββββββββββββββββββββββββββββββββββββββββββββ
{
"valid": true,
"statusCode": 200,
"responseBody": {
"id": 1,
"name": "John Doe"
},
"timestamp": "2024-01-15T10:30:00Z",
"errorCount": 0
}The included example uses the JSONPlaceholder API for testing:
# Build the tool
go build -o apicontract ./cmd/apicontract
# Test GET request
./apicontract -spec examples/openapi.yaml \
-endpoint https://jsonplaceholder.typicode.com/posts/1 \
-method GET
# Test POST request
./apicontract -spec examples/openapi.yaml \
-endpoint https://jsonplaceholder.typicode.com/posts \
-method POST \
-body '{"title":"foo","body":"bar","userId":1}'0: Validation passed successfully1: Validation failed or error occurred
- kin-openapi - OpenAPI 3.0 implementation for Go
- fatih/color - Colored terminal output
- gorilla/mux - HTTP routing (via kin-openapi)
- Standard Go
net/http- HTTP client
.
βββ cmd/
β βββ apicontract/ # CLI entry point
β βββ main.go
βββ pkg/
β βββ client/ # HTTP client with env var support
β β βββ client.go
β βββ loader/ # OpenAPI spec loader
β β βββ loader.go
β βββ validator/ # Schema validation logic
β β βββ validator.go
β βββ reporter/ # Report generation
β β βββ reporter.go
β βββ output/ # Output formatting
β βββ output.go
βββ examples/
β βββ openapi.yaml # Sample OpenAPI spec
βββ go.mod
βββ go.sum
βββ README.md
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the GPL-3.0 License - see the LICENSE file for details.
Copyright (c) 2025 Max Base