<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jared Hughes</title>
    <description>The latest articles on DEV Community by Jared Hughes (@jaredhughes).</description>
    <link>https://dev.to/jaredhughes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F181538%2F00467329-1d1c-4323-991b-50e6d09fba99.png</url>
      <title>DEV Community: Jared Hughes</title>
      <link>https://dev.to/jaredhughes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jaredhughes"/>
    <language>en</language>
    <item>
      <title>Making a useful REST API Specification</title>
      <dc:creator>Jared Hughes</dc:creator>
      <pubDate>Wed, 21 Dec 2022 07:02:33 +0000</pubDate>
      <link>https://dev.to/jaredhughes/making-a-useful-rest-api-specification-2p8c</link>
      <guid>https://dev.to/jaredhughes/making-a-useful-rest-api-specification-2p8c</guid>
      <description>&lt;p&gt;Getting people to work well with your API is hard work, and documenting it is perhaps the best way to do so. A popular way to document REST APIs is through a machine-readable specification such as OpenAPI. This comes with a few benefits, mostly that there is widespread tooling for generating and transforming OpenAPI specifications. For example, there are tools for documentation generators, mock servers, and so much more.&lt;/p&gt;

&lt;p&gt;This is not a tutorial for REST APIs or OpenAPI and surrounding tooling: see other tutorials to learn the specification language and tooling. This post is about making your OpenAPI specifications properly useful to help developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip 1: Precise types
&lt;/h2&gt;

&lt;p&gt;This should go without saying, but the types inside your API specification should be specific in order to be useful. The main way I see this appear is that some specification generators seem to default to "string" for everything, even if something else makes sense. In an analysis of 1154 specifications from &lt;a href="https://github.com/APIs-guru/openapi-directory"&gt;OpenAPI directory&lt;/a&gt;, I found that 60% of the field types were strings. Many of these were instances that made sense, such as IDs or names, but many were misused types: for example, there was a year value encoded with type "string," and a boolean value with type "string" and enum "true" or "false". Using too-broad types such as strings can make it harder to understand the specification, and decrease the effectiveness of certain tools.&lt;/p&gt;

&lt;p&gt;For example, see the following snippet from the specification for the &lt;a href="https://github.com/APIs-guru/openapi-directory/blob/3a6ef952c6ace9257716a554852d5e151f3ad899/APIs/bbc.com/1.0.0/openapi.yaml#L3781"&gt;bbc.com REST API&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;embargoed&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;enum&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;true"&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;false"&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It uses the string "true" where a boolean value could be more appropriate, which can lead to confusion as other fields might properly use booleans.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip 2: Examples
&lt;/h2&gt;

&lt;p&gt;Only 10% of OpenAPI specifications provide examples directly inside the API, based on the same analysis as before. But embedded examples are huge for readability: they help people get started with the API immediately.&lt;/p&gt;

&lt;p&gt;As a demonstration, consider the following specification for a price schema (modified from the amadeus API). It defines two properties &lt;code&gt;base&lt;/code&gt; and &lt;code&gt;currency&lt;/code&gt;, each with string type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Price&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;base&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Amount without taxes&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;
    &lt;span class="na"&gt;currency&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a consumer of this API, it’s not immediately clear how to fill in this schema. Of course, precise types would help: the base amount could be matched with a regex pattern specifying valid currency quantities, and the currency could be selected from an enum of valid currencies.&lt;/p&gt;

&lt;p&gt;But consider a consumer trying to understand the API: It is better to have examples so they can try calling the API quickly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Price&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;base&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Amount without taxes&lt;/span&gt;
      &lt;span class="na"&gt;example&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;632.70"&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;
    &lt;span class="na"&gt;currency&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;example&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;USD"&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, with the examples filled in, the consumer of the REST API understands the types better. The currency field could be a currency code like "USD" or "EUR," and the amount is a string representation of a decimal number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;To make an OpenAPI specification useful for documenting a REST API, it is important to use precise types and include examples. Using precise types helps to clearly define the data being used in the API, and examples provide a way for developers to quickly understand how to use the API. By following these best practices, you can create a useful and effective REST API specification.&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
