Design, Develop and Mock APIs with Postman (Workshop)

A presentation at POST/CON 2019 in September 2019 in San Francisco, CA, USA by Kaustav Das Modak

Slide 1

Slide 1

Design, Develop & Mock APIs with Postman PRESENTED BY Kaustav Das Modak Developer Advocate, Postman @kaustavdm

Slide 2

Slide 2

Concepts • Real-life API collaboration • API Design: What we need • Postman fundamentals What we will cover Role: Producer • Creating collections and Mocks • Using “API” and schema Role: Consumer • Using Mocks • Collaborating with producer

Slide 3

Slide 3

Part 1: Concepts

Slide 4

Slide 4

Let’s talk about APIs

Slide 5

Slide 5

Applications today are built of multiple interacting components APIs are the building blocks of modern software. Multiple services, spanning multiple teams and multiple geographical regions. Developing those services in parallel without teams blocking others is a challenge that teams face often.

Slide 6

Slide 6

When systems talk to each other, we should carefully design how they interact In a world where systems talk to each other, it is vital to carefully design how these systems will interact. It is terribly easy to get lost in the quagmire of multiple services and multiple dependencies and who does whom.

Slide 7

Slide 7

Design your APIs before you implement them In a world where systems talk to each other, it is vital to carefully design how these systems will interact. Instead of the traditional code-first approach to software development, API-first development is a software development pattern which asks you to design and scope out your APIs before you build the software.

Slide 8

Slide 8

Agree on domain boundaries and data models before you implement them Gist of API-first design Allows stakeholders to add their inputs.

Slide 9

Slide 9

Agree on domain boundaries and data models before you implement them Collaborate

Slide 10

Slide 10

Business use case Abstraction Encapsulation Agree on domain boundaries and data models before you implement them Collaborate

Slide 11

Slide 11

Business use case Abstraction Encapsulation Entities Resources Data structures Agree on domain boundaries and data models before you implement them Collaborate

Slide 12

Slide 12

Data models Code Design Implementation Business logic Publish Separating the design of the API from its implementation, the architect is constrained only by the data model and business logic. Your API can now evolve unfettered by any existing user interface or legacy engineering frameworks.

Slide 13

Slide 13

A good design should adapt to changes Caveat: Make sure you design APIs in a way that they remain flexible to future changes.

Slide 14

Slide 14

Key concepts

Slide 15

Slide 15

If you work with APIs…

Slide 16

Slide 16

What is an API? When people talk about APIs, sometimes they talk about the implementation or the code, sometimes they’re referring to the interface, and sometimes they’re referring to the hosted version of the service.

Slide 17

Slide 17

What is an API? Interface Schema OpenAPI RAML GraphQL Postman

Slide 18

Slide 18

What is an API? Interface Implementation Schema OpenAPI RAML GraphQL Postman Code Databases Repositories Microservices …

Slide 19

Slide 19

What is an API? Interface Implementation Instance Schema OpenAPI RAML GraphQL Postman Code Databases Repositories Microservices … Deployed Interface + Implementation + Servers …

Slide 20

Slide 20

What is an API? Interface Implementation Instance Schema OpenAPI RAML GraphQL Postman Code Databases Repositories Microservices … Deployed Interface + Implementation + Servers …

Slide 21

Slide 21

What is an API? Interface Implementation Instance Schema OpenAPI RAML GraphQL Postman Code Databases Repositories Microservices … Deployed Interface + Implementation + Servers …

Slide 22

Slide 22

Producer Consumer Roles Producer: Who builds and serves an API Consumer: Who uses that API for their work

Slide 23

Slide 23

Producer Consumer Requirements Roles

Slide 24

Slide 24

Resources Producer Consumer Requirements Roles

Slide 25

Slide 25

Private Partner Scopes Arrow follows more number of potential consumers Public

Slide 26

Slide 26

More potential consumers Private Partner Scopes Public

Slide 27

Slide 27

Schema Callout: How many of these have you worked with? Any ones that you know are missing from this list?

Slide 28

Slide 28

OpenAPI 2.0 | 3.0 GraphQL RAML WSDL API Blueprint Schema Postman Collections

Slide 29

Slide 29

Questions to ask Producers How do we gather requirements? How do we share API docs? How do we collect feedback? Consumers How do we give our inputs? How do we consume docs? How do we test our requirements?

Slide 30

Slide 30

Design the interface Write API schema Build a testable, executable spec Collaborate on decisions What we need for effective API design Document the interface Resource & usage descriptions Request/Response examples Collaborate on implementation API Mocks API Contracts

Slide 31

Slide 31

What is the single source of truth?

Slide 32

Slide 32

Part 2: Setting things up

Slide 33

Slide 33

GET postman.com Make sure you have the latest version of Postman app installed Create account. Signin.

Slide 34

Slide 34

What we will build today Design API for a hypothetical service to manage list of cats. As both producer and consumer. We will focus on the API interface design. Not the code for actually building the service. In the process we will use Postman’s Mock Servers to serve as a backend.

Slide 35

Slide 35

Routes: v0.1

  • GET /cats Returns list of all cats - POST /cats Add a new cat Cat schema: - id: Integer name: String breed: String age: Integer

Slide 36

Slide 36

Routes (new): v0.2

  • GET /cats/{{catId}} Find a specific cat

Slide 37

Slide 37

Part 3: Postman fundamentals A quick overview of the Postman features we will need to build the rest

Slide 38

Slide 38

Collections Group and organize your requests into meaningful collections.

Slide 39

Slide 39

Variables Foundation of dynamic values for requests. Can be manipulated programmatically.

Slide 40

Slide 40

Workspaces • Organised by service and function • Service producers and consumers share their collections in them Workspaces form the foundation of collaboration for our teams.

Slide 41

Slide 41

Pre-request Script • Written in JavaScript • Executed in a sandboxed NodeJS environment • Executed before request is sent • Modify request through variables Every single request can have tests. Foreshadowing: Just like variables, tests can also be written at collection and folder levels.

Slide 42

Slide 42

Tests • Written in JavaScript • Executed in a sandboxed NodeJS environment • Executed after response is received • Can have assertions • Quick-start snippets Every single request can have tests. Foreshadowing: Just like variables, tests can also be written at collection and folder levels.

Slide 43

Slide 43

Postman API Programmatically interact with elements in the Postman ecosystem api.getpostman.com

Slide 44

Slide 44

Blueprints • Collections created by service producers to describe an API • Includes examples of each request to document responses Collections that serve as API reference Provides request and response structure (Usually) First piece in the design phase of an API

Slide 45

Slide 45

Mock servers • Created by service producers from blueprint collections • Used by service consumers to test API contracts More on contracts later.

Slide 46

Slide 46

Comments • To make contextual comments and tag other team members in collections, in the app, and in the browser • Used to negotiate API design among stakeholders One of the newer features in Postman Comments in collections make discussions contextual. Postman is invested in improving this feature. Again, dogfooding here helps us understand the scopes for improvement.

Slide 47

Slide 47

Questions? (and a quick break)

Slide 48

Slide 48

Part 4: Role - API Producer

Slide 49

Slide 49

• Create blueprint collection • • Build a blueprint collection • Add requests • Add documentation • Add examples • Success cases • Failure cases Share collection in Workspace Comments on requests

Slide 50

Slide 50

Create Mock Server • • • What is a Mock Server? (Recap) Create a mock server from Blueprint collection Execute Blueprint collection against Mock

Slide 51

Slide 51

Create “API” and schema • • Create a new “API” Add OpenAPI 3.0 YAML schema • From: bit.ly/postman-api-yaml • File: api-v0.1.yaml

Slide 52

Slide 52

Use versioning • • Edit API version tag to v0.1 Add blueprint collection and mock to API v0.1

Slide 53

Slide 53

Questions? (and another quick break)

Slide 54

Slide 54

Part 5: Role - API Consumer

Slide 55

Slide 55

• Using Mocks as a consumer • • • • Build contract collection based on blueprint collection Send requests to Mock endpoint Save and document requests Add tests to assert on response Switch base URL using environments

Slide 56

Slide 56

Part 6: Work with versions

Slide 57

Slide 57

• • Create v0.2 • • • Create v0.2 for the API Update OpenAPI schema • From: bit.ly/postman-api-yaml • File: api-v0.2.yaml Update blueprint Update contract Tag collections with new version

Slide 58

Slide 58

Questions? (and we’re done!)

Slide 59

Slide 59

Thank you! @postmanclient @kaustavdm betterpractices.dev