- Create posts
- Read (retrieve) posts
- Update posts
- Delete posts
Following established API conventions not only improves code readability but also aids in maintaining consistency across your application.
API Endpoint Conventions
The following best practices are recommended when designing your API endpoints:-
Use plural nouns for resource paths
- For posts, use
/postsinstead of/post. - For users, use
/usersinstead of/user.
- For posts, use
-
Create Operation
Use HTTP POST to create a new resource. For example, in FastAPI, define the endpoint for creating a post as follows: -
Read Operation
There are two scenarios for reading data:-
To retrieve multiple posts (or all posts), use a GET request to
/posts: -
To retrieve a specific post, use a GET request to
/posts/{id}. Here,{id}represents the unique identifier generated by the database:
-
To retrieve multiple posts (or all posts), use a GET request to
-
Update Operation
The update operation allows you to modify an existing post (e.g., change the title or content). There are two HTTP methods available:- PUT: Requires all resource fields—even if some remain unchanged.
- PATCH: Requires only the fields that need to be updated.
-
Delete Operation
To remove a post based on its unique ID, use the DELETE request:
The above endpoints ensure that your API remains consistent and easy to understand. Once you establish a CRUD API for one resource, implementing another typically involves replicating and modifying these routes.
Consolidated CRUD Example
Below is a complete example of CRUD endpoints for the posts resource:/createpost instead of /posts, we will update it to match the conventions outlined above.
For further details and code examples, refer back to this guide as you continue to develop your API.