Examples of Using OpenAPI for Microservices Documentation

Explore practical examples of using OpenAPI to document microservices effectively.
By Jamie

Introduction to OpenAPI for Microservices Documentation

OpenAPI, formerly known as Swagger, is a specification for defining APIs in a standard format. It is especially useful in microservices architecture, where multiple services need to communicate efficiently. Documenting APIs using OpenAPI allows developers to create a shared understanding of how services interact, making it easier to maintain, integrate, and scale applications. In this article, we will explore three diverse examples of using OpenAPI for documenting microservices, showcasing different contexts and use cases.

Example 1: E-Commerce Order Service

Context

In an e-commerce application, a microservice is responsible for managing customer orders. Documenting this service with OpenAPI helps front-end developers and third-party integrators understand how to interact with the order management system.

openapi: 3.0.0
info:
  title: Order Service
  description: API for managing customer orders in an e-commerce application.
  version: 1.0.0
paths:
  /orders:
    get:
      summary: Retrieve a list of orders
      responses:
        '200':
          description: A list of orders
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    orderId:
                      type: string
                    customerId:
                      type: string
                    totalAmount:
                      type: number
    post:
      summary: Create a new order
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                customerId:
                  type: string
                items:
                  type: array
                  items:
                    type: object
                    properties:
                      productId:
                        type: string
                      quantity:
                        type: integer
      responses:
        '201':
          description: Order created successfully

Notes

  • This example includes endpoints for retrieving and creating orders.
  • It can be extended to include additional operations, such as updating or deleting orders.

Example 2: User Authentication Microservice

Context

A user authentication microservice is crucial for verifying users and providing secure access to other services. OpenAPI documentation can clarify the authentication processes and required parameters.

openapi: 3.0.0
info:
  title: User Authentication Service
  description: API for authenticating users in a microservices architecture.
  version: 1.0.0
paths:
  /auth/login:
    post:
      summary: User login
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                username:
                  type: string
                password:
                  type: string
      responses:
        '200':
          description: Successful login
          content:
            application/json:
              schema:
                type: object
                properties:
                  token:
                    type: string
        '401':
          description: Invalid credentials
  /auth/register:
    post:
      summary: User registration
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                username:
                  type: string
                password:
                  type: string
      responses:
        '201':
          description: User registered successfully
        '400':
          description: Bad request

Notes

  • This example demonstrates endpoints for user login and registration.
  • Additional endpoints for password recovery and user profile management can be added.

Example 3: Inventory Management Service

Context

In a microservices architecture for a retail application, an inventory management service tracks product availability. OpenAPI helps developers understand how to access inventory data and update stock levels.

openapi: 3.0.0
info:
  title: Inventory Service
  description: API for managing product inventory in a retail application.
  version: 1.0.0
paths:
  /inventory/{productId}:
    get:
      summary: Get inventory details for a specific product
      parameters:

        - name: productId
          in: path
          required: true
          description: The ID of the product
          schema:
            type: string
      responses:
        '200':
          description: Inventory details retrieved successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  productId:
                    type: string
                  quantity:
                    type: integer
                  location:
                    type: string
    put:
      summary: Update inventory for a specific product
      parameters:

        - name: productId
          in: path
          required: true
          description: The ID of the product
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                quantity:
                  type: integer
                location:
                  type: string
      responses:
        '200':
          description: Inventory updated successfully
        '404':
          description: Product not found

Notes

  • This example outlines endpoints for retrieving and updating inventory details.
  • It can be expanded to include batch updates or historical inventory data retrieval.