There is no manual that says: this is how you define a REST API. Fortunately, we have developed REST so many times that we now know all the ins and outs. We do not want to keep that knowledge from you, which is why in this blog article we give you five tips for defining a good REST API.
By robust we mean that an API does not collapse, stutter or slow down when there is suddenly more traffic to your website, for example. An API has to keep working quickly and well and be scalable. So when building your REST API, you think about what happens to your data and how much data you share. These days we all expect a web page to load within a few seconds. Caching helps with this. Using a unique identifier, the API sees whether the data you have is still recent or whether an update has to take place. So when there is more traffic or a large amount of data is shared, a scalable and robust REST API makes sure the data exchange takes no effort.
An API has to be built logically. That means you have to think about how you structure your data, because the structure of your data determines how you shape your API, so that you get the right information back. What collections of information do you have, and how will you divide up your REST API? A collection consists of customers, for example, and a subcollection of a customer contains address details, a profile photo and a phone number. HTTP methods such as GET, PUT, POST and DELETE are also important here to perform the right action.
What if an action goes wrong? You give errors a certain structure too. First, to see that something is going wrong, and second, to find out what exactly is not working as it should. Everyone knows the 404 page: the error message you get back when you want to visit a web page that no longer exists. An error code is useful not only for the end user, but also for the administrator. You get insight into where and why a request goes wrong.
Structure also makes for a fast REST API and application. JSON gives structure to your data. Say an end user requests a customer file consisting of 3000 customers. You do not want to give them that gigantic amount of information all at once, but in doses. As you scroll through that data, more data is gradually added (dynamic content). An appropriate structure for your data thus makes sure you get the right data back quickly, and if something goes wrong, you immediately know what failed and why.
Security is found in several places, for example in front of and behind a login. Must-haves in the area of security are therefore encryption and OpenID Connect. Encryption scrambles data so that it cannot be read by everyone. You do that from the client. Which data a user may view you arrange by building in roles and rights, so by applying authorization.
OpenID makes it possible to log in via another 'organization', for example with your Google, Apple or Facebook account. You want users to be able to get started quickly, and having to create yet another account for your service can be a barrier. With OpenID you offer the end user convenience, but also a secure way of requesting data with authentication.
By documenting well, you are prepared for the future. You help other developers with it, but it also makes your API ready for connecting new applications. So as soon as you start working with a REST API, it is useful to think about what you want to achieve with the data you share, who may view which information and how you share the data. OpenAPI Specification is a standard for REST API documentation. You can even display those specifications visually, so that you can test your API easily and in an automated way. By documenting well, using the OpenAPI Specification, your API will last longer!
In the past you defined your API and that was that. Nowadays we keep tinkering with our APIs. That has consequences for how you build your REST API so that it keeps working for your consumers. Say you work with different versions. The first version retrieves customer data in English, but the second version in Dutch. It is then important that your API is stateless, so that two versions of the REST API can run side by side and your consumer is not affected.
Hypermedia as the Engine of Application State (HATEOAS) indicates which versions exist. By default, the latest version is then used. The API sees which collections exist and can then refer from one collection to another. Via a link (hypermedia), the API then retrieves the right data there. That way you can tinker with an API, and the integration does not immediately collapse when something changes.
Want to watch these tips again? We made a video about it! Do you have any further questions about REST API? Then leave your question here or schedule a time to chat with one of our API specialists.