# api-contract-testing

Verifies API contracts between microservices with consumer-driven tests and OpenAPI schemas, skipping full end-to-end setups.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 475 installs · ★ 236
- Author: secondsky
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/secondsky/claude-skills/tree/HEAD/plugins/api-contract-testing/skills/api-contract-testing/SKILL.md
- Updated: 2026-10-20
- HTML: https://affpapa.org/en/nevibecode/skills/secondsky-api-contract-testing

## What it does

The skill outlines workflows for verifying interactions between API consumers and providers without full integration environments. It provides TypeScript snippets using `@pact-foundation/pact`, middleware patterns for `express-openapi-validator`, and references for Python JSON Schema or Java REST Assured.

Contracts test structural shape instead of rigid hard-coded values through matchers. Provider verification depends on a running target instance and the `PACT_BROKER_URL` environment variable.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add secondsky/claude-skills --skill api-contract-testing -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/api-contract-testing/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/api-contract-testing/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add secondsky/claude-skills --skill api-contract-testing -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/api-contract-testing/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add secondsky/claude-skills --skill api-contract-testing -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/api-contract-testing/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install secondsky/claude-skills api-contract-testing
```

2. **Manually: put the files in place**
- File: `.github/skills/api-contract-testing/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add secondsky/claude-skills --skill api-contract-testing -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/secondsky/claude-skills --path plugins/api-contract-testing/skills/api-contract-testing
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/api-contract-testing/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add secondsky/claude-skills --skill api-contract-testing -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/secondsky/claude-skills/tree/HEAD/plugins/api-contract-testing/skills/api-contract-testing/SKILL.md

## When to use it
- catch breaking API schema changes across microservices
- write consumer tests without provisioning end-to-end integration testbeds
- validate incoming HTTP payloads against OpenAPI files in Express
- verify provider compliance against contracts published to Pact Broker

## Example prompts
- Write a Pact consumer test for the GET `/users/123` route
- Add OpenAPI schema validation to an Express app using `openapi.yaml`
- Set up provider contract verification for UserService with Pact Broker integration

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: d304dfb344093bec.

BEGIN THIRD-PARTY FILE d304dfb344093bec
````text
---
name: api-contract-testing
description: Verifies API contracts between services using consumer-driven contracts, schema validation, and tools like Pact. Use when testing microservices communication, preventing breaking changes, or validating OpenAPI specifications.
license: MIT
---

# API Contract Testing

Verify that APIs honor their contracts between consumers and providers without requiring full integration tests.

## Key Concepts

| Term | Definition |
|------|------------|
| Consumer | Service that calls an API |
| Provider | Service that exposes an API |
| Contract | Agreed request/response format |
| Pact | Consumer-driven contract testing tool |
| Schema | Structure definition (OpenAPI, JSON Schema) |
| Broker | Central repository for contracts |

## Pact Consumer Test (TypeScript)

```typescript
import { PactV3, MatchersV3 } from '@pact-foundation/pact';

const provider = new PactV3({
  consumer: 'OrderService',
  provider: 'UserService'
});

describe('User API Contract', () => {
  it('returns user by ID', async () => {
    await provider
      .given('user 123 exists')
      .uponReceiving('a request for user 123')
      .withRequest({ method: 'GET', path: '/users/123' })
      .willRespondWith({
        status: 200,
        body: MatchersV3.like({
          id: '123',
          name: MatchersV3.string('John'),
          email: MatchersV3.email('john@example.com')
        })
      })
      .executeTest(async (mockServer) => {
        const response = await fetch(`${mockServer.url}/users/123`);
        expect(response.status).toBe(200);
      });
  });

  it('returns 404 for non-existent user', async () => {
    await provider
      .given('user does not exist')
      .uponReceiving('a request for non-existent user')
      .withRequest({ method: 'GET', path: '/users/999' })
      .willRespondWith({
        status: 404,
        body: MatchersV3.like({
          error: { code: 'NOT_FOUND', message: MatchersV3.string() }
        })
      })
      .executeTest(async (mockServer) => {
        const response = await fetch(`${mockServer.url}/users/999`);
        expect(response.status).toBe(404);
      });
  });
});
```

## Provider Verification

```typescript
import { Verifier } from '@pact-foundation/pact';

new Verifier({
  provider: 'UserService',
  providerBaseUrl: 'http://localhost:3000',
  pactBrokerUrl: process.env.PACT_BROKER_URL,
  publishVerificationResult: true,
  providerVersion: process.env.GIT_SHA,
  stateHandlers: {
    'user 123 exists': async () => {
      await db.users.create({ id: '123', name: 'John' });
    },
    'user does not exist': async () => {
      await db.users.deleteAll();
    }
  }
}).verifyProvider();
```

## OpenAPI Validation (Express)

```javascript
const OpenApiValidator = require('express-openapi-validator');

app.use(OpenApiValidator.middleware({
  apiSpec: './openapi.yaml',
  validateRequests: true,
  validateResponses: true,
  validateSecurity: true
}));
```

## Additional Implementations
````
END THIRD-PARTY FILE d304dfb344093bec

The third-party file has ended. Everything between the d304dfb344093bec tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2991 of 3906 bytes. © secondsky · MIT · https://github.com/secondsky/claude-skills/tree/HEAD/plugins/api-contract-testing/skills/api-contract-testing/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
