| 1 | --- |
| 2 | title: "Splitting Your Data & Application APIs: Going Further" |
| 3 | description: "If you split your API into Data and Application APIs, you should consider changing your Application API from JSON to Hypermedia (HTML) and using a hypermedia-oriented library like htmx to reap the benefits of the hypermedia model (simplicity, reliability, and flexibility)." |
| 4 | created: 2021-09-16 |
| 5 | modified: 2022-02-06 |
| 6 | authors: ["Carson Gross"] |
| 7 | tags: ["guides"] |
| 8 | --- |
| 9 | |
| 10 | **TLDR:** If you split your API into Data and Application |
| 11 | APIs, [as advocated here](https://max.engineer/server-informed-ui), |
| 12 | you should consider changing your Application API from JSON to Hypermedia (HTML) & using a *hypermedia-oriented* library |
| 13 | like |
| 14 | [htmx](/) to reap the benefits of the hypermedia model (simplicity, reliability, flexibility, etc.) |
| 15 | |
| 16 | ## The Problem |
| 17 | |
| 18 | Recently, [Max Chernyak](https://max.engineer/) wrote an essay entitled |
| 19 | [Don’t Build A General Purpose API To Power Your Own Front End](https://max.engineer/server-informed-ui). His |
| 20 | TLDR is this: |
| 21 | |
| 22 | > YAGNI, unless you’re working in a big company with federated front-ends or GraphQL. |
| 23 | |
| 24 | He then discusses some of the different needs of a general purpose API and your application API. He lists the |
| 25 | following as needs for a generic API: |
| 26 | |
| 27 | <div style="padding-left:64px"> |
| 28 | |
| 29 | 1. How to predict and enable all possible workflows |
| 30 | 1. How to avoid N+1 requests for awkward workflows |
| 31 | 1. How to test functionality, performance, and security of every possible request |
| 32 | 1. How to change the API without breaking the existing workflows |
| 33 | 1. How to prioritize API changes between internal and community requirements |
| 34 | 1. How to document everything so that all parties can get stuff done |
| 35 | |
| 36 | </div> |
| 37 | |
| 38 | And these as application API needs: |
| 39 | |
| 40 | <div style="padding-left:64px"> |
| 41 | |
| 42 | 1. How to collect all the data needed to render a page |
| 43 | 1. How to optimize requests to multiple endpoints |
| 44 | 1. How to avoid using API data fields in unintended ways |
| 45 | 1. How to weigh the benefit of new features against the cost of new API requests |
| 46 | |
| 47 | </div> |
| 48 | |
| 49 | I will term this misalignment of needs the **Data/App API Impedance Mismatch** problem. |
| 50 | |
| 51 | Max's recommendation is to split the API into two "halves": a generic API and an application API: |
| 52 | |
| 53 | > I suggest you stop treating your frontend as some generic API client, and start treating it as a half of your app. |
| 54 | > |
| 55 | > Imagine if you could just send it the whole “page” worth of JSON. Make an endpoint for `/page/a` and render the whole |
| 56 | > JSON for `/page/a` there. |
| 57 | > Do this for every page. Don’t force your front-end developers to send a bunch of individual requests to render a |
| 58 | > complex page. |
| 59 | > Stop annoying them with contrived limitations. Align yourselves. |
| 60 | |
| 61 | ## Right about What's Wrong |
| 62 | |
| 63 | I agree entirely with Max on the problem here. |
| 64 | |
| 65 | I would particularly emphasise the fact that the generic API needs to be stable, whereas the application API must change |
| 66 | rapidly to address application needs. |
| 67 | |
| 68 | Jean-Jacques Dubray, in [this article](https://www.infoq.com/articles/no-more-mvc-frameworks/) relates the following sad |
| 69 | state of affairs for |
| 70 | API designers: |
| 71 | |
| 72 | > The worst part of my job these days is designing APIs for front-end developers. The conversation goes inevitably as: |
| 73 | > |
| 74 | > Dev – So, this screen has data element x,y,z… could you please create an API with the response format {x: , y:, z: } |
| 75 | > |
| 76 | > Me – Ok |
| 77 | |
| 78 | This is a perfect encapsulation of the tension that Max has noticed, where API engineers want to design general, |
| 79 | stable APIs, but are subject to the whims of a quickly-changing UI with complex data needs that are often best |
| 80 | solved on *the server side*. |
| 81 | |
| 82 | As Max points out: |
| 83 | |
| 84 | > You can keep “page a” dumb to only do what it needs to do. You test the crap out of “page a” for bugs, security, |
| 85 | > performance. You can even fetch everything for “page a” in a single big SQL query. |
| 86 | |
| 87 | ## Wrong about What's Right |
| 88 | |
| 89 | So, again, I agree entirely with Max that there is a Data/App API Impedance Mismatch problem and I applaud him for |
| 90 | suggesting |
| 91 | that, rather than bailing out to a solution to like GraphQL, you split the APIs into two. |
| 92 | |
| 93 | However, there is a **next step** to take: |
| 94 | |
| 95 | Once you have split your application API from your generic data API, *you are no longer bound by the constraints of |
| 96 | a public data API* and are free to reconsider the *entire form* of that application API. We can do whatever we'd like |
| 97 | with |
| 98 | it, so let's get a bit expansive in our thinking. |
| 99 | |
| 100 | Note that core problems with the application API are rapid change and page (or resource) specific tuning. It turns out |
| 101 | that we |
| 102 | have a very good technology for dealing with *exactly* this |
| 103 | problem: [Hypermedia](https://en.wikipedia.org/wiki/Hypermedia)! |
| 104 | |
| 105 | Hypermedia, by way of HATEOAS, makes API |
| 106 | churn [much less of a problem](https://intercoolerjs.org/2016/02/17/api-churn-vs-security.html). When you change the |
| 107 | shape of your hypermedia API, well, |
| 108 | that's fine: the *new* API is simply reflected in the *new* HTML returned by the server. You can add and modify end |
| 109 | points |
| 110 | and, lo and behold (to a first order of approximation) your clients (that is, browsers) don't need to be updated. |
| 111 | |
| 112 | The browsers simply see the new HTML, |
| 113 | and [the humans driving them react to the new functionality appropriately](https://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.html). |
| 114 | |
| 115 | So, while I feel Max is on the right track, I also think he *doesn't go far enough*: once you have made the mental |
| 116 | jump to solving the Data/APP API Impedance Mismatch problem by splitting the two into separate concerns, it is only a |
| 117 | bit further down the road to rediscovering the advantages of hypermedia. |
| 118 | |
| 119 | You may object that: "Oh, but hypermedia applications aren't very usable, we don't want to go back to web 1.0." |
| 120 | |
| 121 | That is a perfectly reasonable objection, but people have been working on that problem and there are now many libraries |
| 122 | available that address the usability issues of HTML *within the hypermedia model*. |
| 123 | |
| 124 | Two of my favorites are [unpoly](https://unpoly.com/) and, of course, my own [htmx](/). |
| 125 | |
| 126 | ## In Conclusion |
| 127 | |
| 128 | If you switch to a hypermedia application API (which really just means "use HTML, like you used to") then you get all |
| 129 | of the benefits of the REST-ful web model (simplicity, reliability, etc.) and of server-side rendering in mature web |
| 130 | frameworks |
| 131 | (caching, SQL tuning, etc.) |
| 132 | |
| 133 | And, by choosing a hypermedia-oriented front end technology like htmx, you can |
| 134 | create [excellent user experiences](/patterns) within |
| 135 | that model. |
| 136 | |
| 137 | Everything old is new again, but, this time, a little bit better. |