Slide 1

Slide 1 text

Pragmatic Web API Design Samnang Chhun | @samnangchhun

Slide 2

Slide 2 text

API Design Table of Contents Basics of HTTP 1 3 JSON:API 4 Discussion 2

Slide 3

Slide 3 text

Basics of HTTP HTTP is the foundation of data communication for the World Wide Web

Slide 4

Slide 4 text

HTTP Application Layer Protocol ✽ Client - Server Model ✽ Decoupled ✽ Stateless ✽ Extensible ✽

Slide 5

Slide 5 text

HTTP Request Message URI ✽ Method ✽ Headers ✽ Message Body (optional) ✽

Slide 6

Slide 6 text

HTTP Response Message Response Code ✽ Headers ✽ Message Body (optional) ✽

Slide 7

Slide 7 text

API Design

Slide 8

Slide 8 text

Modeling APIs Your API Is a Contract

Slide 9

Slide 9 text

Modeling APIs Who Will Be Using Your API?

Slide 10

Slide 10 text

Modeling APIs What’s the Purpose of Your API?

Slide 11

Slide 11 text

Design guidelines Preventing migration of business logic to API consumer

Slide 12

Slide 12 text

Design guidelines An API is only as good as its documentation

Slide 13

Slide 13 text

Design guidelines Your resources are not your database tables

Slide 14

Slide 14 text

Design guidelines Use HTTP Status Codes

Slide 15

Slide 15 text

Design guidelines Use HTTP Methods

Slide 16

Slide 16 text

Design guidelines Define a consumable error payload

Slide 17

Slide 17 text

Design guidelines Adopt a hypermedia type for your API HAL ✽ Collection+JSON ✽ JSON:API SIREN ✽ ✽ Others ✽

Slide 18

Slide 18 text

A SPECIFICATION FOR BUILDING APIS IN JSON

Slide 19

Slide 19 text

Why JSON:API? The spec provides consistent rules for how requests and responses are formatted. ✽ ● It reduces both the number of requests and the amount of data transmitted between clients and servers. ✽ ● Following shared conventions, you can increase productivity, take advantage of generalized tooling, and focus on what matters: your application. ✽ ● Strong ecosystem in developer community. ✽

Slide 20

Slide 20 text

JSON:API It’s a Standard ✅

Slide 21

Slide 21 text

A document MAY contain any of these top-level members: Top Level Document Structure data: the document’s “primary data” ✽ errors: an array of error objects ✽ meta: a meta object that contains non-standard meta-information. ✽ jsonapi: an object describing the server’s implementation. ✽ links: a links object related to the primary data. ✽ included: an array of resource objects that are related to the primary data and/or each other (“included resources”). ✽ A document MUST contain at least one of the following top-level members:

Slide 22

Slide 22 text

A resource object MUST contain at least the following top-level members: Resource Objects id: the document’s “primary data” ✽ type: an array of error objects ✽ In addition, a resource object MAY contain any of these top-level members: attributes: an attributes object representing some of the resource’s data ✽ relationships: a relationships object describing relationships between the resource and other JSON:API resources. ✽ meta: a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship. links: a links object containing links related to the resource. ✽ ✽

Slide 23

Slide 23 text

Resource Objects (Example)

Slide 24

Slide 24 text

Compound Documents

Slide 25

Slide 25 text

Sparse Fieldsets A client MAY request that an endpoint return only specific fields in the response on a per-type basis by including a field[TYPE] parameter.

Slide 26

Slide 26 text

Sorting A server MAY choose to support requests to sort resource collections according to one or more criteria (“sort fields”).

Slide 27

Slide 27 text

Pagination A server MAY choose to limit the number of resources returned in a response to a subset (“page”) of the whole set available. The following keys MUST be used for pagination links: first: the first page of data ✽ last: the last page of data ✽ next: the next page of data prev: the previous page of data ✽ ✽

Slide 28

Slide 28 text

Filtering The filter query parameter is reserved for filtering data. Servers and clients SHOULD use this key for filtering operations.

Slide 29

Slide 29 text

Error Object Error objects MUST be returned as an array keyed by errors in the top level of a JSON:API document.

Slide 30

Slide 30 text

Asynchronous Processing Consider a situation when you need to create a resource and the operation takes a long time to complete.

Slide 31

Slide 31 text

Adopted Implementations

Slide 32

Slide 32 text

Thank You

Slide 33

Slide 33 text

Discussions

Slide 34

Slide 34 text

The Clean Architecture

Slide 35

Slide 35 text

Request / Response