Monday, February 14, 2022

RESTful API - Best Practices

RESTful API Designing Guidelines — The Best Practices

API Endpoint

Let’s write a few APIs for companies that have some employees, to understand more.

The /companies endpoint is a good example that contains no action. But the question is: “How do we tell the server about the actions to be performed on the companies resource and whether to add, delete, or update?”

  • The method GET path /companies/34 should get the details of company 34.
  • The method DELETE path /companies/34 should delete company 34.

  • GET /companies/3/employees/45 should get the details of employee 45, who belongs to company 3.
  • DELETE /companies/3/employees/45 should delete employee 45, who belongs to company 3.
  • POST /companies should create a new company and return the details of the new company created.

Searching, Sorting, Filtering, and Pagination

All of these actions are simply the query on one dataset. There will be no new set of APIs to handle these actions. We need to append the query params with the GET method API.

  • Filtering — For filtering the dataset, we can pass various options through query params. E.g. GET /companies?category=banking&location=india would filter the company’s list data with the company category of banking and where the location is: India.
  • Searching — When searching the company name in the company list, the API endpoint should be GET /companies?search=Digital Mckinsey.
  • Pagination — When the dataset is too large, we divide the data set into smaller chunks, which helps in improving performance and it is easier to handle the response. E.g. GET /companies?page=23 means to get the list of companies on the 23rd page.

No comments:

Post a Comment