{"id":55662,"date":"2022-10-03T16:43:25","date_gmt":"2022-10-03T11:13:25","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55662"},"modified":"2022-10-03T16:43:25","modified_gmt":"2022-10-03T11:13:25","slug":"how-to-create-custom-rest-api","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-create-custom-rest-api\/","title":{"rendered":"How to create custom Rest Api"},"content":{"rendered":"<div>\n<p>In this tutorial, we will learn how to create a custom RESTful Web Service in Drupal 9.<\/p>\n<p>Before creating Rest Api, we need to know what REST API is. An application programming interface (API or web API) allows interaction with RESTful web services. Where REST is known as Representational State Transfer.<\/p>\n<p><strong style=\"font-size: 1rem;\">First, <\/strong><span style=\"font-size: 1rem;\">We will need to install these core modules HTTP Basic Authentication, REST Web Services, and serialization module.<\/span><\/p>\n<\/div>\n<p>Then install Contributed module called <strong>REST UI, <\/strong>which you can download from<br \/>\nthe composer requiring &#8216;drupal\/restui:^1.21&#8217;<\/p>\n<div><\/div>\n<div><\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-55653\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-20-41-10-1024x525.png\" alt=\"\" width=\"625\" height=\"320\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-20-41-10-1024x525.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-20-41-10-300x154.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-20-41-10-768x393.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-20-41-10-624x320.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-20-41-10.png 1052w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/div>\n<div><\/div>\n<p>Let&#8217;s create a custom module named &#8220;<strong>Rest Example<\/strong>&#8221; in \/modules\/custom<\/p>\n<div><\/div>\n<div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55661\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-23-33-46.png\" alt=\"\" width=\"236\" height=\"163\" \/><\/div>\n<\/div>\n<p>After that create <strong>ExampleRest.php <\/strong><\/p>\n<p>in directory \/modules\/custom\/rest_example\/src\/Plugin\/rest\/resource\/<strong>ExampleRest<\/strong><strong>.php<\/strong>.<\/p>\n<p>This is our custom resource file which will have get method that will get all nodes of content type Football Teams.<\/p>\n<div><\/div>\n<div><\/div>\n<div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-55654\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-46-46-1024x446.png\" alt=\"\" width=\"625\" height=\"272\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-46-46-1024x446.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-46-46-300x131.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-46-46-768x335.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-46-46-624x272.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-46-46.png 1235w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/div>\n<\/div>\n<div>\n<div>\n<pre>\/**\r\n* Provides a Rest Resource Example\r\n*\r\n* @RestResource(\r\n* id = \"rest_example\",\r\n* label = @Translation(\"Rest Example Resource\"),\r\n* uri_paths = {\r\n* \"canonical\" = \"\/rest_example\"\r\n* }\r\n* )\r\n*\/<\/pre>\n<p>Let\u2019s understand annotation.<\/p>\n<\/div>\n<ul>\n<li><strong>@RestResource<\/strong> is a function, through which know, it\u2019s a kind of Rest Resource<\/li>\n<li><strong>id<\/strong>\u00a0define the id of Rest Resource API<\/li>\n<li><strong>label<\/strong>\u00a0displaying to the user through which author can know about this api<\/li>\n<li><strong>uri_paths<\/strong>\u00a0:\u00a0<strong>canonical<\/strong>\u00a0attributes set the path of API.<\/li>\n<\/ul>\n<\/div>\n<p>In our case, we will use\u00a0 &#8220;sitename<strong>\/rest_example&#8221; <\/strong>to hit our API.\u00a0After this, we need to install our module.<\/p>\n<p>Then go to <strong>\/admin\/config\/services\/rest\u00a0<\/strong>and enable our custom resource <strong>Rest Example Resource<\/strong><\/p>\n<div><\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-55656\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-21-57-1-1024x170.png\" alt=\"\" width=\"625\" height=\"104\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-21-57-1-1024x170.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-21-57-1-300x50.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-21-57-1-768x127.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-21-57-1-624x103.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-21-57-1.png 1213w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/div>\n<div><\/div>\n<p>After that, select the following options<\/p>\n<div><\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-55657\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-54-47-1024x455.png\" alt=\"\" width=\"625\" height=\"278\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-54-47-1024x455.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-54-47-300x133.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-54-47-768x341.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-54-47-624x277.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-54-47.png 1214w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/div>\n<div><\/div>\n<p>Further, allow anonymous permission to our rest resource.<\/p>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-55658\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-19-1024x217.png\" alt=\"\" width=\"625\" height=\"132\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-19-1024x217.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-19-300x64.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-19-768x163.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-19-624x132.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-19.png 1244w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/div>\n<div><\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-55659\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-26-1024x227.png\" alt=\"\" width=\"625\" height=\"139\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-26-1024x227.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-26-300x67.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-26-768x170.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-26-624x138.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-22-36-26.png 1325w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/div>\n<div><\/div>\n<div><\/div>\n<p>Now we will hit our get resource.<\/p>\n<div><\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55660\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-23-09-12.png\" alt=\"\" width=\"960\" height=\"637\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-23-09-12.png 960w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-23-09-12-300x199.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-23-09-12-768x510.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/09\/Screenshot-from-2022-09-30-23-09-12-624x414.png 624w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how to create a custom RESTful Web Service in Drupal 9. Before creating Rest Api, we need to know what REST API is. An application programming interface (API or web API) allows interaction with RESTful web services. Where REST is known as Representational State Transfer. First, We will need [&hellip;]<\/p>\n","protected":false},"author":1501,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":164},"categories":[3602],"tags":[1832,4862,226],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55662"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1501"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55662"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55662\/revisions"}],"predecessor-version":[{"id":55665,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55662\/revisions\/55665"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}