Bonus: REST, RESTful and web APIs

Before we talk about the API that we are going to use, we'll very briefly review the main ideas behind the RESTful approach. Entire books are dedicated to this architectural style, but we will try to convey the essence of what it is in a few paragraphs. So, please, bear with us!

REpresentational State Transfer (REST) is an architectural style for designing web services, which is heavily centered on the notion of resources that is core to the World Wide Web (WWW). The REST architectural style was defined by Roy Fielding in his Ph.D. dissertation (https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm). Interestingly, Mr. Fielding is also one of the main authors of the HTTP specifications.

The main goals of REST are to create loosely coupled, scalable, and efficient applications with simple, consistent, flexible, and easy-to-use interfaces.

To realize these goals, REST defines a set of principles and constraints:

These are the main constraints that a REST API must adhere to. An API that does not respect all of these constraints is not a truly RESTful API, but rather a web API or REST-like API.

An important point to keep in mind, though, is that REST is NOT a standard. As a matter of fact, there are probably as many different variants of REST APIs as there are APIs. We won't enter into this debate here though; for the rest of this book, we'll talk about REST, RESTful, and web APIs interchangeably (apologies to the purists).

RESTful APIs usually rely on the HTTP protocol and they try to make the best use of it. For the rest of our explanation, we'll assume HTTP-based REST APIs.

Resources are concrete, high-level concepts (for example, cars, employees, bills) using nouns and lower/kebab case (for example, /api/v1/current-deals). Each resource is associated with a specific URL (for example, /api/v1/cars). If you know how to access one resource type, then accessing another should be easy if the uniformity constraint is respected (for example, /api/v1/employees).

The plural form is used because, usually, resources are collections. For example, we could retrieve the first car in the preceding example at the following URL: /v1/cars/1.

Depending on the considered APIs and domain models, resources can have links to other resources and can have subresources, actions, and so on. For example, assuming that cars have passengers, we could retrieve those passengers via a call to /api/v1/cars/1/passengers. Again, in this case, passengers would be a collection.

URIs have the following structure: URI = scheme "://" authority "/" path [ "? query ] [ "#" fragment ]

Here's an example: https://www.foo.bar/hello-world?param1=value1&param2=value2#section1

Here are the most important parts of URLs:

In addition, here are some points to be aware of regarding resource URLs:

To manipulate resources, RESTful APIs make use of the different HTTP methods/verbs:

Usually, RESTful APIs expose data as JSON, less often so as XML, and sometimes even both (for example, using parameters to select the format).

HTTP status codes are used to indicate the result of the different operations. There are only a few categories:

There are many HTTP status codes, so we can't go through them all. Still, here are a few important ones to know about:

Finally, RESTful APIs also rely a lot on standard HTTP headers (for example, Accept, Content-Type, Encoding, Cache-Control, ETag, and many more).

There is really a lot more to know about REST APIs, but we'll stop here as it should be just about enough for our purposes.