If you're working with custom data in Magento 2, chances are you'll need to expose that data to external systems, mobile apps, or third-party tools. That's where creating a Custom REST API comes in handy.
Magento 2 comes with a wide range of built-in REST APIs such as Order, Product, and Customer APIs. However, when you're dealing with custom modules or custom business logic, the default APIs just aren’t enough.
So, to manage your custom data and fields, you’ll have to create a custom REST API.
In this blog post, we’ll walk you through the step-by-step process of creating a Custom REST API in Magento 2.
Why Create a Custom REST API in Magento 2?
While Magento’s native REST APIs are powerful, they can’t cover every unique requirement. With a custom REST API, you can:
- Expose your custom module’s data to third parties
- Integrate with external systems securely
- Enable mobile or headless apps to communicate with your backend
How to Create a Custom REST API in Magento 2?
Please follow the below-mentioned steps to learn how to create a Custom REST API in your Magento 2 store.
Step 1. Create module.xml
First of all, create a module.xml file in the app/code/MageDelight/CustomApi/etc/ folder and paste the following code.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="MageDelight_CustomApi" setup_version="1.0.0" /> </config>
Step 2. Create registration.php
After that, create a registration.php file in the app/code/MageDelight/CustomApi/ and paste the following code.
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MageDelight_CustomApi', __DIR__ );
Step 3. Define the API Route in webapi.xml
Next, create a webapi.xml file in the app/code/MageDelight/CustomApi/etc/ folder and paste the following code.
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd"> <route method="POST" url="/V1/custom/custom-api/"> <service class="MageDelight\CustomApi\Api\CustomInterface" method="getPost"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
Note: Replace anonymous with a specific ACL reference if you want to restrict access.
Step 4. Configure Dependency Injection in di.xml
Now, create a di.xml file in the app/code/MageDelight/CustomApi/etc/ folder and paste the following code.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="MageDelight\CustomApi\Api\CustomInterface" type="MageDelight\CustomApi\Model\Api\Custom"/> </config>
Step 5. Create the Interface File
After that, create a CustomInterface.php file in the path app/code/MageDelight/CustomApi/Api/ folder and paste the following code.
<?php namespace MageDelight\CustomApi\Api;
interface CustomInterface { /** * GET for Post api * @param string $value * @return string */ public function getPost($value); }
Step 6. Create the API Logic in the Model
Next, create a Custom.php file in the app/code/MageDelight/CustomApi/Model/Api/ folder and paste the following code.
<?php
namespace MageDelight\CustomApi\Model\Api;
use Psr\Log\LoggerInterface;
class Custom
{
protected $logger;
public function __construct(
LoggerInterface $logger
)
{
$this->logger = $logger;
}
/**
* @inheritdoc
*/
public function getPost($value)
{
$response = ['success' => false];
try {
// Your Code here
$response = ['success' => true, 'message' => $value];
} catch (\Exception $e) {
$response = ['success' => false, 'message' => $e->getMessage()];
$this->logger->info($e->getMessage());
}
$returnArray = json_encode($response);
return $returnArray;
}
}
Step 7. Run Magento CLI Commands
After adding all the files, run the following commands to register and enable the module:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
Step 8. Test Your Custom API
You can now test your new custom REST API using tools like Postman or directly via:
[your-domain]/swagger
Look for:
POST /V1/custom/custom-api
Send a POST request with a JSON body like:
{
"value": "Hello API!"
}
Expected response:
{
"success": true,
"message": "Hello API!"
}
Pro Tips
- For complex payloads, consider using custom data interfaces instead of simple strings.
- Secure your API using OAuth, token-based, or admin authentication, based on the use case.
- Log errors using Magento’s built-in logger for easier debugging.
Also read: How to Manage Index and Cache Management Using Magento 2 REST API?
Conclusion
And that’s about it!
This is the easiest way to create a Custom REST API in Magento 2.
And if you need our professional assistance with Magento Web Development, feel free to reach out to us anytime.



