<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>notroot (Posts about literate programming)</title><link>https://blog.notroot.online/</link><description></description><atom:link href="https://blog.notroot.online/categories/literate-programming.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 &lt;a href="mailto:jedwards8th@gmail.com"&gt;Joseph Edwards VIII&lt;/a&gt; </copyright><lastBuildDate>Sat, 06 Apr 2024 23:15:30 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Writing Literate API Documentation in Emacs Org Mode</title><link>https://blog.notroot.online/posts/writing-literate-api-documentation-in-emacs-org-mode/</link><dc:creator>Joseph Edwards VIII</dc:creator><description>&lt;p&gt;
On a scale of "NFT" to "Integer", &lt;a href="http://howardism.org/Technical/Emacs/literate-programming-tutorial.html"&gt;Literate Programming&lt;/a&gt; usually rates a little below the bottom on the usefulness scale. Unfair! It's not entirely unearned, but there are some things that "LitProg" (kidding!) is very good for.
&lt;/p&gt;

&lt;p&gt;
With &lt;a href="https://github.com/pashky/restclient.el"&gt;restclient&lt;/a&gt;, &lt;a href="https://github.com/alf/ob-restclient.el"&gt;ob-restclient&lt;/a&gt; and Emacs Org Mode, I'll show you how to write beautiful, useful, self-generating API documentation that easily renders to a static website, using a template forked off of &lt;a href="https://readthedocs.org/"&gt;Read the Docs&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
Where's the &lt;b&gt;"WOW!"&lt;/b&gt; you may ask? Using this technique, you can entirely get rid of &lt;a href="https://postman.com"&gt;Postman&lt;/a&gt; or whatever clunky proprietary REST API client you're using. The API documentation &lt;b&gt;IS&lt;/b&gt; the program.
&lt;/p&gt;


&lt;div id="orgfaa47e0" class="figure"&gt;
&lt;p&gt;&lt;img src="https://blog.notroot.online/images/lit-api-ex-1.png" alt="nil"&gt;
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
The only other requisite is an API to talk to. For this tutorial, we'll be using the free, public &lt;a href="https://jsonplaceholder.typicode.com/"&gt;JSON Placeholder&lt;/a&gt; API.
&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;&lt;b&gt;NOTE&lt;/b&gt;&lt;/b&gt;: Since I wrote this blog post in Org Mode, the included examples of Org are exported as plain-text examples, without syntax-highlighting. Suffice it to say that Org text looks much prettier in Emacs.
&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;&lt;b&gt;TL;DR&lt;/b&gt;&lt;/b&gt;: &lt;a href="https://gist.github.com/joseph8th/9a87c4f14b867c1abe8d2e81322f4eac#file-jsonplaceholder-org"&gt;Complete Example Org File&lt;/a&gt;
&lt;/p&gt;

&lt;div id="outline-container-orgb97e9a9" class="outline-2"&gt;
&lt;h2 id="orgb97e9a9"&gt;Getting Started&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-orgb97e9a9"&gt;
&lt;p&gt;
First, install and configure &lt;code&gt;restclient&lt;/code&gt; and &lt;code&gt;ob-restclient&lt;/code&gt; in your Emacs.
&lt;/p&gt;

&lt;p&gt;
Then, create a new file in Emacs, called &lt;code&gt;jsonplaceholder.org&lt;/code&gt;. At the top of this file, include the following header information (obviously change the author and email):
&lt;/p&gt;

&lt;pre class="example" id="org4898508"&gt;
#+title: JSON Placeholder API Documentation
#+author: Joseph Edwards VIII
#+email: foobar@example.com

#+startup: indent
#+export_file_name: index.html
#+options: num:nil ^:nil H:5 toc:2

#+setupfile: https://fniessen.github.io/org-html-themes/org/theme-readtheorg.setup
&lt;/pre&gt;

&lt;p&gt;
Let's talk about this header a bit, since it's not immediately apparent what's going on.
&lt;/p&gt;

&lt;p&gt;
The top section is self-explanatory. The second section just tells Emacs to display org-mode with indents, specifies the name of the file to export to, and controls the way that section headings and the table of contents will be displayed.
&lt;/p&gt;

&lt;p&gt;
The final section loads the HTML template &lt;code&gt;setupfile&lt;/code&gt; to use when exporting to HTML. In practice, I also add a number of &lt;code&gt;#+html_head:&lt;/code&gt; items to customize the &lt;a href="https://github.com/fniessen/org-html-themes"&gt;ReadTheOrg&lt;/a&gt; theme CSS.
&lt;/p&gt;
&lt;/div&gt;

&lt;div id="outline-container-org0324581" class="outline-3"&gt;
&lt;h3 id="org0324581"&gt;Initialize&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-org0324581"&gt;
&lt;p&gt;
The first section we'll add will serve a functional purpose, and initialize the document. In practice, this section can be quite detailed, allowing you to obtain a bearer token, read values from a database, or otherwise initialize elements that will be used later in the program. For our simple example, we only need one element: the URL to the API.
&lt;/p&gt;

&lt;p&gt;
It may seem odd to parameterize the API URL. Don't we want it shown in our requests? Perhaps. But perhaps, like most software engineers, we have separate development, staging and production environments? We would have to find and replace each URL to change environment. This way, we can just change it in one place.
&lt;/p&gt;

&lt;p&gt;
One small note: we don't want to export this section to HTML. We can tell Emacs this by adding the &lt;code&gt;:noexport:&lt;/code&gt; tag to the section header.
&lt;/p&gt;

&lt;p&gt;
In your &lt;code&gt;jsonplaceholder.org&lt;/code&gt; file, add:
&lt;/p&gt;

&lt;pre class="example" id="orgadb68dd"&gt;
* Init :noexport:

#+name: api-url
: https://jsonplaceholder.typicode.com
&lt;/pre&gt;

&lt;p&gt;
Setting the &lt;code&gt;#+name: api-url&lt;/code&gt; means we can reference the scalar value &lt;code&gt;: https://jsonplaceholder.typicode.com&lt;/code&gt; from other source blocks in our document. &lt;b&gt;We can take &lt;code&gt;api-url&lt;/code&gt; as input.&lt;/b&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org0bd20ae" class="outline-3"&gt;
&lt;h3 id="org0bd20ae"&gt;Introduce&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-org0bd20ae"&gt;
&lt;p&gt;
While in practice, we're going to be &lt;i&gt;using&lt;/i&gt; &lt;code&gt;jsonplaceholder.org&lt;/code&gt; to make API requests, Emacs will &lt;i&gt;also&lt;/i&gt; be making API requests when we export to HTML to generate the API documentation.
&lt;/p&gt;

&lt;p&gt;
That reminds us that this is documentation. So write a nice introduction for your readers, as well.
&lt;/p&gt;

&lt;p&gt;
In your &lt;code&gt;jsonplaceholder.org&lt;/code&gt; file, add the following (copied from the actual API documentation for JSON Placeholder):
&lt;/p&gt;

&lt;pre class="example" id="org23a9214"&gt;
* Introduction

JSONPlaceholder is a free online REST API that you can use whenever you need some fake data. It can be in a README on GitHub, for a demo on CodeSandbox, in code examples on Stack Overflow, ...or simply to test things locally.
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org0f94c2b" class="outline-2"&gt;
&lt;h2 id="org0f94c2b"&gt;Making Requests&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-org0f94c2b"&gt;
&lt;p&gt;
Now let's document our first request and response. JSON Placeholder provides six resources, and routes for all HTTP methods. We'll start with the &lt;code&gt;/posts&lt;/code&gt; resource.
&lt;/p&gt;

&lt;p&gt;
In your &lt;code&gt;jsonplaceholder.org&lt;/code&gt; file, add:
&lt;/p&gt;

&lt;pre class="example" id="org294e7b2"&gt;
* Posts

The ~/posts~ resource allows us to create, read, update, and delete posts.

#+name: get-posts
#+begin_src restclient :var api=api-url
  GET :api/posts
#+end_src
&lt;/pre&gt;

&lt;p&gt;
Notice in the source block header, we are directing Org to use &lt;code&gt;restclient&lt;/code&gt; as the language.
&lt;/p&gt;

&lt;p&gt;
We also set the variable &lt;code&gt;api&lt;/code&gt; to the value stored in the scalar named &lt;code&gt;api-url&lt;/code&gt;. When using Org variables inside a source block, you just use the variable as you normally would in that language. In &lt;code&gt;restclient&lt;/code&gt;, we preface variables with a colon, so that's how we use it in the request, &lt;code&gt;GET :api/posts&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Now, make the API call by typing &lt;code&gt;C-c C-c&lt;/code&gt; with the cursor inside the source block. The response will be displayed below the request block, in all its gory glory!
&lt;/p&gt;

&lt;p&gt;
And this illustrates the usefulness of literate API documentation. We aren't going to include example requests and example responses that have to be updated whenever changes are made to the API endpoints or responses.
&lt;/p&gt;

&lt;p&gt;
We're only including &lt;b&gt;real&lt;/b&gt; requests!
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-orge02fd60" class="outline-2"&gt;
&lt;h2 id="orge02fd60"&gt;Exporting&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-orge02fd60"&gt;
&lt;p&gt;
We only have the one request, but it's enough to export. Let's try it!
&lt;/p&gt;

&lt;p&gt;
In your &lt;code&gt;jsonplaceholder.org&lt;/code&gt; file, type &lt;code&gt;C-c C-e h o&lt;/code&gt; to export to &lt;code&gt;index.html&lt;/code&gt; in the same directory as your org file, and then open that file in the browser. It should look like this:
&lt;/p&gt;


&lt;div id="orgaa1b0ad" class="figure"&gt;
&lt;p&gt;&lt;img src="https://blog.notroot.online/images/lit-api-ex-2.png" alt="nil"&gt;
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
Ok, but where's the response? Emacs didn't make the API request and include the response like I promised! Oh no!
&lt;/p&gt;

&lt;p&gt;
That's OK. By default, Emacs only exports the source block. To also export results, edit the header of the source block named &lt;code&gt;get-posts&lt;/code&gt;. Add &lt;code&gt;:exports both&lt;/code&gt; so that it looks like:
&lt;/p&gt;

&lt;pre class="example" id="orge2e0224"&gt;
#+begin_src restclient :var api=api-url :exports both
&lt;/pre&gt;

&lt;p&gt;
And export, again. Now you should see the response, as well!
&lt;/p&gt;


&lt;div id="org2c6f9f1" class="figure"&gt;
&lt;p&gt;&lt;img src="https://blog.notroot.online/images/lit-api-ex-3.png" alt="nil"&gt;
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
No more updating example responses and including embarrassing typos! This is real, live response data.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-orga9c8a3f" class="outline-2"&gt;
&lt;h2 id="orga9c8a3f"&gt;Appearance&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-orga9c8a3f"&gt;
&lt;p&gt;
Before we go any further with the "programming" part of "literate programming", let's recall for a moment that we are writing API documentation, and we want it to be clean and readable. Let's spend a little time sprucing up the appearance.
&lt;/p&gt;

&lt;p&gt;
Currently, our document is ugly in three ways:
&lt;/p&gt;

&lt;ol class="org-ol"&gt;
&lt;li&gt;The anchors to headings are ugly, and read like: &lt;code&gt;index.html#orga1b1b2d&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The response is too big! We will have to scroll for pages to get to the next request.&lt;/li&gt;
&lt;li&gt;The response headers are dumped out at the bottom of the response. It's not clean JSON.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;

&lt;div id="outline-container-orge1e26bc" class="outline-3"&gt;
&lt;h3 id="orge1e26bc"&gt;Ugly Anchors&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-orge1e26bc"&gt;
&lt;p&gt;
The ugly anchors issue is easily solved. When Emacs exports to HTML, it generates random anchor tags for headings, but we can easily specify a custom ID.
&lt;/p&gt;

&lt;p&gt;
In your &lt;code&gt;jsonplaceholder.org&lt;/code&gt; file, place your cursor at the end of the &lt;code&gt;Introduction&lt;/code&gt; heading, and then type &lt;code&gt;C-c C-x p&lt;/code&gt; to add a property. Type in &lt;code&gt;CUSTOM_ID&lt;/code&gt; for the property name, and &lt;code&gt;introduction&lt;/code&gt; for the value.
&lt;/p&gt;

&lt;p&gt;
Now do the same thing for the &lt;code&gt;Posts&lt;/code&gt; heading, setting &lt;code&gt;CUSTOM_ID&lt;/code&gt; to &lt;code&gt;resource-posts&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
It should now look like this:
&lt;/p&gt;

&lt;pre class="example" id="org31a24f3"&gt;
* Posts
:PROPERTIES:
:CUSTOM_ID: resource-posts
:END:
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org6ec845f" class="outline-3"&gt;
&lt;h3 id="org6ec845f"&gt;Enormous Response&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-org6ec845f"&gt;
&lt;p&gt;
Inspecting &lt;code&gt;index.html&lt;/code&gt; reveals that the size of the response block is determined by the tag and class &lt;code&gt;pre.src&lt;/code&gt;. Some tinkering on my part results in the following snippet:
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&amp;lt;style&amp;gt;pre.src{background:#343131;color:white;max-height:500px;overflow-y:auto;}&amp;lt;/style&amp;gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;
We can add this to our exported &lt;code&gt;index.html&lt;/code&gt; very easily in Org Mode using the &lt;code&gt;#+html_head:&lt;/code&gt; header. After the line that starts with &lt;code&gt;#+setupfile:&lt;/code&gt;, add:
&lt;/p&gt;

&lt;pre class="example" id="org12b318a"&gt;
#+html_head: &amp;lt;style&amp;gt;pre.src{background:#343131;color:white;max-height:500px;overflow-y:auto;} &amp;lt;/style&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Also, nobody likes "Light Mode," so I changed to "Dark Mode".
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org270d236" class="outline-3"&gt;
&lt;h3 id="org270d236"&gt;Ugly Response&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-org270d236"&gt;
&lt;p&gt;
The ugly response headers we see are coming from the &lt;code&gt;restclient&lt;/code&gt; package, and there's no way to suppress them without hacking over &lt;code&gt;restclient&lt;/code&gt; itself. That of course is do-able … this &lt;i&gt;is&lt;/i&gt; Emacs, after all.
&lt;/p&gt;

&lt;p&gt;
We will instead use a &lt;i&gt;literate&lt;/i&gt; solution.
&lt;/p&gt;

&lt;p&gt;
One of the more powerful features of Literate Programming is language-agnosticism. We run a SQL query from Org Mode, and pipe the data to a Python source block where we manipulate it, then pass it on to Bash, then to R, then to Elisp, then to Common Lisp, then back to Python and save it back to the database.
&lt;/p&gt;

&lt;p&gt;
In our case, we're going to leverage &lt;code&gt;shell&lt;/code&gt; and &lt;a href="https://github.com/stedolan/jq/wiki/Installation"&gt;the 'jq' command-line JSON processor&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
Go ahead and install &lt;code&gt;jq&lt;/code&gt; now, and then change your &lt;code&gt;Posts&lt;/code&gt; request so it looks like the following:
&lt;/p&gt;

&lt;pre class="example" id="org4f0c978"&gt;
#+name: get-posts
#+begin_src restclient :var api=api-url :results value
  GET :api/posts
#+end_src

#+begin_src shell :var response=get-posts :results value raw :wrap src js :exports results
  echo $response | jq
#+end_src
&lt;/pre&gt;

&lt;p&gt;
And now place your cursor in the bottom (&lt;code&gt;shell&lt;/code&gt;) source block, and type &lt;code&gt;C-c C-c&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
The response block calls the &lt;code&gt;get-posts&lt;/code&gt; request block, and then pipes &lt;code&gt;$response&lt;/code&gt; into &lt;code&gt;jq&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Wow! No more response headers! Just nice, clean JSON.
&lt;/p&gt;
&lt;/div&gt;

&lt;div id="outline-container-org8647a57" class="outline-4"&gt;
&lt;h4 id="org8647a57"&gt;Black Magic Explained&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-org8647a57"&gt;
&lt;p&gt;
"But what is this black magic?!" you may ask. Let's pop the hood.
&lt;/p&gt;

&lt;p&gt;
The first big change was to the request. No longer are we saying, &lt;code&gt;:exports both&lt;/code&gt;. Now we are instead saying, &lt;code&gt;:results value&lt;/code&gt;. As mentioned, the default for &lt;code&gt;:exports&lt;/code&gt; is &lt;i&gt;only the source block&lt;/i&gt;. By deleting &lt;code&gt;:exports both&lt;/code&gt;, we're going back to that default. This source block will not be evaluated, nor will the results be exported.
&lt;/p&gt;

&lt;p&gt;
Instead, we now tell the request, &lt;code&gt;:results value&lt;/code&gt;. What does that mean? &lt;a href="https://orgmode.org/manual/Results-of-Evaluation.html"&gt;It's complicated&lt;/a&gt;, but basically it just means that Org gets the value from the evaluated code, itself, rather than using an external process.
&lt;/p&gt;

&lt;p&gt;
The second, more obvious change is that we've added a second source block, just for results. Several important things are happening in the block header:
&lt;/p&gt;

&lt;ol class="org-ol"&gt;
&lt;li&gt;We call &lt;code&gt;shell&lt;/code&gt; this time. That will be whatever shell (bash, cmd.exe, fish, etc.) you are using.&lt;/li&gt;
&lt;li&gt;We set a new variable &lt;code&gt;:var response=get-posts&lt;/code&gt;. Now this source block will call the request block named &lt;code&gt;get-posts&lt;/code&gt; and put the results in the variable named &lt;code&gt;response&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We set &lt;code&gt;:results value raw&lt;/code&gt; because we don't want Org to wrap the results in &lt;code&gt;shell&lt;/code&gt; type.&lt;/li&gt;
&lt;li&gt;We set &lt;code&gt;:wrap src js&lt;/code&gt; because we DO want Org to wrap the results in &lt;code&gt;js&lt;/code&gt; type.&lt;/li&gt;
&lt;li&gt;We set &lt;code&gt;:exports results&lt;/code&gt; because we don't want to see the source code, only the results.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Finally, inside the &lt;code&gt;shell&lt;/code&gt; block, we pipe the &lt;code&gt;$results&lt;/code&gt; variable into &lt;code&gt;jq&lt;/code&gt;, which strips out the response header comment lines and outputs nicely formatted, prettified JSON for our results block.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org038c411" class="outline-3"&gt;
&lt;h3 id="org038c411"&gt;Putting It All Together&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-org038c411"&gt;
&lt;p&gt;
Let's see how our tweaks to appearance worked out! Export your &lt;code&gt;jsonplaceholder.org&lt;/code&gt; file again with &lt;code&gt;C-c C-e h o&lt;/code&gt; and you should now see something like:
&lt;/p&gt;


&lt;div id="org19ad199" class="figure"&gt;
&lt;p&gt;&lt;img src="https://blog.notroot.online/images/lit-api-ex-4.png" alt="nil"&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-orge085b7f" class="outline-4"&gt;
&lt;h4 id="orge085b7f"&gt;Finishing Touches&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-orge085b7f"&gt;
&lt;p&gt;
Looks much better, but there's a few more tweaks to make. Since it's just applying the same principles we already discussed, let's fast-forward to the end.
&lt;/p&gt;

&lt;p&gt;
Change your &lt;code&gt;jsonplaceholder.org&lt;/code&gt; file so it looks like this:
&lt;/p&gt;

&lt;pre class="example" id="org7cc7beb"&gt;
#+title: JSON Placeholder API Documentation
#+author: Joseph Edwards VIII
#+email: foobar@example.com

#+startup: indent
#+export_file_name: index.html
#+options: num:nil ^:nil H:5 toc:2

#+setupfile: https://fniessen.github.io/org-html-themes/org/theme-readtheorg.setup
#+html_head: &amp;lt;style&amp;gt;pre.src{background:#343131;color:white;max-height:500px;overflow-y:auto;} &amp;lt;/style&amp;gt;
#+html_head: &amp;lt;style&amp;gt;p{margin-bottom:1em;}&amp;lt;/style&amp;gt;
#+html_head: &amp;lt;style&amp;gt;h2{padding-top:1em;margin-top:2em;border-top:darkgray 2px solid;}&amp;lt;/style&amp;gt;
#+html_head: &amp;lt;style&amp;gt;h3{padding-top:1em;margin-top:2em;border-top:lightblue 1px dashed;}&amp;lt;/style&amp;gt;
#+html_head: &amp;lt;style&amp;gt;h4{padding-top:1em;margin-bottom:1em;}&amp;lt;/style&amp;gt;
#+html_head: &amp;lt;style&amp;gt;h5{color:black;font-size:1em;padding-top:1em;margin-bottom:1em;} &amp;lt;/style&amp;gt;
#+html_head: &amp;lt;style&amp;gt;div.response&amp;gt;h5,div.request&amp;gt;h5{padding-top:0;margin-bottom:0.5em;color:darkgray;font-size:0.8em;}&amp;lt;/style&amp;gt;
#+html_head: &amp;lt;style&amp;gt;h6{margin-bottom:1em;}&amp;lt;/style&amp;gt;

* Init :noexport:

#+name: api-url
: https://jsonplaceholder.typicode.com

* Introduction
:PROPERTIES:
:CUSTOM_ID: introduction
:END:

JSONPlaceholder is a free online REST API that you can use whenever you need some fake data. It can be in a README on GitHub, for a demo on CodeSandbox, in code examples on Stack Overflow, ...or simply to test things locally.

* Posts
:PROPERTIES:
:CUSTOM_ID: resource-posts
:END:

The ~/posts~ resource allows us to create, read, update, and delete posts.

** ~GET /posts~
:PROPERTIES:
:CUSTOM_ID: method-get-posts
:END:

Obtain all posts.

**** Request
:PROPERTIES:
:HTML_CONTAINER_CLASS: request
:END:

#+name: get-posts
#+begin_src restclient :var api=api-url :results value
  GET :api/posts
#+end_src

**** Response
:PROPERTIES:
:HTML_CONTAINER_CLASS: response
:END:

#+begin_src shell :var response=get-posts :results value raw :wrap src js :exports results
  echo $response | jq
#+end_src
&lt;/pre&gt;

&lt;p&gt;
Now our &lt;code&gt;index.html&lt;/code&gt; should look like this:
&lt;/p&gt;


&lt;div id="org395aee1" class="figure"&gt;
&lt;p&gt;&lt;img src="https://blog.notroot.online/images/lit-api-ex-5.png" alt="nil"&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org930dbc1" class="outline-4"&gt;
&lt;h4 id="org930dbc1"&gt;Discussion&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-org930dbc1"&gt;
&lt;p&gt;
Briefly let's highlight a couple items in the above changes.
&lt;/p&gt;

&lt;p&gt;
First, note the use of a new property, &lt;code&gt;:HTML_CONTAINER_CLASS: request&lt;/code&gt; and &lt;code&gt;:HTML_CONTAINER_CLASS: response&lt;/code&gt;. Upon export, this property attaches the respective CSS class of &lt;code&gt;.request&lt;/code&gt; or &lt;code&gt;.response&lt;/code&gt; to the &lt;code&gt;div&lt;/code&gt; element containing the heading. In this way, we can style &lt;code&gt;div.request&amp;gt;h5&lt;/code&gt; and &lt;code&gt;div.response&amp;gt;h5&lt;/code&gt; to make clear what each of these blocks is for.
&lt;/p&gt;

&lt;p&gt;
Also, note that we have moved our request and response sections inside a new subheading of &lt;code&gt;Posts&lt;/code&gt;, called &lt;code&gt;GET /posts&lt;/code&gt;. Now, when we click on "Posts" in the sidebar menu, we see "GET /posts" as a submenu item. This is controlled at by the Org header directive &lt;code&gt;#+options: toc:2&lt;/code&gt; included. If we set it to &lt;code&gt;toc:1&lt;/code&gt; we would not see the submenu. If we set it to &lt;code&gt;toc:3&lt;/code&gt; we would see "Request" and "Response" in the submenu below "GET /posts". Try it and see!
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org997fe75" class="outline-2"&gt;
&lt;h2 id="org997fe75"&gt;Finish the Job&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-org997fe75"&gt;
&lt;p&gt;
Documenting the rest of the API now proceeds according to a set pattern. Indeed, a &lt;code&gt;yasnippet&lt;/code&gt; could be constructed to insert a new method section, and speed the process along.
&lt;/p&gt;

&lt;p&gt;
However, that doesn't reflect the utility of the Literate Programming approach to API documentation. Normally, I write this documentation &lt;i&gt;as I am building new API endpoints&lt;/i&gt;, in order to test and perfect the endpoint. I use this method &lt;i&gt;instead of Postman&lt;/i&gt;, and when I am done working on the API, it is &lt;i&gt;already documented&lt;/i&gt; and under version control with the source code.
&lt;/p&gt;

&lt;p&gt;
For example, I would have written what we have so far &lt;i&gt;at the same time I was writing&lt;/i&gt; &lt;code&gt;GET :api/posts&lt;/code&gt;, and I would have used that file extensively, making note of any parameters required.
&lt;/p&gt;

&lt;p&gt;
When I pushed code, anyone on my team (who uses Emacs) could open the &lt;code&gt;README.org&lt;/code&gt; and test the API for themselves.
&lt;/p&gt;

&lt;p&gt;
As a last step before I push code, I can generate the &lt;code&gt;index.html&lt;/code&gt; and pop it into a public static directory. Now when you browse to the base URL of my API, instead of getting an error message, you get API documentation.
&lt;/p&gt;

&lt;p&gt;
Let's wrap up this post by documenting the rest of the &lt;code&gt;/posts&lt;/code&gt; resource methods. This will also serve to illustrate how to use &lt;code&gt;restclient&lt;/code&gt; to perform &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; HTTP methods.
&lt;/p&gt;
&lt;/div&gt;

&lt;div id="outline-container-orge08f50c" class="outline-3"&gt;
&lt;h3 id="orge08f50c"&gt;Adding a POST Method&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-orge08f50c"&gt;
&lt;p&gt;
Let's add a &lt;code&gt;POST /posts&lt;/code&gt; header under &lt;code&gt;GET /posts&lt;/code&gt;. You can copy and paste, and then change the following:
&lt;/p&gt;

&lt;ol class="org-ol"&gt;
&lt;li&gt;Change &lt;code&gt;:CUSTOM_ID:&lt;/code&gt; from &lt;code&gt;method-get-posts&lt;/code&gt; to &lt;code&gt;method-post-posts&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Change the "Request" &lt;code&gt;#+name:&lt;/code&gt; from &lt;code&gt;get-posts&lt;/code&gt; to &lt;code&gt;post-posts&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Change the "Response" &lt;code&gt;:var response&lt;/code&gt; from &lt;code&gt;get-posts&lt;/code&gt; to &lt;code&gt;post-posts&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Now, add the variable &lt;code&gt;:var user-id=1&lt;/code&gt; to the &lt;code&gt;post-posts&lt;/code&gt; request source block header. With &lt;code&gt;requests&lt;/code&gt;, we also need to specify the content type and JSON payload to deliver.
&lt;/p&gt;

&lt;p&gt;
Notice that we can use Org variables &lt;i&gt;inside the JSON payload&lt;/i&gt;. &lt;b&gt;WOW!&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;
The final POST request should look like:
&lt;/p&gt;

&lt;pre class="example" id="org34e2ec5"&gt;
#+name: post-posts
#+begin_src restclient :var api=api-url :var user-id=1 :results value
  POST :api/posts
  Content-Type: application/json

  {
    "title": "foo",
    "body": "bar",
    "userId": :user-id
  }
#+end_src
&lt;/pre&gt;

&lt;p&gt;
Now, when you put the cursor in the "Response" block and type &lt;code&gt;C-c C-c&lt;/code&gt; you should get back the new post data you created in JSON Placeholder. It should look something like:
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"foo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"body"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"bar"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"userId"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;101&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org53a2b13" class="outline-3"&gt;
&lt;h3 id="org53a2b13"&gt;Adding a PUT Method&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-org53a2b13"&gt;
&lt;p&gt;
As you can imagine, the process proceeds similarly. Copy and paste &lt;code&gt;POST /posts&lt;/code&gt; to &lt;code&gt;PUT /posts/:id&lt;/code&gt;, and change the custom ID, request name, and response variable to &lt;code&gt;put-posts&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Now our request should look similar to a POST request, except now we must specify the post's &lt;code&gt;id&lt;/code&gt; in the URL, and the JSON we use will update an existing post, rather than creating a new one.
&lt;/p&gt;

&lt;p&gt;
Our final PUT request should look like:
&lt;/p&gt;

&lt;pre class="example" id="org73f8183"&gt;
#+name: put-posts
#+begin_src restclient :var api=api-url :var id=1 :var user-id=1 :results value
  PUT :api/posts/:id
  Content-Type: application/json

  {
    "title": "foo",
    "body": "bar",
    "userId": :user-id
  }
#+end_src
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-orgef70f12" class="outline-3"&gt;
&lt;h3 id="orgef70f12"&gt;Adding a DELETE Method&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-orgef70f12"&gt;
&lt;p&gt;
Surprise, surprise! Same idea as before. Copy, paste, change, run, profit!
&lt;/p&gt;

&lt;pre class="example" id="orgde27ff7"&gt;
#+name: delete-posts
#+begin_src restclient :var api=api-url :var id=1 :results value
  DELETE :api/posts/:id
#+end_src
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org33850c9" class="outline-2"&gt;
&lt;h2 id="org33850c9"&gt;Conclusion&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-org33850c9"&gt;
&lt;p&gt;
Our final API documentation isn't very detailed (we only documented the &lt;code&gt;/posts&lt;/code&gt; resource) but the rest is just a repetition of what we've already discussed. Still, it's quite nice, and can easily be made nicer:
&lt;/p&gt;


&lt;div id="org7dccc02" class="figure"&gt;
&lt;p&gt;&lt;img src="https://blog.notroot.online/images/lit-api-ex-6.png" alt="nil"&gt;
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
Also, as discussed, it would be easy enough to write a &lt;code&gt;yasnippet&lt;/code&gt; to automate create new method sections, then tab from field to field filling it out.
&lt;/p&gt;

&lt;p&gt;
Also as discussed, that's not really the most useful approach to writing Literate API documentation. More useful is to write it as you go, using the document instead of Postman. Then not only is your documentation never wrong or out of sync with the API, but you don't have to go back and "waste time" documenting something you've already completed!
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-orgf08cbf6" class="outline-2"&gt;
&lt;h2 id="orgf08cbf6"&gt;Next Steps&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-orgf08cbf6"&gt;
&lt;p&gt;
More advanced Literate Programming techniques may also prove useful when writing Literate API Documentation.
&lt;/p&gt;

&lt;p&gt;
I have one such document I wrote for the &lt;a href="https://wurkzen.com"&gt;Wurkzen API&lt;/a&gt; which allows you to acquire and cache a bearer token used in later requests.
&lt;/p&gt;

&lt;p&gt;
It then creates a number of objects using the API, and then uses them to construct an entire dummy client, customers, locations, and perform all the API actions against dummy data that was created by the document, and then deleted by the document.
&lt;/p&gt;

&lt;p&gt;
And if there are errors in my API, then the API documentation displays the errors! It also acts as an integration test, as detailed as you want to make it.
&lt;/p&gt;

&lt;p&gt;
AND every request made by Emacs as it exports to HTML is displayed in Emacs &lt;code&gt;minibuffer&lt;/code&gt; and logged in Emacs &lt;code&gt;*Messages*&lt;/code&gt; buffer, so you can find errors with an incremental search instead of reading the &lt;code&gt;index.html&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Literate Programming: beautiful, elegant, powerful.
&lt;/p&gt;

&lt;p&gt;
And useful!
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org798cd9e" class="outline-2"&gt;
&lt;h2 id="org798cd9e"&gt;P.S.&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-org798cd9e"&gt;
&lt;p&gt;
I ended up &lt;a href="https://gist.github.com/joseph8th/9a87c4f14b867c1abe8d2e81322f4eac#file-lit-api-method-section"&gt;writing a yasnippet for this&lt;/a&gt; anyway.
&lt;/p&gt;

&lt;p&gt;
Type in &lt;code&gt;lit-api&lt;/code&gt; and &lt;code&gt;C-&amp;lt;tab&amp;gt;&lt;/code&gt; and then tab from field to field.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;</description><category>api</category><category>emacs</category><category>literate programming</category><category>org-mode</category><guid>https://blog.notroot.online/posts/writing-literate-api-documentation-in-emacs-org-mode/</guid><pubDate>Wed, 26 Jan 2022 00:48:54 GMT</pubDate></item></channel></rss>