RESTful API Guideline (deprecated article)

05 Jan 2019 (last updated on 12 Oct 2020) - written by Hardy Scheel

With the OpenAPI 3.0 defacto-Standard many questions are obsolete for me now. It arrived already in 2018. OpenAPI 3 describes how a RESTful API can be used best. I mark this document obsolete now, because it’s redundant with OpenAPI 2.0 and 3.0. I suggest you to use OpenAPI 3.0 specification for your projects and further learning.

As of the end of 2018 there were many discussions and implementations about REST APIs. Because I need some APIs for some projects, I decided to summon some of the best practices to realize REST APIs. This topic is very huge and the research very time consuming. There is also no standardization, but de facto standards and some very important recommendations that belong to the thematic of API usage and design. This document will be constantly revised. HTTP/1.1 in combination with REST and such similar hacks will still be needed in the future for older devices/clients. HTTP/2 solves many of this workarounds, but it has to be more widely supported by clients.

There are two views of an API. In front (frontend) and behind (backend). We have to take into account these sight of views, especially when we define resources as API endpoints at the backend.

Table of content


Definitions and Terminologies

When designing RESTful APIs the following few terms are the most relevant to understand.


API endpoints

The meaning of RESTful APIs at the API endpoint could be explained as, that the paths declaration at the API endpoints should contain plural forms of resources. And the HTTP methods define the type of action to be performed on the resource. Below are some example API endpoints we use (request) with HTTP methods. As an practical example we use Spaceships which has Astronauts and we use some HTTP methods on these Astronauts:

Let’s combine a resource under a resource where our astronauts belong to certain space ships:

To interact with these APIs we use the form of a natural language. We use collections and also single resources as API endpoints to declare URLs. These resources are nouns and plural. To perform actions on a collection or resource we use the HTTP methods (GET, POST, DELETE, PUT). These methods are our verbs and we combine them. Don’t use /addNewAstronaut as an API endpoint or something similar which has a combination of resource (noun) and method (verb) to interact with the resource. Because the number of APIs will become massive on greater projects. They will also perform redundant actions.


Roles

The API endpoint should never tell the type of data it will provide. Is the request data the data for admins, certain clients or users? The API has to decide if a client call is eligible to perform an action. A role base authorization system has to be implement first. The handler of the API should decide what data can be provided and what actions/methods are allowed. The roles are retrieved from the authentication token, or similar depending on your implemented authorization system. The API take the role of the requesting client and only provides the functionality and data that matches the role.