{"id":1817,"date":"2021-06-28T07:22:56","date_gmt":"2021-06-28T07:22:56","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/perform-crud-operations-using-api-in-magento-2\/"},"modified":"2025-07-16T17:39:03","modified_gmt":"2025-07-16T12:09:03","slug":"perform-crud-operations-using-api-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/perform-crud-operations-using-api-in-magento-2\/","title":{"rendered":"How to Perform CRUD Operations Using API in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">One of the most commonly required operations in the Magento 2 store while managing a database is to perform CRUD operations. CRUD stands for Create, Read, Update and Delete.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2\u00a0offers the collection to manage data in a database easily. That\u2019s why we don\u2019t have to write many lines of code to create a CRUD.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, what if we want to perform such kinds of operations based on API requests or integrate API on the frontend?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, you need to send data from other platforms\u2019 registration forms to the Magento database or integrate API in mobile. For such requirements, we have to call an API that sends a request to the server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In such scenarios, use the below code to&nbsp;<strong><em>perform CRUD operation using API in Magento 2<\/em><\/strong>!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Perform CRUD Operations Using API in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>module.xml<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/&nbsp;<\/strong>and use 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=\"Vendor_Module\" setup_version=\"1.0.0\" \/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>registration.php<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/<\/strong>&nbsp;and paste 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;?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Vendor_Module',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>webapi.xml<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/&nbsp;<\/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\"\n        xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/app\/code\/Magento\/Webapi\/etc\/webapi.xsd\">\n    &lt;route method=\"POST\" url=\"\/V1\/custom\/custom-api\/\">\n        &lt;service class=\"Vendor\\Module\\Api\\CustomInterface\" method=\"getPost\"\/>\n        &lt;resources>\n            &lt;resource ref=\"anonymous\"\/>\n        &lt;\/resources>\n    &lt;\/route>\n    &lt;route method=\"GET\" url=\"\/V1\/custom\/getdata\/\">\n        &lt;service class=\"Vendor\\Module\\Api\\CustomInterface\" method=\"getData\"\/>\n        &lt;resources>\n            &lt;resource ref=\"anonymous\"\/>\n        &lt;\/resources>\n    &lt;\/route>\n    &lt;route method=\"GET\" url=\"\/V1\/custom\/delete\/\">\n        &lt;service class=\"Vendor\\Module\\Api\\CustomInterface\" method=\"getDelete\"\/>\n        &lt;resources>\n            &lt;resource ref=\"anonymous\"\/>\n        &lt;\/resources>\n    &lt;\/route>\n    &lt;route method=\"GET\" url=\"\/V1\/custom\/getbyid\/\">\n        &lt;service class=\"Vendor\\Module\\Api\\CustomInterface\" method=\"getById\"\/>\n        &lt;resources>\n            &lt;resource ref=\"anonymous\"\/>\n        &lt;\/resources>\n    &lt;\/route>\n    &lt;route method=\"POST\" url=\"\/V1\/custom\/edit\/\">\n        &lt;service class=\"Vendor\\Module\\Api\\CustomInterface\" method=\"getEdit\"\/>\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&nbsp;<strong>di.xml<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/&nbsp;<\/strong>and use 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=\"Vendor\\Module\\Api\\CustomInterface\" type=\"Vendor\\Module\\Model\\Api\\Custom\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5. Create&nbsp;<strong>CustomInterface.php<\/strong>&nbsp;at&nbsp;<strong><strong>app\/code\/Vendor\/Module\/Api\/<\/strong><\/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 Vendor\\Module\\Api;\n\n\ninterface CustomInterface\n{\n    \/**\n     * GET for Post api\n     * @param string $title\n     * @return string\n     *\/\n    public function getPost($title);\n\n    \/**\n     * @return string\n     *\/\n    public function getData();\n\n    \/**\n     * @param int $id\n     * @return bool true on success\n     *\/\n    public function getDelete($id);\n\n    \/**\n     * @param int $id\n     * @return string\n     *\/\n    public function getById($id);\n\n    \/**\n     * GET for Post api\n     * @param string $title\n     * @return string\n     *\/\n    public function getEdit($title);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">6. Create&nbsp;<strong>Custom.php<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/Model\/Api\/&nbsp;<\/strong>and use 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;?php namespace Vendor\\Module\\Model\\Api;\n\nuse Vendor\\Module\\Api\\CustomInterface;\nuse Vendor\\Module\\Model\\ModuleFactory;\n\nclass Custom implements CustomInterface\n{\n    private $moduleFactory;\n    private $quote;\n    protected $response = ['success' => false];\n\n    public function __construct(moduleFactory $moduleFactory, \\Magento\\Quote\\Model\\Quote $quote)\n    {\n        $this->quote = $quote;\n        $this->moduleFactory = $moduleFactory;\n    }\n\n    \/** * GET for Post api * @param string $title * @return string *\/\n    public function getPost($title)\n    {\n        $insertData = $this->moduleFactory->create();\n        try {\n            $insertData->setTitle($data['title'])->save();\n            $response = ['success' => true, 'message' => $data];\n        } catch (\\Exception $e) {\n            $response = ['success' => false, 'message' => $e->getMessage()];\n        }\n        return $response;\n    }\n\n    \/** * @return string *\/\n    public function getData()\n    {\n        try {\n            $data = $this->moduleFactory->create()->getCollection()->getData();\n            return $data;\n        } catch (\\Exception $e) {\n            return ['success' => false, 'message' => $e->getMessage()];\n        }\n    }\n\n    \/** * @param int $id * @return bool true on success *\/\n    public function getDelete($id)\n    {\n        try {\n            if ($id) {\n                $data = $this->moduleFactory->create()->load($id);\n                $data->delete();\n                return \"success\";\n            }\n        } catch (\\Exception $e) {\n            $response = ['success' => false, 'message' => $e->getMessage()];\n        }\n        return \"false\";\n    }\n\n    \/** * @param int $id * @return string *\/\n    public function getById($id)\n    {\n        try {\n            if ($id) {\n                $data = $this->moduleFactory->create()->load($id)->getData();\n                return ['success' => true, 'message' => json_encode($data)];\n            }\n        } catch (\\Exception $e) {\n            return ['success' => false, 'message' => $e->getMessage()];\n        }\n    }\n\n    \/** * GET for Post api * @param string $title * @return string *\/\n    public function getEdit($title)\n    {\n        $edit = file_get_contents(\"php:\/\/input\");\n        $data = json_decode($edit, true);\n        $insertData = $this->moduleFactory->create();\n        $id = $data['id'];\n        if ($id) {\n            try {\n                $insertData->load($id);\n                $insertData->setTitle($data['title'])->save();\n                $response = ['success' => true, 'message' => $data];\n            } catch (\\Exception $e) {\n                $response = ['success' => false, 'message' => $e->getMessage()];\n            }\n        }\n        return response;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also read:<\/strong>&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/alternative-to-magento-2-deprecated-load-save-and-delete-methods\/\" target=\"_blank\" rel=\"noreferrer noopener\">Alternative to Magento 2 Deprecated Load, Save and Delete Methods<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post 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>One of the most commonly required operations in the Magento 2 store while managing a database is to perform CRUD operations. CRUD stands for Create,&#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-1817","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1817","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=1817"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1817\/revisions"}],"predecessor-version":[{"id":18042,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1817\/revisions\/18042"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}