+++ title = "Hypermedia APIs vs. Data APIs" date = 2021-07-17 updated = 2022-04-07 [taxonomies] author = ["Carson Gross"] tag = ["posts"] +++ A *hypermedia* API is an API that returns [hypermedia](https://en.wikipedia.org/wiki/Hypermedia), typically HTML over HTTP. This style of API is distinguished from data APIs that do not return a hypermedia. The most familiar form of this latter style of API today is the ubiquitous JSON API. These two different types of API have distinctly different design needs and, therefore, should use different design constraints and adopt different goals when being created. Hypermedia APIs: * Will be trivially [REST-ful](https://en.wikipedia.org/wiki/Representational_state_transfer), since they are simply what [Roy Fielding was describing](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm). * Should be driven by the needs of the underlying hypermedia application * May change dramatically *without* versioning information, because hypermedia utilizes [self describing messages](https://en.wikipedia.org/wiki/Representational_state_transfer#Uniform_interface) * Should be passed directly to humans, to maximize the flexibility of the system Data APIs, on the other hand: * Will not benefit dramatically from REST-fulness, beyond perhaps [Level 2 of the Richardson Maturity Model](https://en.wikipedia.org/wiki/Richardson_Maturity_Model) * Should strive for both regularity and expressiveness due to the arbitrary data needs of consumers * Should be versioned and should be very stable within a particular version of the API * Should be consumed by code, processed and then potentially presented to a human ## APIs Today Today, APIs are typically thought of in terms of JSON-over-HTTP. These are almost always data-oriented APIs rather than hypermedia APIs, although occasionally hypermedia concepts are incorporated into them (typically to little benefit of the end users.) There has been a [movement away](https://graphql.org/) from REST-ful APIs as the industry has begun to [recognize the problems with fitting data APIs into the REST-ful model.](https://kieranpotts.com/rebranding-rest/) This is a good thing: the industry should question REST-ful ideas in the Data API world and begin looking at older client-server technologies that did a better job of servicing that particular network architecture, leaving REST instead to the network architecture that it was coined to describe: hypermedia APIs. ## Designing a Hypermedia API To show how a hypermedia API might be designed differently than a data API, let's consider the following situation, which came up on the [htmx discord](/discord) recently: > 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 > polling every 30 seconds so that updates from other users are shown. Let's consider this UI in terms of a base url, `/contacts` The first thing we will need is an end point to retrieve the form and the table of current contacts. This will live at `/contacts`, giving: ```txt GET /contacts -> render the form & contacts table ``` Next, we want to be able to create contacts. This would be done via a POST to the same URL: ```txt GET /contacts -> render the form & contacts table POST /contacts -> create the new contact, redirect to GET /contacts ``` with HTML that looks something like this: ```html