Skip to content

A fast API contract validation and regression testing tool that validates live API responses against OpenAPI/Swagger specifications. A fast API contract validation and regression testing tool.

License

Notifications You must be signed in to change notification settings

BaseMax/go-api-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

go-api-contract

A fast API contract validation and regression testing tool that validates live API responses against OpenAPI/Swagger specifications.

Features

  • πŸ” 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

Installation

From Source

git clone https://github.com/BaseMax/go-api-contract.git
cd go-api-contract
go build -o apicontract ./cmd/apicontract

Using Go Install

go install github.com/BaseMax/go-api-contract/cmd/apicontract@latest

Usage

Basic Usage

apicontract -spec openapi.yaml -endpoint https://api.example.com/users/1 -method GET

With Request Body (POST/PUT/PATCH)

apicontract -spec openapi.yaml \
  -endpoint https://api.example.com/posts \
  -method POST \
  -body '{"title":"Test","body":"Content","userId":1}'

With Headers

apicontract -spec openapi.yaml \
  -endpoint https://api.example.com/protected \
  -method GET \
  -headers '{"Authorization":"Bearer token123","Content-Type":"application/json"}'

JSON Output

apicontract -spec openapi.yaml \
  -endpoint https://api.example.com/users/1 \
  -method GET \
  -json

Verbose Mode

apicontract -spec openapi.yaml \
  -endpoint https://api.example.com/users/1 \
  -method GET \
  -verbose

Environment Variables

The 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.

Command-Line Flags

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 -

Example OpenAPI Specification

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

Output Examples

Colored CLI Output (Default)

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  API Contract Validation Report
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

βœ“ All validations passed

Timestamp: 2024-01-15T10:30:00Z
Status Code: 200
Errors Found: 0
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

JSON Output

{
  "valid": true,
  "statusCode": 200,
  "responseBody": {
    "id": 1,
    "name": "John Doe"
  },
  "timestamp": "2024-01-15T10:30:00Z",
  "errorCount": 0
}

Testing with JSONPlaceholder

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}'

Exit Codes

  • 0: Validation passed successfully
  • 1: Validation failed or error occurred

Libraries Used

  • 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

Project Structure

.
β”œβ”€β”€ 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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

Author

Copyright (c) 2025 Max Base

About

A fast API contract validation and regression testing tool that validates live API responses against OpenAPI/Swagger specifications. A fast API contract validation and regression testing tool.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •