API OpenAPI Design
CONTRACT-FIRST OPENAPI 3.1 DESIGN TREATS THE SPECIFICATION AS THE SINGLE SOURCE OF TRUTH — SCHEMAS DEFINED ONCE IN COMPONENTS AND REFERENCED EVERYWHERE, DISCRIMINATORS THAT MAKE POLYMORPHISM EXPLICIT, AND OPERATION IDS THAT DRIVE CONSISTENT CODE GENERATION ACROSS EVERY CLIENT LANGUAGE — SO THE CONTRACT IS NEVER AN AFTERTHOUGHT BOLTED ONTO A RUNNING SERVER.
When to Use
- Starting a new API and choosing whether to write the spec first or derive it from code
- Designing schemas that appear in multiple request and response bodies and need a single canonical definition
- Modeling a polymorphic payload (e.g., a
PaymentMethodthat is either aCard,BankAccount, orWallet) without duplicating fields across every variant - Setting
operationIdvalues that code generators and documentation tools use to name methods and pages - Adding async event streams (webhooks, Kafka topics) to an API that already has REST endpoints and needs both documented in one place
- Integrating spectral linting or a code-generation pipeline into CI so the spec is validated and clients are regenerated on every merge
- Writing the API design review checklist for a platform team that owns multiple services
Instructions
Key Concepts
Contract-first vs. code-first — Contract-first means you write the OpenAPI YAML before writing any server code. The spec is committed to version control, reviewed like code, and used to generate server stubs and client SDKs. Code-first derives the spec from annotations or reflection at runtime. Contract-first wins when multiple teams consume the API (they can build clients before the server ships), when you need breaking-change review in pull requests, and when you want deterministic
operationIdvalues. Code-first is acceptable for internal services with a single consumer. Stripe, Twilio, and GitHub all publish hand-authored contract-first specs.Schema reuse with
$refandcomponents— Every named type belongs incomponents/schemasand is referenced via$ref: '#/components/schemas/TypeName'everywhere it is used. Never inline a schema that appears in more than one location. Benefits: a rename or field addition is made in one place; validators and code generators produce a single class per type; documentation renders one canonical schema page. Group related components:components/schemasfor data types,components/parametersfor reusable query/path/header parameters,components/responsesfor shared response envelopes (e.g.,404NotFound,ValidationError), andcomponents/requestBodiesfor shared request shapes.Discriminator for polymorphism — When a field can be one of several object types (a union), use
oneOfwith adiscriminatorblock. Thediscriminator.propertyNamenames the field consumers read to determine the concrete type;discriminator.mappingmaps each value to a$ref. Without a discriminator, code generators produce untypedanyOfunions that require manual casting. Example: aPaymentMethodwithtype: card | bank_account | walletusesdiscriminator: { propertyName: type, mapping: { card: '#/components/schemas/Card', bank_account: '#/components/schemas/BankAccount', wallet: '#/components/schemas/Wallet' } }. Every concrete schema must include the discriminator property as a required field.operationIdnaming conventions — Every operation must have a uniqueoperationIdinverb-nounorverb_nounformat that reads as a method name in code. Use the format{action}{Resource}in PascalCase for code generators:CreatePayment,ListInvoices,GetCustomer,DeleteWebhook,UpdateSubscription. Avoid generic names likegetAllorpost1. TheoperationIdbecomes the method name in generated SDKs, the anchor in documentation, and the identifier in test suites. A consistent convention across all operations in a spec makes generated clients feel idiomatic.AsyncAPI for event-driven APIs — AsyncAPI 2.x / 3.x is the OpenAPI equivalent for message-based systems (Kafka, AMQP, WebSocket, webhooks). Where OpenAPI describes request/response channels, AsyncAPI describes publish/subscribe channels: the
channelsblock maps topic names topublishandsubscribeoperations, each with amessageschema defined incomponents/messages. A platform API that exposes both REST endpoints and Kafka events should maintain two specs: one OpenAPI and one AsyncAPI, with shared schema$refs pointing to a commonschemas/directory so data types are not duplicated between them.Code generation integration — Use
openapi-generator-clioroapi-codegen(Go) in CI to regenerate client SDKs from the spec on every merge to the main branch. The generated output is committed to the repository; a diff in CI fails the build if the spec changed but the generated code was not regenerated. This guarantees that client libraries are always in sync with the spec. Configure a.openapi-generator-ignorefile to protect hand-written files (custom auth layers, retry wrappers) from being overwritten by the generator.
Worked Example
Stripe-style Payment Intent API — contract-first OpenAPI 3.1
openapi: '3.1.0'
info:
title: Payments API
version: '2024-04-10'
paths:
/payment_intents:
post:
operationId: CreatePaymentIntent
summary: Create a PaymentIntent
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreatePaymentIntentRequest'
responses:
'201':
description: PaymentIntent created
content:
application/json:
schema:
$ref: '#/components/schemas/PaymentIntent'
'422':
$ref: '#/components/responses/ValidationError'
/payment_intents/{id}:
get:
operationId: GetPaymentIntent
parameters:
- $ref: '#/components/parameters/ResourceId'
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/PaymentIntent'
'404':
$ref: '#/components/responses/NotFound'
components:
parameters:
ResourceId:
name: id
in: path
required: true
schema:
type: string
schemas:
CreatePaymentIntentRequest:
type: object
required: [amount, currency, payment_method]
properties:
amount:
type: integer
description: Amount in smallest currency unit (cents)
example: 1099
currency:
type: string
example: usd
payment_method:
$ref: '#/components/schemas/PaymentMethod'
PaymentMethod:
oneOf:
- $ref: '#/components/schemas/CardPaymentMethod'
- $ref: '#/components/schemas/BankAccountPaymentMethod'
discriminator:
propertyName: type
mapping:
card: '#/components/schemas/CardPaymentMethod'
bank_account: '#/components/schemas/BankAccountPaymentMethod'
CardPaymentMethod:
type: object
required: [type, number, exp_month, exp_year]
properties:
type:
type: string
enum: [card]
number:
type: string
exp_month:
type: integer
exp_year:
type: integer
BankAccountPaymentMethod:
type: object
required: [type, routing_number, account_number]
properties:
type:
type: string
enum: [bank_account]
routing_number:
type: string
account_number:
type: string
PaymentIntent:
type: object
required: [id, amount, currency, status, created]
properties:
id:
type: string
example: pi_3NqXxx
amount:
type: integer
currency:
type: string
status:
type: string
enum: [requires_payment_method, requires_confirmation, processing, succeeded, canceled]
created:
type: string
format: date-time
responses:
ValidationError:
description: Validation failed
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
NotFound:
description: Resource not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
Error:
type: object
required: [code, message]
properties:
code:
type: string
message:
type: string
Anti-Patterns
Inlining schemas instead of using
$ref. WhenCreatePaymentRequestandUpdatePaymentRequestboth inline the same address object, a field addition requires two edits. The types diverge silently. Every named type used in more than one location belongs incomponents/schemas.Missing or generic
operationIdvalues. AnoperationIdofpost_paymentorget1generates a method namedpost_payment()in every SDK. Consumers cannot discover intent from the name. UseCreatePaymentandGetPayment— the generator produces a method whose name reads like a sentence.Using
anyOfwithout a discriminator for union types.anyOfwithout a discriminator property produces an untyped union in generated code. Consumers must manually inspect the payload to determine the concrete type. Add adiscriminator.propertyNameanddiscriminator.mappingto everyoneOf/anyOfthat represents a tagged union.Not versioning the spec file. Placing the OpenAPI spec in a repo without a version field (or keeping
version: 0.0.1forever) means consumers cannot detect when breaking changes were introduced. Theinfo.versionshould match the API release version and be updated on every breaking change.Skipping linting in CI. A spec that is never linted accumulates style violations, missing descriptions, and undocumented error responses. Add a spectral or vacuum lint step to CI that enforces the team's ruleset; block merges on lint errors, not just YAML parse failures.
Details
AsyncAPI Side-by-Side with OpenAPI
A payments platform that exposes REST endpoints AND publishes payment.succeeded events to Kafka maintains two specs:
specs/
openapi.yaml ← REST paths, operations, HTTP schemas
asyncapi.yaml ← Kafka topics, message schemas
schemas/
PaymentIntent.yaml ← shared via $ref from both specs
Error.yaml
Both specs $ref into schemas/ for shared types. The AsyncAPI channels block:
channels:
payment.succeeded:
publish:
message:
$ref: '#/components/messages/PaymentSucceeded'
components:
messages:
PaymentSucceeded:
payload:
$ref: '../../schemas/PaymentIntent.yaml'
This means the PaymentIntent schema is defined exactly once. REST consumers and event consumers share the same generated type.
Real-World Case Study: GitHub's OpenAPI Spec
GitHub maintains a public contract-first OpenAPI spec for their REST API (github/rest-api-description). The spec has over 900 operations with consistent operationId values in the format {category}/{action} (e.g., repos/create-for-authenticated-user). Key outcomes:
- Third-party SDK generators (octokit, PyGithub) consume the spec directly — GitHub does not maintain those clients
- The spec is the authoritative documentation source; the developer portal renders from it
- Breaking changes are detectable via diff against the previous version in CI
GitHub's approach demonstrated that a large, complex API (900+ operations) can be fully described contract-first with consistent conventions, enabling an ecosystem of generated tooling without GitHub maintaining any of it directly.
Source
- OpenAPI Specification 3.1.0
- AsyncAPI Specification 2.6
- Stripe OpenAPI Spec (github.com/stripe/openapi)
- GitHub REST API Description (github.com/github/rest-api-description)
- OpenAPI Generator Documentation
Process
- Define
components/schemasfor all domain types before writing anypaths— the schema vocabulary is established first and paths reference it. - Write each path operation with a unique
operationIdin{Verb}{Resource}PascalCase; addsummaryand at least one error response per operation. - Model all union types with
oneOf+discriminator; verify every concrete schema includes the discriminator property as required. - Run spectral or vacuum lint against the spec in CI; treat lint errors as build failures.
- Run
harness validateto confirm skill files are well-formed and related skills are correctly cross-referenced.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
- related_skills: api-contract-testing, api-error-contracts, api-backward-compatibility, api-validation-errors
Success Criteria
- All named types appear in
components/schemasand are referenced via$ref; no inline schema is duplicated across two or more locations. - Every operation has a unique
operationIdin{Verb}{Resource}PascalCase format that produces a readable method name in generated SDKs. - All polymorphic fields use
oneOfwith adiscriminator.propertyNameanddiscriminator.mapping; every concrete schema includes the discriminator field as required. - A spectral or vacuum lint step runs in CI and blocks merges on ruleset violations.
- Shared domain types referenced by both REST and async event specs live in a common
schemas/directory, not duplicated in each spec.