--- layout: layout.njk tags: posts title: Hypermedia APIs vs. Data APIs --- ## Hypermedia APIs vs. Data APIs 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-oriented APIs. These two categories have different design needs and should be considered separately. Hypermedia APIs: * Will be [REST-ful](https://en.wikipedia.org/wiki/Representational_state_transfer) almost trivially * Should be driven by the needs of the underlying hypermedia application * May change dramatically without versioning information, because of the [self describing messages](https://en.wikipedia.org/wiki/Representational_state_transfer#Uniform_interface) inherent to the * Should be passed directly to humans, to maximize the flexibility of the system Data-Oriented 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 regularity and expressivity due to the arbitrary data needs of consumers * Should be versioned and very stable within a particular version * 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 badly and 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: ``` 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: ``` GET /contacts -> render the form & contacts table POST /contacts -> create the new contant, redirect to GET /contacts ``` with HTML that looks something like this: ```html