Many giants like Facebook, Google, Github, Netflix, Amazon, and Twitter have their own REST(ful) APIs that you can access to get or even write data.
But why all the need for REST?
Is it that good and why is it so prevalent?
Surely it’s not the only way to convey messages?
What is the difference between REST and HTTP?
Well, it turns out REST is pretty flexible and compatible with HTTP that is the main protocol the internet is based upon. Since it is an architectural style and not the standard, it provides a lot of freedom to implement various design best practices. Did I mention it’s language agnostic?
In this blog post, our goal will be to explain REST as clearly as possible so you can clearly understand when and how to use it, as well as what it is in essence.
We’ll go through some basics and definitions as well as show off some REST API best practices. This should give you all the knowledge you need to implement REST API in any language you prefer to code in.
So, in this post we’ll talk about:
- What Is REST?
- Is REST Bound to HTTP?
- REST and HATEOAS Support
- What Does RESTful API Mean?
- Too Much REST Aka RESTafarian Syndrome
REST API best practices:
- Abstract vs Concrete API
- URI Formatting (Nouns, Not Verbs). Good URL vs Bad URL Examples
- Error Handling
- Status Codes
- Versioning REST API
- Importance of Documentation
REST (Representational State Transfer) is an architectural style founded by Roy Fielding in his Ph.D. dissertation “Architectural Styles and the Design of Network-based Software Architectures” at UC Irvine. He developed it in parallel with HTTP 1.1 (no pressure).
We use REST primarily as a way to communicate between computer systems on the World Wide Web.
By definition, it’s not. Although you can use some other application protocol with REST, HTTP has remained undisputed champion among application protocols when it comes to implementation of REST.
HATEOAS or the Hypermedia As The Engine Of Application State is the important feature of every scalable and flexible REST API.
There are several advantages to using hypermedia:
- Enables API designers rather than to include everything they can in each response, to provide one thing properly and hypermedia links to related endpoints and thus decouple the design
- Helps an API evolve and mature more gracefully
- Provides a user with the means to explore the API more deeply
So it is clear that the HATEOAS was designed with durability in mind.
Here is how GitHub does it:
"name": "Code Maze",
"company": "Code Maze",
"bio": "A practical programmers' resource.",
As you can see, besides the crucial information requested by the client, you can find a bunch of related hypermedia links in the response which lead you to other parts of the API you can freely explore.
“RESTful”, implies a few features:
- Client-server architecture: The complete service is comprised of the “Client” which is the front end and the “Server” which is the backend part of the whole system
- Stateless: The server should not save any states between different requests. The state of the session is exclusively left to the responsibility of the client. As per REST definition: All REST interactions are stateless. That is, each request contains all of the information necessary for a connector to understand the request, independent of any requests that may have preceded it. (Roy’s dissertation ch. 5.2.2)
- Cacheable: The client should be able to store responses in a cache for the greater performance
So, the RESTful API is a service that follows these rules (hopefully) and uses HTTP methods to manipulate the set of resources.
But why do we need or use RESTful APIs?
Because they give us an easy, flexible and scalable way to make distributed applications that communicate over the internet.
Yes, you guessed it. Yes, we can 🙂
There is even a phrase for the people that follow the REST fanatically as defined by Mike Schinkel.
A RESTifarian is a zealous proponent of the REST software architectural style as defined by Roy T. Fielding in Chapter 5 of his PhD. dissertation at UCIrvine. You can find RESTifarians in the wild on the REST-discuss mailing list. But be careful, RESTifarians can be extremely meticulous when discussing the finer points of REST, as I learned recently while participating on the list. 🙂
Too much of anything can be bad.
We need a bit pragmatism to make good applications and services. A theory is important to know and understand, but the implementation of that theory is what differentiate bad vs good vs excellent applications. So be smart, have the end user in mind.
So let’s go some important points that make the API “shine”, and the lives of the users a whole lot easier.
When developing software we often use abstraction and polymorphism to get most of our applications. We want to reuse as much of the code as possible.
So should we write our APIs that way too?
Well, that is not exactly the case with APIs. For the REST APIs, the concrete is better than abstract. Can you guess why?
Let me show you a few examples:
Let’s look at two API versions. Is it better to have an API that has one
/entities or an API that has
Which one seems more descriptive to you as a developer? Which API would you rather use?
I would always choose the second one.
Here is another one of the REST API best practices. How should you format your endpoints?
If you use the software development approach you will end up with something like this:
You get the point… There will be a ton of endpoints, each one doing something else. There is a better system to sort this mess out.
Treat the resource like a noun, and HTTP method as a verb. If you do it like that, you’ll end up with something like this:
GET /blogposts – gets all the blog posts
GET /blogposts/12 – gets the blog post with the id 12
POST /blogposts – adds a new blog post and returns the details
DELETE /blogposts/12 – removes the blog post with the id 12
GET /authors/3/blogposts – gets all the blog posts of the author with id 3
This is cleaner and more precise way to use way to use the API. It is immediately clear to the end user, and there is a method to the madness.
You can make it even cleaner by using singular instead of plural for the resource names. That one is up to you.
Another important aspect of the API building. There are a few good ways to handle the errors.
Let’s see how the top dogs do it:
- Response: Status Code 400
Twitter gives you the Status Code and Error Code with the short description of the nature of the error that occurred. They leave it up to you to look the codes up on their Response Codes page.
- Response: Status Code 400
Facebook gives you a more descriptive error message.
- Response: Status Code 404
XHTML123456789<?xml version='1.0' encoding='UTF-8'?><TwilioResponse><RestException><Code>20404</Code><Message>The requested resource /2010-04-01/Accounts/1234/IncomingPhoneNumbers/1234 was not found</Message><MoreInfo>https://www.twilio.com/docs/errors/20404</MoreInfo><Status>404</Status></RestException></TwilioResponse>
Twilio gives you XML response by default and the link to the documentation where you can find the error details.
As you can see, the approaches to error handling differ from the implementation to the implementation.
The important thing is not to leave the user of the REST API “hanging”, not knowing what happened or aimlessly wandering through the wastes of StackOverflow searching for the explanation.
When designing a REST API, we communicate with the API user by utilizing HTTP Status Codes. There are a lot of status codes, describing multiple possible responses.
But just how many should we use? Should we have a strict status code for every situation?
As with many things in life, the KISS principle applies here too. There are over 70 status codes out there. Do you know them by heart? Will the potential API user know them all, or will it once again result in googling stuff?
Most developers are familiar with the most common status codes:
400 Bad Request
500 Internal Server Error
By starting with these three, you can cover most of the functionalities of your REST API.
Other commonly seen codes include:
204 No Content
404 Not Found
We can use these to help the user figure out quickly what the result was. We should probably include some kind of message if you feel the status code is not descriptive enough like we discussed in the Error handling section. Once again, we need to be pragmatic, help the user by using a limited number of codes and descriptive messages.
You can find the complete HTTP Status codes list, as well as other helpful HTTP stuff here, summarized on CodeMaze.
Every request should be made over HTTPS.
There are many tricks to improve the security of your REST API, but you must be cautious when implementing them, because of the stateless nature of REST. Remembering the state of the last request goes out of the window, and the client is where the state should be stored and verified.
Timestamping and logging requests can help a bit too.
There is much more to be said on this topic, but it is out of the scope of this post. We have a nice post on HTTP Security here on CodeMaze if you want to learn more about that.
You’ve already written your REST API and it has been very successful and many people have used it and are happy with it. But you have that juicy new functionality that breaks other parts of the system. The breaking change.
Never fear, there is a solution for that!
Before we start making your API, we can version it by prefixing the endpoints with the API version:
This way we can always increment our API version number (eg. v2, v3…) whenever there are breaking changes in our API. This also signals the users something drastic has changed and they should be careful when using the new version.
This one is the no-brainer. You could be the best API designer in the world, but without documentation, your API is as good as dead.
Proper documentation is essential for every software product and web service alike.
We can help the user by being consistent and using clear and descriptive syntax, sure. But there is no real replacement for the good ol’ documentation pages.
Some of the great examples:
and many others…
There are many tools that can help you document your API, but don’t forget to add the human touch, only one human can properly understand another. For now at least (looking at you AI). 🙂
We went through many concepts of the REST API building and covered some of the top REST API best practices. These might seem a bit strange or overwhelming when served at once, but try making your own REST API. And try to implement some the REST API best practices you learned here.
Make the tiniest API possible and see how it looks. You’ll be surprised how well it can turn out by just following these few practices.
We have an ASP.NET Core Web Api series in which we demonstrate these practices. Also if you are a C# developer check out our article on how to consume RESTful APIs in a few different ways.
And with this said, I REST my case… kmhmh… because you know, on the court… oh nevermind :/