{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/2021-08-20-rest-api-design/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"REST API Design","date":"2021-08-20"},"html":"<p>In the projects that I have been working on, the frontend communicates with the backend through APIs. The most common pattern that we use is REST API. REST stands for Representational State Transfer, and it is a set of guidelines on how the client and server should communicate.</p>\n<h3>HTTP Methods</h3>\n<p>REST API uses HTTP methods to define what action we want to perform. The most common ones are:</p>\n<p><strong>GET</strong> is used to retrieve data. For example, if we want to get a list of users, we send a GET request to <code class=\"language-text\">/api/users</code>. If we want to get a specific user, we send a GET request to <code class=\"language-text\">/api/users/1</code> where 1 is the user id.</p>\n<p><strong>POST</strong> is used to create new data. If we want to create a new user, we send a POST request to <code class=\"language-text\">/api/users</code> with the user data in the request body.</p>\n<p><strong>PUT</strong> is used to update existing data. If we want to update user with id 1, we send a PUT request to <code class=\"language-text\">/api/users/1</code> with the updated data in the request body.</p>\n<p><strong>DELETE</strong> is used to remove data. If we want to delete user with id 1, we send a DELETE request to <code class=\"language-text\">/api/users/1</code>.</p>\n<h3>Status Codes</h3>\n<p>When the server responds, it sends back a status code to indicate what happened. Some common ones are:</p>\n<p><code class=\"language-text\">200</code> means the request was successful. <code class=\"language-text\">201</code> means a new resource was created successfully. <code class=\"language-text\">400</code> means the request was invalid, usually because the data sent by the client is wrong or missing. <code class=\"language-text\">401</code> means the client is not authenticated. <code class=\"language-text\">404</code> means the resource was not found. <code class=\"language-text\">500</code> means something went wrong on the server side.</p>\n<p>It is important to return the correct status code so the client knows how to handle the response properly.</p>\n<h3>Naming Conventions</h3>\n<p>When designing API endpoints, there are some conventions that are good to follow. Use nouns instead of verbs for the endpoint path. For example, use <code class=\"language-text\">/api/users</code> instead of <code class=\"language-text\">/api/getUsers</code>. The HTTP method already tells us the action, so we do not need to repeat it in the path.</p>\n<p>Use plural nouns for collections, so <code class=\"language-text\">/api/users</code> instead of <code class=\"language-text\">/api/user</code>. And for nested resources, we can chain them like <code class=\"language-text\">/api/users/1/orders</code> to get all orders for user with id 1.</p>\n<h3>Query Parameters</h3>\n<p>For filtering, sorting, or pagination, we can use query parameters. For example, <code class=\"language-text\">/api/users?role=admin</code> to filter users by role, <code class=\"language-text\">/api/users?sort=name</code> to sort by name, or <code class=\"language-text\">/api/users?page=2&amp;limit=10</code> to get the second page with 10 items per page.</p>\n<h3>Versioning</h3>\n<p>As the API grows, there might be cases where we need to change the structure of the response or the behaviour of an endpoint. To avoid breaking existing clients that are still using the old version, we can version our API. A common approach is to include the version in the URL like <code class=\"language-text\">/api/v1/users</code> and <code class=\"language-text\">/api/v2/users</code>.</p>\n<p>This way, clients using v1 will not be affected when we make changes in v2.</p>"}},"pageContext":{"slug":"/2021-08-20-rest-api-design/"}},"staticQueryHashes":[]}