Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Marco Otte-Witte @marcoow

Slide 3

Slide 3 text

http://simplabs.com @simplabs

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

http://jsonapi.org

Slide 6

Slide 6 text

A specification for building APIs in JSON

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

“ “ “ “

Slide 9

Slide 9 text

“ “ “ “

Slide 10

Slide 10 text

Why is this even needed?

Slide 11

Slide 11 text

https://twitter.com/thomasfuchs/status/604323589979049984

Slide 12

Slide 12 text

everybody is using RESTful JSON APIs already

Slide 13

Slide 13 text

…but they are all different

Slide 14

Slide 14 text

GET /repos/sinatra/sinatra { "id": 1, "name": "sinatra", … }

Slide 15

Slide 15 text

GET /repos/sinatra/sinatra { "repo": { "id": 82, "name": "sinatra/sinatra", … } }

Slide 16

Slide 16 text

GET /1.1/users/show.json? screen_name=marcoow { "id": 1, "name": "marcoow", … }

Slide 17

Slide 17 text

GET /users/marcoow { "id": 1, "login": "marcoow", … }

Slide 18

Slide 18 text

GET /repos/simplabs/rails_api_auth { "id": 1,
 "name": "rails_api_auth", "owner": { "id": 1,
 "name": "simplabs", … } … }

Slide 19

Slide 19 text

GET /repos/:repo_id/branches/master { "branches": { { "id": 1, "repository_id": 891, … } } }

Slide 20

Slide 20 text

https://www.broxap.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/i/bikeshed_bxmwmu2.jpg_1.jpg.jpg

Slide 21

Slide 21 text

JSON API is your anti bikeshedding weapon

Slide 22

Slide 22 text

some History

Slide 23

Slide 23 text

it started with a lengthy discussion between Yehuda Katz and Steve Klabnik at RailsConf 2013

Slide 24

Slide 24 text

Yehuda wrote the first draft

Slide 25

Slide 25 text

1.0 released on May 29th 2015

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

the Goals

Slide 28

Slide 28 text

define a generic media type that works across a broad set of use cases

Slide 29

Slide 29 text

make the format similar to existing server-side framework practices

Slide 30

Slide 30 text

having a human readable format that is also easy to debug

Slide 31

Slide 31 text

ensuring ease of implementation both on the server as well as on the client side

Slide 32

Slide 32 text

the Format

Slide 33

Slide 33 text

Media Type application/vnd.api+json http://www.iana.org/assignments/media-types/application/vnd.api+json

Slide 34

Slide 34 text

Resource Objects represent individual resources

Slide 35

Slide 35 text

GET /articles/1 { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" } } }

Slide 36

Slide 36 text

GET /articles { "data": [ { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" } }, { "type": "articles", "id": "2", "attributes": { "title": "Rails is Omakase" } } ] }

Slide 37

Slide 37 text

GET /articles/1 { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" }, "relationships": { "author": { "data": { "type": "people", "id": "1" } } } } }

Slide 38

Slide 38 text

Hypermedia is part of the spec but opt-in

Slide 39

Slide 39 text

GET /articles/1 { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" }, "relationships": { "author": { "links": { "self": "/articles/1/relationships/author", "related": "/articles/1/author" } } } } }

Slide 40

Slide 40 text

Inclusion of related resources is a way of reducing requests

Slide 41

Slide 41 text

GET /articles/1 { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" }, "relationships": { "author": { "data": { "type": "people", "id": "1" } } } }, "included": [{ "type": "people", "id": "1", "attributes": { "name": "Dan Gebhard" } }] }

Slide 42

Slide 42 text

CRUD works pretty much as you'd expect

Slide 43

Slide 43 text

GET /articles GET /articles/1 POST /articles PATCH /articles/1 DELETE /articles/1

Slide 44

Slide 44 text

POST /articles { "data": { "type": "articles", "attributes": { "title": "JSON API paints my bikeshed!" } } }

Slide 45

Slide 45 text

HTTP/1.1 201 Created Location: http://example.com/articles/1 { "data": { "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" } } }

Slide 46

Slide 46 text

PATCH /articles/1 { "data": { "type": "articles", "id": "1", "attributes": { "title": "json:api paints my bikeshed!" } } }

Slide 47

Slide 47 text

HTTP/1.1 204 No Content

Slide 48

Slide 48 text

DELETE /articles/1

Slide 49

Slide 49 text

HTTP/1.1 204 No Content

Slide 50

Slide 50 text

Advanced Features

Slide 51

Slide 51 text

Inclusion of related resources can also be requested by the client

Slide 52

Slide 52 text

GET /articles/1?include=comments.author

Slide 53

Slide 53 text

Sparse field sets can be used to reduce the response size

Slide 54

Slide 54 text

GET /articles? include=author&fields[articles]=title,body&fi elds[people]=name

Slide 55

Slide 55 text

Bulk Operations allow creating/updating/deleting multiple resources at once

Slide 56

Slide 56 text

POST /articles { "data": [{ "type": "articles", "attributes": { "title": "JSON API paints my bikeshed!" } }, { "type": "articles", "attributes": { "title": "Rails is Omakase" } }] }

Slide 57

Slide 57 text

HTTP/1.1 201 Created { "data": [{ "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!" } },{ "type": "articles", "id": "2", "attributes": { "title": "Rails is Omakase" } }] }

Slide 58

Slide 58 text

Ruby Implementations

Slide 59

Slide 59 text

ActiveModelSerializers supports it in 0.10.0 https://github.com/rails-api/active_model_serializers

Slide 60

Slide 60 text

ROAR https://github.com/apotonick/roar

Slide 61

Slide 61 text

JSONAPI::Resources https://github.com/cerebris/jsonapi-resources

Slide 62

Slide 62 text

Client Libraries are available for many languages http://jsonapi.org/implementations/

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Slide 65

Slide 65 text

http://simplabs.com @simplabs