Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a Better API with JSON:API

Building a Better API with JSON:API

Chris Guzman

August 20, 2017
Tweet

More Decks by Chris Guzman

Other Decks in Technology

Transcript

  1. » Minimize requests & data between clients & servers »

    Consistency, popular among consultancies » Always backwards compatible » Stable 1.0, not dead
  2. » data: the document’s “primary data” » errors: an array

    of error objects » meta: a meta object that contains non-standard meta- information.
  3. { "data": { "type": "articles", "id": "1", "attributes": { "title":

    "Rails is Omakase" }, "relationships": { }, "links": { "self": "http://example.com/articles/1" }, } }
  4. { "relationships": { "author": { "links": { "self": "/articles/1/relationships/author", "related":

    "/articles/1/author" }, "data": { "type": "people", "id": "9" } } } }
  5. { "meta": { "count": "42", "copyright": "Copyright 2015 Example Corp.",

    "authors": [ "Yehuda Katz", "Steve Klabnik", "Dan Gebhardt", "Tyler Kellen" ] } }
  6. { "errors": [ "id": "UUID", "status": "418", "code": "42", "title":

    "You're a teapot", "detail": "The object is not short nor stout", "links": { "about": "https://httpstatuses.com/418" }, "meta": { "email": "[email protected]" } ] }
  7. #Request comments with an article GET /articles/1?include=comments Accept: application/vnd.api+json #Request

    comments as well as the author of each of those comments GET /articles/1?include=comments.author Accept: application/vnd.api+json
  8. GET /articles?include=comments,author &fields[people]=first-name,last-name &sort=-date » fetch all articles with their

    associated comments and authors » Only be return the first and last names » Sorted by date, most recent first
  9. POST /photos { "data": { "type": "photos", "attributes": { "title":

    "Ember Hamster", "src": "http://example.com/images/productivity.png" }, "relationships": { "photographer": { "data": { "type": "people", "id": "9" } } } } }