9 Tweets Apr 13, 2023
API routes structure โ€” best practices
Thread ๐Ÿงต๐Ÿ‘‡
The HTTP protocol defines several methods. Let's name the most used ones.
GET - obtains a copy of the resource from the given URI.
POST - creates a new resource at the specified URI
PUT - changes the resource at the given URI with a new one or creates a new one.
PATCH - performs a partial update of a resource
DELETE - Deletes the resource located at the given URI.
To make your API URI structure easy to follow, you should structure it meaningfully.
Let's assume we have an API with endpoints that manage customers.
We'll show different URIs with HTTP methods and their meanings.
1๏ธโƒฃ /customers URI
POST - Create a new customer
GET - Retrieve all customers
PUT - Bulk update of customers
DELETE - Remove all customers
2๏ธโƒฃ /customers/2 URI
In this example, we're going to manipulate a single customer with an ID of 2
POST - Not used
GET - Retrieve the details for the customer with ID=2
PUT - Update the details of the customer with ID=2
DELETE - Remove customer with ID=2
3๏ธโƒฃ /customers/2/orders
If our API uses nested models, we can use the structure mentioned above.
We're going to manipulate orders for the customer with an ID=2
POST - Create a new order for the customer with ID=2
GET - Retrieve all orders for the customer with ID=2
PUT - Bulk update of orders for the customer with ID=2
DELETE - Remove all orders for the customer with ID=2
Thanks for reading!
Follow us @Rapid_API for more exclusive content. ๐Ÿ™๐Ÿ’™

Loading suggestions...