{"id":1819,"date":"2021-06-25T05:17:07","date_gmt":"2021-06-25T05:17:07","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/solved-failed-to-load-api-definition-in-magento-2-swagger\/"},"modified":"2025-05-22T10:24:18","modified_gmt":"2025-05-22T04:54:18","slug":"solved-failed-to-load-api-definition-in-magento-2-swagger","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/solved-failed-to-load-api-definition-in-magento-2-swagger\/","title":{"rendered":"Solved: Failed to Load API Definition in Magento 2 Swagger"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">While developing&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 extensions<\/a>, we often need to create a custom API to pass the data that has been fetched from the client. For example, while working with order tracking, we need to get the order id and email id from the customer and then pass it through API to perform further operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to maintain the standards of coding in API, we mostly prefer to add phpdoc in the file and if the editor is PHPStorm then we use the Ctrl+Alt+Ins shortcut key to do so.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The phpdoc contains code that seems like a comment but anyhow it affects the output in API creation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you\u2019ve not added the variable\u2019s datatype in phpdoc of API, it generates an error of&nbsp;<em><strong>failed to<\/strong><\/em>&nbsp;<em><strong>load API definition in Magento 2 swagger,<\/strong><\/em><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><em><strong>Failed to Load API Definition<\/strong><\/em><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">and displays in the swagger as below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-at-May-25th-2021-5.55.10-pm.png\" alt=\"https:\/\/drops.meetanshi.com\/zzdopo\" class=\"wp-image-15277\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">So, the solution to this error is to add the missing datatype in the code as mentioned below:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution to Failed to Load API definition in Magento 2 Swagger<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When we add phpdoc in API creation it adds like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n    * @param $orderId\n    * @param $mailId\n    * @return mixed\n*\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the above code with:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n    * @param int $orderId\n    * @param mixed $mailId\n    * @return mixed\n*\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The solution is simple but difficult to identify!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While developing&nbsp;Magento 2 extensions, we often need to create a custom API to pass the data that has been fetched from the client. For example,&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1819","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1819"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1819\/revisions"}],"predecessor-version":[{"id":14282,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1819\/revisions\/14282"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}