Debian Codegraph @

www/src/content/essays/hypermedia-apis-vs-data-apis.md

raw symbols SCIP
1 ---
2 title: "Hypermedia APIs vs. Data APIs"
3 description: "In this essay, Carson Gross explores the fundamental differences between hypermedia APIs and data APIs, He explains how hypermedia APIs, which return HTML over HTTP, can embrace frequent changes due to their self-describing nature, while data APIs require more stability and versioning to avoid breaking client code. Carson argues that these distinct characteristics should inform different 01-frontend approaches for each type of API."
4 created: 2021-07-17
5 modified: 2022-04-07
6 authors: [ "Carson Gross" ]
7 tags: ["foundations"]
8 ---
9
10 A *hypermedia* API is an API that returns [hypermedia](https://en.wikipedia.org/wiki/Hypermedia), typically HTML over
11 HTTP. This style of API is distinguished from data APIs that do not return a hypermedia. The most familiar form of this
12 latter style of API today is the ubiquitous JSON API.
13
14 These two different types of API have distinctly different design needs and, therefore, should use different design
15 constraints and adopt different goals when being created.
16
17 Hypermedia APIs:
18
19 * Will be trivially [REST-ful](https://en.wikipedia.org/wiki/Representational_state_transfer), since they are simply
20 what [Roy Fielding was describing](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm).
21 * Should be driven by the needs of the underlying hypermedia application
22 * May change dramatically *without* versioning information, because hypermedia
23 utilizes [self describing messages](https://en.wikipedia.org/wiki/Representational_state_transfer#Uniform_interface)
24 * Should be passed directly to humans, to maximize the flexibility of the system
25
26 Data APIs, on the other hand:
27
28 * Will not benefit dramatically from REST-fulness, beyond
29 perhaps [Level 2 of the Richardson Maturity Model](https://en.wikipedia.org/wiki/Richardson_Maturity_Model)
30 * Should strive for both regularity and expressiveness due to the arbitrary data needs of consumers
31 * Should be versioned and should be very stable within a particular version of the API
32 * Should be consumed by code, processed and then potentially presented to a human
33
34 ## APIs Today
35
36 Today, APIs are typically thought of in terms of JSON-over-HTTP. These are almost always data-oriented APIs rather
37 than hypermedia APIs, although occasionally hypermedia concepts are incorporated into them (typically to
38 little benefit of the end users.) There has been a [movement away](https://graphql.org/) from REST-ful APIs as the
39 industry has begun
40 to [recognize the problems with fitting data APIs into the REST-ful model.](https://kieranpotts.com/rebranding-rest/)
41
42 This is a good thing: the industry should question REST-ful ideas in the Data API world and begin looking at older
43 client-server
44 technologies that did a better job of servicing that particular network architecture, leaving REST instead to the
45 network architecture
46 that it was coined to describe: hypermedia APIs.
47
48 ## Designing a Hypermedia API
49
50 To show how a hypermedia API might be designed differently than a data API, let's consider the following situation,
51 which came up on the [htmx discord](/discord) recently:
52
53 > I want a page with a form and a table on it. The form will add new elements to the table, and the table will also be
54 > polling every 30 seconds so that updates from other users are shown.
55
56 Let's consider this UI in terms of a base url, `/contacts`
57
58 The first thing we will need is an end point to retrieve the form and the table of current contacts. This will
59 live at `/contacts`, giving:
60
61 ```txt
62 GET /contacts -> render the form & contacts table
63 ```
64
65 Next, we want to be able to create contacts. This would be done via a POST to the same URL:
66
67 ```txt
68 GET /contacts -> render the form & contacts table
69 POST /contacts -> create the new contact, redirect to GET /contacts
70 ```
71
72 with HTML that looks something like this:
73
74 ```html
75 <div>
76 <form action='/contacts' method="post">
77 <!-- form for adding contacts -->
78 </form>
79 <table>
80 <!-- contacts table -->
81 </table>
82 </div>
83 ```
84
85 So far, so standard web 1.0 application, and thus far the data-API and hypermedia API needs haven't diverged very much,
86 although it is worth noteing that the hypermedia API is *self describing* and could be modified (say, changing the URL
87 for creating
88 contacts) without breaking the hypermedia application.
89
90 Now we get to the part where htmx is needed: polling the server for updates to the table occasionally. To do this
91 we will add a new end point, `/contacts/table`, which renders only the table of contacts:
92
93 ```txt
94 GET /contacts -> render the form & contacts table
95 POST /contacts -> create the new contact, redirect to GET /contacts
96 GET /contacts/table -> render the contacts table
97 ```
98
99 and then add a poll trigger to the table:
100
101 ```html
102 <div>
103 <form action='/contacts' method="post">
104 <!-- form for adding contacts -->
105 </form>
106 <table hx-trigger="every 30s" hx-get="/contacts/table" hx-swap="outerHTML">
107 <!-- contacts table -->
108 </table>
109 </div>
110 ```
111
112 Here we see the hypermedia API and data API begin to diverge. This new end point is driven entirely by hypermedia
113 needs, not data model needs. This end point can go away if the hypermedia needs of the application change; its form may
114 change
115 dramatically and so on, which is entirely acceptable since the system is self-describing.
116
117 Since we have updated the HTML to use htmx for polling, we may as well make the form use htmx as well for a better
118 UX experience:
119
120 ```html
121 <div>
122 <form action='/contacts' method="post" hx-boost="true">
123 <!-- form for adding contacts -->
124 </form>
125 <table hx-trigger="every 30s" hx-get="/contacts/table" hx-swap="outerHTML">
126 <!-- contacts table -->
127 </table>
128 </div>
129 ```
130
131 We can, if we choose, add additional end points for things like server-side validation of inputs, dynamic forms and
132 so forth. These end points would be driven by *hypermedia needs* rather than any sort of data model considerations:
133 we think in terms of what we are trying to achieve with our application.
134
135 ## API Churn
136
137 The crux point of this short essay is this: API churn is fine in a hypermedia system because *the messages in a
138 hypermedia system are self-describing*.
139 We can thrash the API around and the application doesn't break: human users simply see the new hypermedia (HTML) and
140 select what
141 actions they want to do.
142
143 Humans, compared with computers,
144 are [good at deciding what to do](https://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.html)
145 and are reasonably OK with change.
146
147 This is in contrast with data APIs. Data APIs cannot be modified without breaking client code and thus must be much
148 more disciplined in their changes. Data APIs also face pressure to provide higher levels of expressiveness so that they
149 can satisfy more client needs without modification.
150
151 <aside>
152
153 *This latter situation is [especially dangerous](https://intercoolerjs.org/2016/02/17/api-churn-vs-security.html) when
154 these data APIs are consumed in a browser, because any data-api expressiveness available to a front-end developer is
155 also available to a potentially hostile user, who can fire up a console and begin hammering away at the API. Apparently,
156 facebook uses a [whitelist](https://twitter.com/AdamChainz/status/1392162996844212232) to deal with this.*
157
158 *Do you?*
159
160 </aside>
161
162 ## Conclusion
163
164 When designing a hypermedia API, you should use a different design mindset than you use for data APIs. Churn is
165 much less of a concern, and providing the end points you need for a good hypermedia experience should be your primary
166 goal.