REST stands for Representational State Transfer.
Any API (Application Programming Interface) that follows the REST design principle is said to be RESTful.
Simply put, a REST API is a medium for two computers to communicate over HTTP (Hypertext Transfer Protocol), in the same way clients and servers communicate.
Use JSON as the Format for Sending and Receiving Data
To ensure the client interprets JSON data correctly, you should set the Content-Type type in the response header to application/json while making the request.
GET would retrieve data, POST will create data, PUT will update data, and DELETE will get rid of the data.
Name Collections with Plural Nouns
So, instead of https://mysite.com/post/123, it should be https://mysite.com/posts/123.
Use Status Codes in Error Handling
STATUS CODE RANGE | MEANING |
---|---|
100 – 199 | Informational Responses. For example, 102 indicates the resource is being processed |
300 – 399 | Redirects For example, 301 means Moved permanently |
400 – 499 | Client-side errors 400 means bad request and 404 means resource not found |
500 – 599 | Server-side errors For example, 500 means an internal server error |
Use Nesting on Endpoints to Show Relationships
For example, in the case of a multi-user blogging platform, different posts could be written by different authors, so an endpoint such as https://mysite.com/posts/author would make a valid nesting in this case.
In the same vein, the posts might have their individual comments, so to retrieve the comments, an endpoint like https://mysite.com/posts/postId/comments would make sense.
You should avoid nesting that is more than 3 levels deep as this can make the API less elegant and readable.
Use Filtering, Sorting, and Pagination to Retrieve the Data Requested
Filtering, sorting, and pagination are all actions that can be performed on the collection of a REST API. This lets it only retrieve, sort, and arrange the necessary data into pages so the server doesn’t get too occupied with requests.
Be Clear with Versioning
https://mysite.com/v1/
https://mysite.com/v2/
References
https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/