1 of 24

Schema-First API Design

with OpenAPI / Swagger

Yos Riady

yos.io

2 of 24

API Design process

What is OpenAPI?

Introduction

Design

Spec

Tools

Conclusion

Writing OpenAPI specs

Summary and further learning

Tooling around OpenAPI

3 of 24

You’re building an API

4 of 24

Adding a new feature to the API

  • You need to:
    • Update the server implementation to support the new feature.
    • Update all client libraries / SDKs.
    • Update the documentation.
  • All the above must be consistent with each other.
  • Also, frontend teams are blocked until your backend API is complete.

5 of 24

Is there a better way to do this?

6 of 24

Schema-First API Design

The Schema-first API design approach means writing your API definition first in one of many API Specification languages before writing any code.

7 of 24

Benefits of the Schema-First Approach

Iterate faster in teams

Generate Artifacts

Generate API Tests

You can generate mock services for clients to work with, even before backend components are ready.

Your API Specification can be used to generate functional tests for the API.

Your API Specification can be used to generate SDKs, documentation, and server scaffolds.

8 of 24

API Specification Languages

9 of 24

API Design process

What is OpenAPI?

Introduction

Design

Spec

Tools

Conclusion

Writing OpenAPI specs

Summary and further learning

Tooling around OpenAPI

10 of 24

OpenAPI / Swagger

11 of 24

What’s in an OpenAPI Specification?

  • General information about the API
  • Available paths e.g. /resources
    • Available operations on each path e.g. GET /resources/{id}
    • input & output for each operation

12 of 24

openapi: 3.0.0��info:title: Sample API� description: Optional multiline or single-line description� version: 0.1.9��paths:� /users:� get:� summary: Returns a list of users.� description: Optional extended description in CommonMark or HTML.� responses:� '200': # status code� description: A JSON array of user names� content:� application/json:� schema:� type: array� items:� type: string

13 of 24

editor.swagger.io

14 of 24

Writing an OpenAPI Specification: openapi

openapi: 3.0.0

The semantic version number of the OpenAPI Specification version that the OpenAPI document uses.

15 of 24

Writing an OpenAPI Specification: info

info:� title: Sample Pet Store App� version: 1.0.1� description: This is a sample server for a pet store.� termsOfService: http://example.com/terms/� contact:� name: API Support� url: http://www.example.com/support� email: support@example.com� license:� name: Apache 2.0� url: http://www.apache.org/licenses/LICENSE-2.0.html

The info section provides general information about the API.

16 of 24

Writing an OpenAPI Specification: paths

paths:� /products/{id}:� get: <operation>�� /orders:� post: <operation>

The paths section contains the endpoints such as /products and operations are the HTTP methods such as GET, POST, DELETE

17 of 24

Writing an OpenAPI Specification: paths

paths:� /products/{id}:� get:� operationId: getProductById� parameters:� - name: id� in: path� description: Product ID� required: true� schema:� type: integer� format: int64� responses:� '200':� content:� application/json:� schema:� $ref: '#/components/schemas/Product'

Each operation documents any parameters for the operation, the different kinds of responses, and other metadata.

18 of 24

Writing an OpenAPI Specification: components

components:� schemas: # Schema object definition� Pet:� type: object� properties:� name:� type: string� petType:� type: string� required:� - name� - petType

The components section lets you define common data structures used in your API. They can be referenced via $ref whenever a schema is required:

$ref: '#/components/schemas/Pet'

19 of 24

editor.swagger.io

20 of 24

Tooling around OpenAPI

And more!

Swagger Codegen

Swagger UI

There are tons of libraries and frameworks that support the OpenAPI ecosystem.

Generates server stubs and client libraries in over 40 languages / platforms.

Generate interactive API documentation that lets users try out API calls in the browser.

21 of 24

22 of 24

API Design process

What is OpenAPI?

Introduction

Design

Spec

Tools

Conclusion

Writing OpenAPI specs

Summary and further learning

Tooling around OpenAPI

23 of 24

Schema-First API Design

with OpenAPI / Swagger

Yos Riady

yos.io

24 of 24

Q&A