Code Snippet Catalog

All code blocks from the documentation - terminals, configs, schemas, workflows

CLI Reference

Core command outputs from reference/cli.mdx

1. contractual init reference/cli.mdx

Project initialization - scans for specs and creates config

terminal
$ contractual init

Scanning for contract specs...

  Detected specs
    openapi      api/openapi.yaml
    json-schema  schemas/user.schema.json

Created contractual.yaml
Created .contractual/
Wrote snapshots for 2 contract(s)
2. contractual lint reference/cli.mdx

Spec validation - shows errors and passes

terminal
$ contractual lint

Linting contracts...

  payments-api  (openapi)
    [error]  oas3-api-servers  No servers defined  api/openapi.yaml:1
    user-schema  (json-schema)  No violations

1 contract(s) passed, 1 contract(s) have errors.
3. contractual breaking reference/cli.mdx

Breaking change detection - compares against snapshots

terminal
$ contractual breaking

Comparing contracts against last snapshot...

  payments-api  (openapi)
    [breaking]      DELETE /v1/charges/{id}  Endpoint removed
    [non-breaking]  GET /v1/charges          Response field `metadata` added

  user-schema  (json-schema)
    No changes detected.

1 breaking change(s) found.
4. contractual changeset reference/cli.mdx

Interactive changeset creation with prompts

terminal
$ contractual changeset

Detected changes in payments-api (openapi):
    [breaking]      DELETE /v1/charges/{id}  Endpoint removed

? Bump type for payments-api: (major)
? Summary: Remove deprecated endpoint

Wrote .contractual/changesets/fuzzy-lion-dances.md
5. contractual version reference/cli.mdx

Consume changesets and bump versions

terminal
$ contractual version

Reading changesets...
  fuzzy-lion-dances.md   →  payments-api  major

Resolved bump types:
  payments-api   1.4.2  →  2.0.0  (major)

Snapshotting specs...
    .contractual/snapshots/payments-api@2.0.0.yaml

Updated .contractual/versions.json
Updated CHANGELOG.md
Deleted 1 changeset file(s)

Done. Commit these changes to complete the release.
6. contractual status reference/cli.mdx

Show current versions and pending changesets

terminal
$ contractual status

Contract Versions
─────────────────────────────────────────────────────
  payments-api   v1.4.2  →  v2.0.0  (major pending)
  user-schema    v0.9.1    up to date

Pending changesets (1):
  fuzzy-lion-dances.md
    contracts: payments-api  bump: major

Run `contractual version` to apply these changesets.

Linting

Lint command outputs from linting/usage.mdx

7. Lint all contracts linting/usage.mdx

Full lint run across all registered contracts

terminal
$ contractual lint

Linting contracts...

  payments-api  (openapi)
    [error]  oas3-api-servers  No servers defined  api/openapi.yaml:1
    user-schema  (json-schema)  No violations

1 contract(s) passed, 1 contract(s) have errors.
8. Lint single contract linting/usage.mdx

Target a specific contract with --contract flag

terminal
$ contractual lint --contract payments-api

Linting payments-api...

    [error]  oas3-api-servers  No servers defined  api/openapi.yaml:1
    [warn]   operation-tags    Operation "GET /charges" has no tags  api/openapi.yaml:42

2 violation(s) found (1 error, 1 warning).

Breaking Change Detection

Breaking command outputs from breaking/usage.mdx

9. Check all contracts breaking/usage.mdx

Detect breaking changes across all contracts

terminal
$ contractual breaking

Comparing contracts against last snapshot...

  payments-api  (openapi)
    [breaking]      DELETE /v1/charges/{id}  Endpoint removed
    [breaking]      GET /v1/charges          Response field `currency` removed
    [non-breaking]  GET /v1/charges          Response field `metadata` added

  user-schema  (json-schema)
    No changes detected.

2 breaking change(s) found across 1 contract(s).
10. Check single contract breaking/usage.mdx

Target specific contract for breaking check

terminal
$ contractual breaking --contract payments-api

Comparing payments-api against last snapshot...

    [breaking]      DELETE /v1/charges/{id}  Endpoint removed
    [breaking]      GET /v1/charges          Response field `currency` removed
    [non-breaking]  GET /v1/charges          Response field `metadata` added

2 breaking change(s) found.
11. Compare against specific base breaking/usage.mdx

Use --base to compare against a tagged version

terminal
$ contractual breaking --base v1.2.0

Comparing contracts against snapshot v1.2.0...

  payments-api  (openapi)
    [breaking]  GET /v1/charges  Response field `currency` removed

1 breaking change(s) found.

Versioning

Changeset and version outputs from versioning/usage.mdx

12. Interactive changeset versioning/usage.mdx

Create changeset with interactive prompts

terminal
$ contractual changeset

Detected changes in payments-api (openapi):
    [breaking]      DELETE /v1/charges/{id}  Endpoint removed
    [non-breaking]  GET /v1/charges          Response field `metadata` added

? Bump type for payments-api: (major)
? Summary: Remove deprecated DELETE /v1/charges/{id} endpoint and add metadata field

Wrote .contractual/changesets/fuzzy-lion-dances.md
13. Non-interactive changeset (CI) versioning/usage.mdx

Auto-generate changeset without prompts for CI

terminal
$ contractual changeset --no-interactive

Auto-generating changeset for detected changes...

  payments-api: major bump detected (breaking changes found)

Wrote .contractual/changesets/silver-hawk-runs.md
14. Empty changeset versioning/usage.mdx

Create changelog-only changeset with no spec changes

terminal
$ contractual changeset --empty

? Bump type: (patch)
? Summary: Update changelog with maintainer notes

Wrote .contractual/changesets/brave-wolf-leaps.md
15. Apply changesets (version) versioning/usage.mdx

Consume all pending changesets and bump versions

terminal
$ contractual version

Reading changesets...
  fuzzy-lion-dances.md   →  payments-api  major
  silver-hawk-runs.md    →  user-schema   minor

Resolved bump types:
  payments-api   1.4.2  →  2.0.0  (major)
  user-schema    0.9.1  →  0.10.0 (minor)

Snapshotting specs...
    .contractual/snapshots/payments-api@2.0.0.yaml
    .contractual/snapshots/user-schema@0.10.0.json

Updated .contractual/versions.json
Updated CHANGELOG.md
Deleted 2 changeset file(s)

Done. Commit these changes to complete the release.
16. Dry run preview versioning/usage.mdx

Preview version changes without writing files

terminal
$ contractual version --dry-run

[dry-run] Reading changesets...
  fuzzy-lion-dances.md   →  payments-api  major
  silver-hawk-runs.md    →  user-schema   minor

[dry-run] Resolved bump types:
  payments-api   1.4.2  →  2.0.0  (major)
  user-schema    0.9.1  →  0.10.0 (minor)

[dry-run] No files were written.

Config Files (YAML)

Configuration snippets from reference/configuration.mdx

contractual.yaml - Basic reference/configuration.mdx

Minimal config with two contracts registered

contractual.yaml
# yaml-language-server: $schema=https://contractual.dev/schema.json

contracts:
  - name: orders-api
    type: openapi
    path: ./specs/orders.openapi.yaml

  - name: order-schema
    type: json-schema
    path: ./schemas/order.schema.json
contractual.yaml - Full reference/configuration.mdx

Full config with custom linter, differ, and code generator

contractual.yaml
contracts:
  - name: orders-api
    type: openapi
    path: ./specs/orders.openapi.yaml
    lint: "spectral lint {spec} --format json"
    breaking: "oasdiff breaking {old} {new} --format json"
    outputs:
      - name: typescript-client
        command: "orval --input {spec} --output ./src/generated"

publish:
  commit: true
  tag: true
  release: true
Disable lint/breaking reference/configuration.mdx

Opt out of specific checks per contract

contractual.yaml
contracts:
  - name: internal-config
    type: json-schema
    path: ./config/internal.schema.json
    lint: false      # Skip linting
    breaking: false  # Skip breaking detection

Schema Files (JSON)

JSON Schema examples from getting-started/quickstart.mdx and recipes/

order.schema.json getting-started/quickstart.mdx

Sample order schema used in quickstart tutorial

order.schema.json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/order.schema.json",
  "title": "Order",
  "type": "object",
  "required": ["id", "status", "amount"],
  "properties": {
    "id": { "type": "string" },
    "status": { "type": "string" },
    "amount": { "type": "number" }
  }
}
order-created.schema.json recipes/json-schema-events.mdx

Event schema for Kafka/SNS topic

order-created.schema.json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "OrderCreated",
  "description": "Published when an order is placed.",
  "type": "object",
  "required": ["orderId", "customerId", "totalAmount"],
  "additionalProperties": false,
  "properties": {
    "orderId": { "type": "string" },
    "customerId": { "type": "string" },
    "totalAmount": { "type": "number" }
  }
}
VS Code settings.json reference/configuration.mdx

Enable YAML schema validation in VS Code

.vscode/settings.json
{
  "yaml.schemas": {
    "https://contractual.dev/schema.json": "contractual.yaml"
  }
}

Changeset Files (Markdown)

Changeset format from versioning/changeset-format.mdx

fuzzy-lion-dances.md versioning/overview.mdx

Standard changeset with bump types and summary

.contractual/changesets/fuzzy-lion-dances.md
---
"orders-api": major
"order-schema": minor
---

## orders-api

Removed `internal_trace_id` field from Order response.

## order-schema

Added optional `shipping_address` field.
Override classification versioning/usage.mdx

Manually override auto-detected bump type

.contractual/changesets/brave-wolf-leaps.md
---
"orders-api": patch  # was: major; field removed but unused
---

Removed the internal `internal_trace_id` field from the
Order response. This field was never documented and no
external consumers reference it.

GitHub Action Workflows

CI/CD workflow snippets from github-action/

PR Check workflow github-action/setup.mdx

Run lint + breaking checks on pull requests

.github/workflows/contracts.yml
name: Contract Governance

on:
  pull_request:
    paths:
      - 'specs/**'
      - 'schemas/**'

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: contractual-dev/action@v1
        with:
          mode: pr-check
          github-token: ${{ secrets.GITHUB_TOKEN }}
Release workflow github-action/release.mdx

Auto-version on merge to main

.github/workflows/release.yml
name: Release Contracts

on:
  push:
    branches: [main]

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: contractual-dev/action@v1
        with:
          mode: release
          github-token: ${{ secrets.GITHUB_TOKEN }}