New to Claude Skills? Learn how to install them →

jeremylongshore on GitHub

Generating API Contracts

Free

Automate API contract generation and testing.

Get this skill

Free · Opens the source repo

What Generating API Contracts does

Generating API Contracts is a skill designed for developers and teams who need to create and maintain API contracts efficiently. By generating OpenAPI specifications from existing API implementations, design documents, or database schemas, this skill ensures that your API documentation remains accurate and up-to-date. It integrates with consumer-driven contract testing frameworks like Pact, enabling teams to verify that their API implementations meet consumer expectations. This can significantly reduce the risk of breaking changes during development and deployment.

The skill operates by scanning your existing codebase to extract relevant information about API endpoints, including HTTP methods, request parameters, and response formats. It then automatically generates a comprehensive OpenAPI specification that serves as a single source of truth for documentation, code generation, and testing. This automation streamlines the process of API documentation, making it easier for teams to focus on development rather than manual updates.

In addition to generating OpenAPI specs, the skill also creates consumer-driven contract tests, which capture the expected interactions from the perspective of API consumers. This helps ensure that any changes made to the API do not break existing functionality relied upon by clients. The generated artifacts, including OpenAPI specs and Pact JSON contracts, are versioned alongside your API source code, facilitating a smooth workflow and collaboration among team members.

This skill is particularly valuable for teams adopting a microservices architecture or those working with multiple API consumers, as it promotes clear communication and expectations between frontend and backend teams. By leveraging this skill, organizations can enhance their API development process, improve collaboration, and maintain high-quality standards in their API offerings.

When to use it

Use this skill when you need to document APIs or generate OpenAPI specifications from existing code or design documents.

When not to use it

This skill may not be suitable for simple projects with minimal API requirements or for teams not utilizing consumer-driven contract testing.

What you can build with it

Code-first OpenAPI Generation

Automatically generate OpenAPI specifications from your existing Express routes and validation schemas.

Consumer-driven Contract Testing

Use Pact to define expected API interactions and verify them during CI, ensuring backend changes do not break frontend functionality.

Design-first Workflow

Create OpenAPI specs in tools like Stoplight Studio and generate server stubs to guide your API implementation.

How to install Generating API Contracts

View source

1. Install with the skills CLI

npx skills add jeremylongshore/claude-code-plugins-plus-skills/generating-api-contracts --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by jeremylongshore

Generating API Contracts

Overview

Generate OpenAPI 3.0/3.1 specifications and consumer-driven contract tests from existing API implementations, design documents, or database schemas. Produce machine-readable contracts that serve as the single source of truth for code generation, documentation, testing, and gateway configuration, with Pact integration for consumer-driven contract verification.

Prerequisites

  • API implementation with route definitions and handler logic, or design requirements document
  • OpenAPI authoring tool: Swagger Editor, Stoplight Studio, or IDE with OpenAPI extension
  • Consumer-driven contract framework: Pact (polyglot), Spring Cloud Contract (Java), or Dredd (generic)
  • Schema validation tool: Spectral for OpenAPI linting
  • Version control for contract files with diff-based review process

Instructions

  1. Scan existing route handlers and controller files using Grep and Read to extract all endpoint paths, HTTP methods, request parameter names/types, and response body shapes.
  2. Generate OpenAPI 3.0 specification from the extracted data, including info (title, version, description), servers (environment URLs), paths (operations), and components (reusable schemas).
  3. Define request schemas with field-level constraints: type, format, required, minimum/maximum, pattern (regex), enum, and example values for every property.
  4. Document all response status codes per endpoint with separate schemas: 200/201 for success, 400 for validation errors (with field-level error array), 401/403 for auth failures, and 404/500.
  5. Add security scheme definitions (bearerAuth, apiKey, oauth2) and apply them to appropriate operations using the security field.
  6. Create Pact consumer contract tests that capture expected interactions from the API consumer perspective, defining expected request/response pairs per endpoint.
  7. Set up provider verification that replays Pact interactions against the actual API implementation, verifying the provider satisfies all consumer expectations.
  8. Generate contract artifacts: OpenAPI spec file, Postman collection, and consumer contract (Pact JSON), all versioned alongside the API source code.

See ${CLAUDE_SKILL_DIR}/references/implementation.md for the full implementation guide.

Output

  • ${CLAUDE_SKILL_DIR}/openapi.yaml - Complete OpenAPI 3.0/3.1 specification
  • ${CLAUDE_SKILL_DIR}/contracts/pact/ - Consumer-driven contract definitions (Pact JSON)
  • ${CLAUDE_SKILL_DIR}/contracts/postman/ - Generated Postman collection for API testing
  • ${CLAUDE_SKILL_DIR}/tests/contract/consumer/ - Consumer contract test implementations
  • ${CLAUDE_SKILL_DIR}/tests/contract/provider/ - Provider verification test suite
  • ${CLAUDE_SKILL_DIR}/scripts/generate-contract.sh - Contract generation automation script

Error Handling

ErrorCauseSolution
Spec-code divergenceAPI implementation changed without updating the OpenAPI specAdd CI check that generates spec from code and diffs against committed spec
Pact verification failureProvider response does not match consumer expectationReview consumer contract for correctness; update provider if contract is valid
Missing operation IDEndpoint has no operationId, preventing code generationGenerate deterministic operation IDs from method + path (e.g., getUsers, createUser)
Circular schema referenceComponents reference each other creating infinite recursionBreak cycles with allOf composition or introduce intermediate types
Example/schema mismatchExample values do not validate against their own schemaAuto-validate all examples during spec generation; reject mismatched examples

Refer to ${CLAUDE_SKILL_DIR}/references/errors.md for comprehensive error patterns.

Examples

Code-first OpenAPI generation: Scan Express route decorators and Zod validation schemas to auto-generate a complete OpenAPI 3.1 spec with accurate request/response schemas, examples, and descriptions.

Consumer-driven contract testing: Frontend team publishes Pact contracts defining the API interactions they depend on; backend CI verifies every contract on each deployment, preventing breaking changes.

Design-first workflow: Author OpenAPI spec in Stoplight Studio, generate server stubs and client SDKs from the spec, then implement business logic in the stubs -- spec stays as the single source of truth.

See ${CLAUDE_SKILL_DIR}/references/examples.md for additional examples.

Resources

Frequently asked questions about Generating API Contracts

Similar skills