Rest API Design
Important aspects of API design:
Naming: Use Nouns to represents resources not verbs.
Eg:
Leverage logical grouping:
Customer Orders:
Avoid DB structuring or any hints in the resource URL that allows attackers to idea of something.
Order back to the customer:
{99} is Order ID
Implementing this can become cumbersome. A better solution is to provide navigable links to
associated resources.
Solution is HATEOAS: The term hypermedia refers to any content that contains links to other forms of
media such as other APIs, images, movies, and text. In HATEOAS, we insert the hypermedia links in
the API response contents.
These hypermedia links allow the client to dynamically navigate to the appropriate resources by
traversing the links.
Use pluralized nouns for resources:
Collection is a Group of Resources:
For eg:
/orders is a group of collection
/orders/234 is a resource with a specific information about specific order.
Don’t go or design Deeper collections.
“collection/resource/collection”
Use Hyphens to increase the readability of the url:
For eg:
Inventory-management/
Don’t change anything on existing Rest endpoint or in existing API response as many people uses it.
Have a version number in the URL such as.
Alternatively, rather than providing multiple versions in the URL we can also send the version as a
parameter.
Filtering, Sorting and pagination:
Generally, REST Apis fetch data from Databases so don’t think to fetch all the data at once.
Ways to filter items:
With specific key and value
Pagination:
A HEAD request is similar to the GET request but the HEAD request returns Headers of the response
with 200 status.