|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "JSON API: Quickstart" |
| 4 | +--- |
| 5 | + |
| 6 | +{% include status.md %} |
| 7 | + |
| 8 | +## JSON API Quickstart |
| 9 | + |
| 10 | +Welcome to the JSON API Quickstart! Following this document should give you an |
| 11 | +idea of what JSON API is like. For the full format documentation, please see |
| 12 | +[the format page](/format/). |
| 13 | + |
| 14 | +### An initial API |
| 15 | + |
| 16 | +For the purposes of this guide, we'll take an example API and convert it to |
| 17 | +JSON API format. This API will expose a blog, with posts, authors, and |
| 18 | +comments. For now, we'll only cover posts. Future work on this guide will cover |
| 19 | +the associations. |
| 20 | + |
| 21 | +There is also a live example of the API running |
| 22 | +[here](http://json-api-demo.herokuapp.com). |
| 23 | + |
| 24 | +As an example, here is what would happen if you were to request a list of all |
| 25 | +posts: |
| 26 | + |
| 27 | +```javascript |
| 28 | +[ |
| 29 | + { |
| 30 | + "id": 1, |
| 31 | + "title": "Rails is Omakase", |
| 32 | + "body": "There are a lot of...", |
| 33 | + "created_at": "2013-11-27T20:32:41.574Z", |
| 34 | + "updated_at": "2013-11-27T20:32:41.574Z" |
| 35 | + } |
| 36 | +] |
| 37 | +``` |
| 38 | + |
| 39 | +Pretty simple, right? This is what you're probably used to seeing when you |
| 40 | +fetch data from a random API. And there's a lot that's good about this: it's |
| 41 | +simple, there's not a lot there, it's pretty straightforward. But here's the |
| 42 | +problem: when everyone creates their own formats for an API, we have to |
| 43 | +re-build software to deal with it, every time. And while for this simple |
| 44 | +example, everything seems straightforward, what happens when you need extra |
| 45 | +features? Everyone implements them differently. JSON API provides a standard |
| 46 | +way that works across different APIs. |
| 47 | + |
| 48 | +### Adding JSON API support |
| 49 | + |
| 50 | +The first thing we need to do to turn this into a valid JSON API response is |
| 51 | +to namespace our data, like this: |
| 52 | + |
| 53 | +```javascript |
| 54 | +{ |
| 55 | + "posts": [{ |
| 56 | + "id": 1, |
| 57 | + "title": "Rails is Omakase", |
| 58 | + "body": "There are a lot of...", |
| 59 | + "created_at": "2013-11-27T20:32:41.574Z", |
| 60 | + "updated_at": "2013-11-27T20:32:41.574Z" |
| 61 | + }] |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +This allows us to have multiple kinds of objects in one response. Imagine when |
| 66 | +we add authors, as well as psots: you'll need a way to separate the two. |
| 67 | + |
| 68 | +Secondly, we're already using `id` as the name for our unique identifier: |
| 69 | +that's good, it's what JSON API already uses. There are three special names |
| 70 | +that are reserved by the specification: `id`, `href`, and `links`. Any other |
| 71 | +names are used for your own application data. |
| 72 | + |
| 73 | +The second thing we need to do is change our MIME type: instead of serving this |
| 74 | +response as `application/json`, it should be served as |
| 75 | +`application/vnd.api+json`. This allows clients to use the `Content-Type` header |
| 76 | +to specifically request JSON API responses, and allows people to find the |
| 77 | +documentation about the API type. |
| 78 | + |
| 79 | +You'll have to check your framework or library's documentation to figure out |
| 80 | +how to accomplish this. For example, |
| 81 | +[here](http://guides.rubyonrails.org/layouts_and_rendering.html#the-content-type-option) |
| 82 | +is the documentation for doing this with Ruby on Rails. |
| 83 | + |
| 84 | +For this simple case, that's all we need to do! |
| 85 | + |
| 86 | +### Moving to the URL style |
0 commit comments