Open API

What is it?

OpenAPI is a specification for describing REST API formats.

  • Tools like Swagger provide tools for implementing that specification. These tools can be used at different stages of the API lifecycle.
    • note: Swagger is the prior name of OpenAPI.

OpenAPI 3.0 uses an extended subset of JSON Schema to describe data formats.

  • this means that some keywords are supported and some are not. See the breakdown

An OpenAPI file (json or yml) allows you to describe your entire API, including:

  • Available endpoints (/users) and operations on each endpoint (GET /users, POST /users)
  • Operation parameters Input and output for each operation
  • Authentication methods
  • Contact information, license, terms of use and other information.

Why use it?

The value of OpenAPI is that it allows APIs to describe their own structure. This allows us to:

  • Design-first users: use Swagger Codegen to generate a server stub for your API. The only thing left is to implement the server logic – and your API is ready to go live!
  • Use Swagger Codegen to generate client libraries for your API in over 40 languages.
  • Use Swagger UI to generate interactive API documentation that lets your users try out the API calls directly in the browser.
  • Use the spec to connect API-related tools to your API. For example, import the spec to SoapUI to create automated tests for your API.


openapi: 3.0.0
  title: Sample API
  description: Optional multiline or single-line description in [CommonMark]( or HTML.
  version: 0.1.9
  - url:
    description: Optional server description, e.g. Main (production) server
  - url:
    description: Optional server description, e.g. Internal staging server for testing
      summary: Returns a list of users.
      description: Optional extended description in CommonMark or HTML.
        '200':    # status code
          description: A JSON array of user names
                type: array
                  type: string



  • main docs $ref can be used to reference models from other models.

For example, imagine we had a model for invoice_items.json, and we wanted to reference an invoice.json model found in the same schema/ directory.

  // ...
  "properties": {
    "id": { "type": "string" },
    "invoice": { "$ref": "invoice.json" }
  // ...

$ref can also be used to reference a specific value within the same JSON document (JSON pointer notation).

  • ex. #/components/schemas/user



We can use ajv to take in our json schema, and generate validation functions to make sure the data coming in adheres to the schema.


Redoc takes the OpenAPI definitions (yml file) and generates HTML that can be viewed in the browser.