{"id":775,"date":"2020-01-21T09:48:22","date_gmt":"2020-01-21T09:48:22","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/01\/21\/create-custom-rest-api-in-magento-2\/"},"modified":"2025-05-21T17:54:58","modified_gmt":"2025-05-21T12:24:58","slug":"create-custom-rest-api-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-custom-rest-api-in-magento-2\/","title":{"rendered":"How to Create Custom Rest API in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/developer.adobe.com\/commerce\/docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">Rest API in Magento 2<\/a> defines a set of functions used by the developers to perform requests and get responses using the HTTP protocol. By default, Magento 2 provides some predefined rest APIs like Product API, Order API, Customer data API with which you can take virtual control of everything happening on the site. You can read our complete guide on <a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a> if you are just getting started.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Even though Magento 2 has these many rest APIs, they are not enough when it comes to using the custom data and their values. To manage your custom data and fields, you need to create custom rest API in Magento 2 and today, I have come up with the steps for the same \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Read:<\/strong> <a href=\"https:\/\/meetanshi.com\/blog\/create-custom-magento-2-theme\/\">How to Create a Custom Theme in Magento 2?<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Create Custom Rest API in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create module.xml at <strong>app\/code\/Meetanshi\/CustomApi\/etc\/module.xml <\/strong>with the below code:<\/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=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Meetanshi_CustomApi\" setup_version=\"1.0.0\" \/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;2. Create registration.php at <strong>app\/code\/Meetanshi\/CustomApi\/registration.php<\/strong> and paste the below code:<br><\/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=\"\">&lt;?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Meetanshi_CustomApi',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create webapi.xml at <strong>app\/code\/Meetanshi\/CustomApi\/etc\/webapi.xml<\/strong> with the below code:<\/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=\"\">&lt;?xml version=\"1.0\"?>\n&lt;routes xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/app\/code\/Magento\/Webapi\/etc\/webapi.xsd\">\n    &lt;route method=\"POST\" url=\"\/V1\/custom\/custom-api\/:value\">\n        &lt;service class=\"Meetanshi\\CustomApi\\Api\\CustomInterface\" method=\"getPost\"\/>\n        &lt;resources>\n            &lt;resource ref=\"anonymous\"\/>\n        &lt;\/resources>\n    &lt;\/route>\n&lt;\/routes><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create di.xml at <strong>app\/code\/Meetanshi\/CustomApi\/etc\/di.xml<\/strong> with the below code:<\/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=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;preference for=\"Meetanshi\\CustomApi\\Api\\CustomInterface\" type=\"Meetanshi\\CustomApi\\Model\\Api\\Custom\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;5. Create CustomInterface.php in path <strong>app\/code\/Meetanshi\/CustomApi\/Api\/CustomInterface.php<\/strong><\/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=\"\">&lt;?php\n\nnamespace Meetanshi\\CustomApi\\Api;\n\ninterface CustomInterface\n{\n    \/**\n     * GET for Post api\n     * @param string $value\n     * @return string\n     *\/\n\n    public function getPost($value);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;6. Create Custom.php in path <strong>app\/code\/Meetanshi\/CustomApi\/Model\/Api\/Custom.php<\/strong><\/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=\"\">&lt;?php\n\nnamespace Meetanshi\\CustomApi\\Model\\Api;\n\nuse Psr\\Log\\LoggerInterface;\n\nclass Custom\n{\n    protected $logger;\n\n    public function __construct(\n        LoggerInterface $logger\n    )\n    {\n\n        $this->logger = $logger;\n    }\n\n    \/**\n     * @inheritdoc\n     *\/\n\n    public function getPost($value)\n    {\n        $response = ['success' => false];\n\n        try {\n            \/\/ Your Code here\n            \n            $response = ['success' => true, 'message' => $value];\n        } catch (\\Exception $e) {\n            $response = ['success' => false, 'message' => $e->getMessage()];\n            $this->logger->info($e->getMessage());\n        }\n        $returnArray = json_encode($response);\n        return $returnArray; \n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, run the setup upgrade and deploy commands and you are done with creating a custom rest API in Magento 2. You can check the created custom rest API using&nbsp;[webiste\/domain]\/swagger<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/01\/custom-rest-api-in-magento-2.png\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/01\/custom-rest-api-in-magento-2.png\" alt=\"Custom Rest API in Magento 2\" class=\"wp-image-7388\"\/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Don&#8217;t forget to share this simple guide to help your fellow Magento developers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rest API in Magento 2 defines a set of functions used by the developers to perform requests and get responses using the HTTP protocol. By&#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-775","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/775","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=775"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":13998,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/775\/revisions\/13998"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}