Swagger allows teams to collaborate on API specifications by providing a central place to review and share them.

Let’s see Swagger in action with a simple OpenAPI 3.0 specification for a basic "Todo" API.

openapi: 3.0.0
info:
  title: Todo API
  version: 1.0.0
  description: A simple API to manage todo items.
servers:
  - url: http://localhost:3000/api/v1
paths:
  /todos:
    get:
      summary: Get a list of all todos
      responses:
        '200':
          description: A list of todo items.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Todo'
    post:
      summary: Create a new todo item
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Todo'
      responses:
        '201':
          description: Todo item created successfully.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Todo'
  /todos/{id}:
    get:
      summary: Get a specific todo item by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
          description: The ID of the todo item to retrieve.
      responses:
        '200':
          description: A single todo item.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Todo'
    put:
      summary: Update a specific todo item by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
          description: The ID of the todo item to update.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Todo'
      responses:
        '200':
          description: Todo item updated successfully.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Todo'
    delete:
      summary: Delete a specific todo item by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
          description: The ID of the todo item to delete.
      responses:
        '204':
          description: Todo item deleted successfully.
components:
  schemas:
    Todo:
      type: object
      properties:
        id:
          type: integer
          readOnly: true
          description: Unique identifier for the todo.
        title:
          type: string
          description: The task to be done.
        completed:
          type: boolean
          default: false
          description: Whether the todo is completed.

This YAML file defines the structure and operations of our Todo API. It specifies endpoints like /todos for listing and creating, and /todos/{id} for retrieving, updating, and deleting specific items. It also defines a Todo schema detailing the properties an individual todo item should have (id, title, completed).

The core problem Swagger (or more accurately, OpenAPI) solves is the ambiguity and manual communication overhead inherent in API development. Before standardized specifications like OpenAPI, developers would often rely on README files, inline comments, or verbal agreements to describe API behavior. This led to:

  • Drift: The actual API implementation would diverge from its documentation.
  • Misunderstandings: Different team members interpreting the same description in different ways.
  • Slow Onboarding: New developers spending significant time deciphering undocumented or poorly documented APIs.
  • Tooling Gaps: Lack of standardized formats made it hard for automated tools (like client generators or documentation UIs) to interact with the API.

OpenAPI addresses this by providing a machine-readable, language-agnostic description of an API. This description acts as a single source of truth. Teams can:

  1. Design First: Write the OpenAPI spec before writing the server code. This allows for API design reviews and discussions without needing a running implementation.
  2. Generate Code: Use tools like Swagger Codegen or OpenAPI Generator to automatically create server stubs (in various languages) and client SDKs directly from the spec.
  3. Auto-Document: Tools like Swagger UI or Redoc can render the OpenAPI spec into interactive, user-friendly documentation that’s always in sync with the spec.
  4. Test: Use the spec to generate test cases or validate requests and responses.
  5. Collaborate: Store the spec in a version control system (like Git) and use pull requests for reviews and changes, just like any other code.

Here’s how the collaboration typically flows:

  • Initial Draft: A developer or architect writes the initial OpenAPI specification.
  • Version Control: The .yaml or .json file is committed to a Git repository.
  • Pull Request: A feature or change to the API is proposed by creating a pull request with modifications to the spec.
  • Review: Other team members (developers, QA, product managers) review the spec for clarity, correctness, and adherence to API design principles. They can comment directly on the lines of code (the spec itself).
  • Feedback Loop: Discussions happen on the pull request, leading to revisions of the spec.
  • Merge: Once approved, the spec is merged into the main branch.
  • Implementation/Generation: The merged spec then triggers automated workflows to regenerate server stubs or client libraries, or serves as the definitive guide for manual implementation.

The real power of Swagger and OpenAPI lies not just in documenting an existing API, but in using the specification as the blueprint for building APIs. This "design-first" approach ensures that the API meets requirements and is well-understood by all stakeholders before any significant coding effort is expended.

A subtle but powerful aspect of OpenAPI is its ability to define "readOnly" and "writeOnly" properties within schemas. For example, in our Todo schema, id is marked readOnly: true. This tells consumers that the server will provide an id when creating or retrieving a todo, but clients should not send an id when creating a new todo item. This distinction is crucial for preventing common errors where clients might try to assign IDs or overwrite server-generated ones, and it’s automatically reflected in generated client code and documentation.

The next step in managing API collaboration is often implementing schema validation against the OpenAPI specification.

Want structured learning?

Take the full Swagger course →