Skip to main content
OpenAPI (formerly known as Swagger) is a standard way to describe REST APIs. Contract automatically generates an OpenAPI specification from your service - no manual YAML writing needed!

What Is OpenAPI?

OpenAPI is a specification format that describes your API:
  • What endpoints exist
  • What parameters they accept
  • What responses they return
  • What your data types look like
This specification can be used to:
  • Generate documentation: Beautiful, interactive API docs with Swagger UI or Redoc
  • Generate client code: Create typed clients in TypeScript, Python, Go, and more
  • Test your API: Import into Postman, Insomnia, or other API tools
  • Validate requests: Ensure requests match the expected format

Quick Start

Generate and serve OpenAPI documentation:
Now visit http://localhost:8080/openapi.json to see your API specification.

What Gets Generated

Contract analyzes your service and generates a complete OpenAPI 3.0.3 specification.

From Your Interface

To OpenAPI Specification

Adding Interactive Documentation

The JSON spec is machine-readable. For human-readable docs, add Swagger UI or Redoc.

Option 1: Swagger UI

Swagger UI provides an interactive interface where users can try your API:
Now visit http://localhost:8080/docs to see interactive documentation!

Option 2: Redoc

Redoc provides clean, three-panel documentation:

Complete Example

This example shows OpenAPI generation using the recommended package-based organization:

Using the OpenAPI Spec

Generate Client Code

Use tools like openapi-generator to create typed clients:

Import into Postman

  1. Open Postman
  2. Click Import
  3. Enter URL: http://localhost:8080/openapi.json
  4. Your API is now ready to test!

Validate with oasdiff

Check for breaking changes between versions:

Service Metadata

Add service information that appears in the generated spec:
This produces:

Multiple Services

Generate specs for multiple services:

Type Mappings

Contract converts Go types to OpenAPI schema types:

Optional Fields

Use omitempty to mark fields as optional:
This produces:

Path Parameters

Fields with the path tag are documented as path parameters:
This produces:

Query Parameters

For GET requests, input fields not used in the path become query parameters:
This produces:

Common Questions

How do I add descriptions to endpoints?

Method descriptions come from the contract.WithDescription option at the method level (not yet implemented). For now, the operation ID includes the method name.

What OpenAPI version is generated?

OpenAPI 3.0.3, a widely supported version.

Can I customize the generated spec?

The current generator produces a standard spec. For extensive customization:
  1. Generate the spec with rest.OpenAPIDocument(svc)
  2. Parse and modify it programmatically
  3. Serve the modified version

Does it support authentication schemes?

Yes, when using WithDefaults with auth settings:
This adds bearer token security to the spec.

Can I add custom tags or extensions?

Not directly. The generator creates a minimal, standards-compliant spec. For advanced customization, modify the generated spec programmatically.

What’s Next?