Extending Magento REST API for Custom Data Insertion
In this article, we will guide you through extending the Magento REST API to insert custom data into your own database tables within the Magento system. We’ll build upon the existing Magento REST API and create a custom module that allows you to add new records via the API.
Prerequisites
Before starting, ensure you have:
- A working Magento installation.
- Basic knowledge of XML configuration files and PHP.
- Access to the Magento database for creating tables.
- A REST client (like Postman) or a cURL client for testing the API.
Creating a Custom Table in Database
First, let’s create a simple table that we’ll use as an example:
CREATE TABLE my_module_custom_data (
id INT PRIMARY KEY AUTO_INCREMENT,
attribute1 VARCHAR(255),
attribute2 VARCHAR(255)
);
Replace attribute1 and attribute2 with the actual fields you want to store.
Creating a Custom Module
In your Magento installation’s app/code directory, create a new folder structure:
/my_module
/controllers
V1.php
/etc
api.xml
config.xml
Configuring the API Configuration XML
Open app/code/my_module/etc/api.xml and add the following content to define your endpoint:
<config>
<api>
<resources>
<my_module_custom_data translate="title" module="my_module">
<info key="test">Custom data API for my_module</info>
<acl role="Administrators">
<resources>
<all />
</resources>
</acl>
</my_module_custom_data>
</resources>
</api>
</config>
This file defines a new resource called my_module_custom_data. The <info> tag provides a description of the API, and the <acl> tag defines which roles have access to it.
Configuring the Module Configuration XML
Open app/code/my_module/config.xml and add the following content:
<config>
<modules>
<My_Module>
<active>true</active>
</My_Module>
</modules>
<api>
<resources>
<my_module_custom_data translate="title" module="my_module">
<info key="test">Custom data API for my_module</info>
<acl role="Administrators">
<resources>
<all />
</resources>
</acl>
</my_module_custom_data>
</resources>
</api>
</config>
This file is used to register your module and define other global configuration settings.
Creating the API Controller
Open app/code/my_module/controllers/V1.php and add the following content:
<?php
namespace My\Module\Controller\Api;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Controller\ResultFactory;
class V1 extends \Magento\Catalog\Controller\Api\Rest
{
/** @var ResourceConnection */
protected $resourceConnection;
public function __construct(
ResourceConnection $resourceConnection,
ResultFactory $resultFactory
) {
parent::__construct($resultFactory);
$this->resourceConnection = $resourceConnection;
}
public function createAction()
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('my_module_custom_data');
// Retrieve data from the request
$data = $this->getRequest()->getHeaders();
$attribute1 = $data['attribute1'];
$attribute2 = $data['attribute2'];
// Insert into the custom table
$connection->insert($tableName, [
'attribute1' => $attribute1,
'attribute2' => $attribute2
]);
return $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)->setData([
'message' => 'Inserted successfully',
'data' => [
'id' => $lastId,
'attribute1' => $attribute1,
'attribute2' => $attribute2
]
]);
}
}
This controller extends the base REST controller and uses the Magento Resource Connection to interact with our custom database table.
Testing the API
Now, you can test your API using a cURL command or Postman:
curl -X POST "http://magento-host/magento/api/rest/my_module_custom_data" -H "Content-Type: application/json" -d '{"attribute1":"Value 1","attribute2":"Value 2"}'
This will insert the provided data into your custom table.
Conclusion
In this article, we demonstrated how to extend Magento’s REST API to create a new endpoint for inserting custom data into a custom database table. By following these steps, you can easily integrate external systems or third-party applications with your Magento store using the built-in REST API.
Remember that extending the REST API requires careful testing and validation, as it involves modifying the core Magento codebase. Always ensure you have proper backups and use a staging environment for development.
Don’t forget to check out Laravel Company for more in-depth tutorials on Magento development and eCommerce best practices.
Happy coding!