A lightweight, specification-first Go API mock server.
Generate realistic mock responses directly from your OpenAPI 3.0 specifications. No code generation, no complex setupβjust a single command.
- π Specification-First: Instantly mock any API by providing an OpenAPI 3.0 (YAML/JSON) file.
- π― Realistic Data Generation: Automatically generates context-aware mock data from OpenAPI schemas - realistic emails, names, dates, and constraint-compliant values when explicit examples are missing.
- β‘οΈ Dynamic Mocking: Test any scenario on the fly. Override status codes (
?__statusCode=404
) and simulate network latency (?__delay=500ms
) with simple query parameters. - π₯ Hot Reload: Automatically reloads OpenAPI specifications and configuration files without server restart for rapid development.
- π Secure Mocking: Full HTTPS/TLS support for testing secure clients and mimicking production environments.
- π Smart Proxy: Automatically forwards requests for undefined endpoints to a real backend server, enabling hybrid mocking with configurable timeouts.
- π Full Observability: Structured JSON logging, and health/readiness endpoints.
- π¦ Zero Dependencies: A single, cross-platform binary with no runtime dependencies. Works on Linux, macOS, and Windows.
- π§ Developer-Friendly: Simple CLI with comprehensive flags, seamless integration with tools like Insomnia, and extensive development tooling.
- π’ Production-Ready: Enterprise-grade architecture with comprehensive testing, Docker support, and configuration management.
- π― Use Cases
- π Quick Start
- π Core Usage
- βοΈ Configuration
- π³ Docker Usage
- π¨βπ» Development
- π£οΈ Roadmap
- π Acknowledgments
- π License
Go-Spec-Mock is designed for API development and testing workflows where you need realistic mock servers without writing backend code.
- Build UIs against accurate API contracts before backend exists
- Test error states and edge cases with status code overrides (
__statusCode=404
) - Work with realistic data shapes from OpenAPI examples
- Rapid prototyping with hot-reload enabled specs
- Validate API designs with stakeholders using live mocks
- Test integration points between services
- Create consistent test environments across development teams
- Contract-first development - spec drives both mock and implementation
- Automated testing with predictable responses
- Load testing with cached responses
- Contract testing between services
- Regression testing with versioned specs
- Spin up mock servers for integration tests
- Parallel development when services are unavailable
- Environment-specific mock configurations
- Deployment validation using production-like data
- Design Phase: Write OpenAPI spec β Start mock server β Share with team
- Development: Frontend consumes mock β Backend implements against same spec
- Testing: Automated tests use mocks β Validate against real implementation
- Deployment: Replace mocks with real services gradually
30 seconds to your first mock API:
# Install
go install github.com/leslieo2/go-spec-mock@latest
# Start mocking with the Petstore example
go-spec-mock --spec-file ./examples/petstore.yaml
# Test it
curl http://localhost:8080/pets
That's it! Your mock API is running with realistic responses from the OpenAPI spec.
- Go version 1.24 or later
Start with your OpenAPI spec:
go-spec-mock --spec-file ./your-api.yaml
Test different scenarios:
# Default response
curl http://localhost:8080/users
# Test error cases
curl "http://localhost:8080/users?__statusCode=404"
curl "http://localhost:8080/users?__statusCode=500"
Design-first workflow with Insomnia:
- Design API in Insomnia β Export as OpenAPI 3.0
go-spec-mock ./api-spec.yaml
- Test against
http://localhost:8080
For a comprehensive, grouped overview of all configuration options, including their YAML/JSON keys, CLI flags, environment variables, and default values, please refer to the Full Configuration Documentation.
Essential flags:
go-spec-mock --spec-file ./api.yaml # Start with spec
go-spec-mock --config ./config.yaml # Use config file
go-spec-mock --hot-reload=false # Disable hot reload
Environment variables:
GO_SPEC_MOCK_SPEC_FILE=./api.yaml
GO_SPEC_MOCK_PORT=8080
Configuration is applied in the following order (highest β lowest priority):
- Explicit CLI Flags (e.g.,
--port 8443
) - Highest priority - Environment Variables (
GO_SPEC_MOCK_PORT=8443
) - Configuration File Values (
port: "8443"
in config.yaml) - CLI Flag Default Values (e.g., default value for
--port
) - Default Configuration - Built-in defaults (lowest priority)
Important Notes:
- Only explicitly set CLI flags override other configuration sources
- CLI flag default values have lower priority than configuration files
- Environment variables override configuration file values
- For production use, prefer configuration files over CLI flags
Examples:
# β
Config file overrides CLI defaults
# go-spec-mock --config config.yaml # Uses port from config file
# β
Explicit CLI flag overrides everything
# go-spec-mock --config config.yaml --port 8443 # Uses port 8443
# β
CLI default values are used as fallback
# go-spec-mock --spec-file api.yaml # Uses port 8080 (CLI default)
Basic mock server:
server:
host: localhost
port: 8080
With proxy fallback:
proxy:
enabled: true
target: "https://api.production.com"
timeout: "15s"
For complex setups, especially involving security, a YAML or JSON configuration file is recommended.
# Use a configuration file
go-spec-mock --config ./config.yaml --spec-file ./examples/petstore.yaml
Example configuration files are provided:
examples/config/go-spec-mock.yaml
- Complete configuration with all options.examples/config/minimal.yaml
- Minimal required configuration.examples/config/security-focused.yaml
- Security-first configuration.
Enable automatic reloading of OpenAPI specifications without server restart:
# Hot Reload Configuration
hot_reload:
enabled: true
debounce: "500ms"
With hot reload enabled, simply save your OpenAPI spec file and the server will automatically reload the new specifications without requiring a restart.
The proxy feature allows go-spec-mock
to act as a pass-through for any requests that are not defined in your OpenAPI specification. This is useful when you want to mock a few endpoints of a larger, existing API without needing to define the entire API surface.
When enabled, if a request comes in for a path that is not in the spec file (e.g., /api/v1/existing-endpoint
), it will be forwarded to the configured target server.
# Proxy configuration
proxy:
enabled: true
target: "https://api.production.com" # The real backend API
timeout: "15s"
With this configuration, you can make requests to http://localhost:8080
:
- Requests to paths defined in your
spec.yaml
(e.g.,/pets
) will be mocked bygo-spec-mock
. - Requests to any other path (e.g.,
/users
,/orders
) will be proxied tohttps://api.production.com
.
Secure your mock server with rate limiting and CORS. These are best configured via a configuration file.
Configure Cross-Origin Resource Sharing (CORS) and other security headers for enterprise-grade protection.
Example config.yaml
:
security:
cors:
enabled: true
allowed_origins: ["http://localhost:3000", "https://yourdomain.com"]
allowed_methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
allowed_headers: ["Content-Type", "Authorization", "X-API-Key"]
allow_credentials: false
max_age: 86400
Enable HTTPS to serve your mock API over a secure connection. This is essential for testing clients that require HTTPS or for more closely mirroring a production environment.
To enable TLS, you need a certificate and a private key file. For local development, you can generate a self-signed certificate using openssl
:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes
Then, configure the server using a configuration file:
Example config.yaml
:
tls:
enabled: true
cert_file: "cert.pem" # Path to your TLS certificate file
key_file: "key.pem" # Path to your TLS private key file
You can also enable TLS and specify file paths via CLI flags:
go-spec-mock --spec-file ./api.yaml --tls-enabled --tls-cert-file cert.pem --tls-key-file key.pem
When enabled, the server will run on HTTPS only. Any HTTP requests to the same port will fail.
The server provides built-in observability endpoints:
Endpoint | Description |
---|---|
/docs |
API documentation with available endpoints |
/health |
Health check endpoint with service status |
/ready |
Readiness probe for load balancers |
Usage examples:
# Check health status
curl http://localhost:8080/health
A Dockerfile
is included for easy containerization.
# 1. Build the Docker image
docker build -t go-spec-mock .
# 2. Run the container with a mounted config file
docker run -p 8080:8080 -p 9090:9090 \
-v $(pwd)/examples:/app/examples \
go-spec-mock:latest --config ./examples/config/minimal.yaml --spec-file ./examples/petstore.yaml
# 3. Run with configuration via environment variables
docker run -p 8081:8081 -p 9091:9091 \
-e GO_SPEC_MOCK_PORT=8081 \
-v $(pwd)/examples/petstore.yaml:/app/petstore.yaml \
go-spec-mock:latest --spec-file /app/petstore.yaml
Contributions are welcome! Please feel free to open an issue or submit a pull request.
- Clone the repository:
git clone https://github.com/leslieo2/go-spec-mock.git cd go-spec-mock
- Install dependencies:
go mod tidy
This project uses a Makefile
to streamline common development tasks.
Command | Description |
---|---|
make build |
Build the go-spec-mock binary for your OS. |
make run-example |
Run the server with the example petstore.yaml spec. |
make run-example-secure |
Run with security features enabled. |
make run-example-secure-config |
Run with security-focused configuration. |
make run-example-minimal |
Run with minimal configuration. |
make test |
Run all tests with coverage report. |
make test-quick |
Run tests without coverage. |
make fmt |
Format the Go source code. |
make lint |
Run golangci-lint to check for code quality issues. |
make vet |
Run go vet for static analysis. |
make security |
Run security scan with gosec . |
make ci |
Run the full CI pipeline (format, lint, test, build). |
make build-all |
Cross-compile binaries for Linux, macOS, and Windows. |
make build-version |
Build with version information. |
make curl-test |
Run automated curl tests against the example server. |
make curl-interactive |
Interactive curl testing session. |
make docker |
Build Docker image. |
make docker-run |
Run with petstore example in container. |
make dev |
Start development server. |
make watch |
Watch for file changes and rebuild. |
.
βββ Makefile # Development commands
βββ README.md # This file
βββ go.mod # Go module definition
βββ main.go # CLI entry point
βββ examples/
β βββ petstore.yaml # Sample OpenAPI spec
β βββ uspto.yml # USPTO API specification example
β βββ config/ # Configuration examples
βββ internal/
β βββ config/ # Configuration management (YAML/JSON)
β βββ parser/ # OpenAPI specification parsing logic
β βββ server/ # HTTP server and routing logic
β β βββ middleware/ # HTTP middleware chain (CORS, security, logging, proxy)
β βββ security/ # Rate limiting and CORS
β βββ observability/ # Logging, and health checks
β βββ hotreload/ # Hot reload functionality for specs and config
β βββ proxy/ # Proxy functionality for undefined endpoints
βββ Dockerfile # Multi-stage Docker build
βββ CHANGELOG.md # Version history and features
βββ LICENSE # Apache 2.0 license
The project is currently at v1.5.1 and is production-ready with enterprise-grade security, observability, and configuration features. All core functionality is complete and battle-tested.
β Phase 1: Core Features (Complete)
- OpenAPI 3.0 specification parsing
- Dynamic HTTP routing from spec paths
- Static example response generation
- Dynamic status code override (
__statusCode
) - Cross-platform builds (Linux, macOS, Windows)
- Comprehensive unit tests and documentation
β Phase 2: Enterprise Enhancements (Complete)
- Request size limiting
- Configurable log levels (DEBUG, INFO, WARN, ERROR)
- Comprehensive security configuration (YAML/JSON)
- CORS configuration with security headers
- Structured (JSON) logging
- Health check endpoint (
/health
) - Readiness probe (
/ready
)
- CORS (Cross-Origin Resource Sharing) configuration
- Configuration via CLI flags and environment variables
- Customizable server timeouts and ports
- HTTPS/TLS support
- Configuration file support (YAML/JSON)
- Docker support with multi-stage builds
- Official Docker images on Docker Hub
- Example Helm charts for Kubernetes deployment
- Hot reload for specifications and configuration
- Comprehensive CLI flags and environment variables
π― Phase 3: Enhanced Core Mocking & Developer Experience (In Progress)
- Dynamic Data Generation - Generate realistic mock data from schema when examples are missing
- Named Example Selection - Support
__example=exampleName
parameter to select specific examples - Response Latency Simulation - Add
__delay=500ms
parameter to simulate network delays
- CLI Endpoint Listing - Show all mock endpoints on server startup
- Easier Installation - Pre-compiled binaries, Homebrew/Scoop packages, and Docker Hub releases
- Enhanced Documentation - Interactive API docs with try-it functionality
- Simple State Management - In-memory storage for basic stateful API scenarios
- CRUD Operations Support - Create, read, update, delete operations with persistent state
π Phase 4: Advanced Integration & Ecosystem (Planned)
- Intelligent Proxy Routing - Configurable proxy rules based on path patterns
- Response Transformation - Modify proxied responses to match expected formats
- Request Filtering - Selective proxy based on headers or query parameters
- JWT Validation - Simple JWT signature verification for testing authenticated clients
- Basic Auth Support - Mock authentication for testing authorization flows
- WebSocket Mocking - Support for real-time API mocking through OpenAPI extensions
- GraphQL Support - Mock GraphQL APIs with schema-based response generation
π Phase 5: Ecosystem & Community Growth (Future Vision)
- Programmatic API - Expose core mocking functionality as a Go library for testing
- Testing Integration - Seamless integration with Go testing frameworks
- VS Code Extension - GUI for managing mock servers and configurations
- CLI Autocomplete - Smart autocomplete for configuration and commands
- Plugin System - Extensible architecture for custom response generators
- OpenAPI Extensions - Contribute to OpenAPI specification for enhanced mocking capabilities
- API Blueprint Support - Expand support to additional API specification formats
- kin-openapi for its robust OpenAPI 3.0 parsing library.
- Insomnia for inspiring a seamless design-first workflow.
- The Go Team for the powerful and simple standard library.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.