How to Create a REST API in Magento 2
Magento 2 provides a powerful Web API framework that allows developers to expose custom functionality through REST APIs. These APIs help integrate Magento with external systems such as mobile apps, ERP systems, and third-party services while keeping the core platform secure and extendable.
In this article, we walk through a complete working example of creating a REST API in Magento 2 that sets a customer’s default shipping address using an address ID.
Create a New Magento 2 Extension
Create a new module under the Hikmadh namespace with the name ApiIntegration.
app/code/Hikmadh/ApiIntegration
app/code/Hikmadh/ApiIntegration/etc
app/code/Hikmadh/ApiIntegration/Api
app/code/Hikmadh/ApiIntegration/Model
Module Configuration – etc/module.xml
<?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="Hikmadh_ApiIntegration" setup_version="0.0.1"/>
</config>
Module Registration – registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Hikmadh_ApiIntegration',
__DIR__
);
Define the REST Endpoint – etc/webapi.xml
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/customers/set-default-shipping" method="POST">
<service class="Hikmadh\ApiIntegration\Api\AddressInterface"
method="setDefaultShipping"/>
<resources>
<resource ref="self"/>
</resources>
<data>
<parameter name="addressId" force="true">%addressId%</parameter>
</data>
</route>
</routes>
Dependency Injection – etc/di.xml
<?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="Hikmadh\ApiIntegration\Api\AddressInterface"
type="Hikmadh\ApiIntegration\Model\CustomerAddress"/>
</config>
Create API Interface – Api/AddressInterface.php
<?php
namespace Hikmadh\ApiIntegration\Api;
/**
* @api
*/
interface AddressInterface
{
/**
* Set default shipping address
*
* @return bool|array
*/
public function setDefaultShipping();
}
Model Logic – Model/CustomerAddress.php
<?php
namespace Hikmadh\ApiIntegration\Model;
use Hikmadh\ApiIntegration\Api\AddressInterface;
use Magento\Customer\Model\AddressFactory;
use Magento\Framework\App\RequestInterface;
class CustomerAddress implements AddressInterface
{
protected $addressFactory;
protected $request;
public function __construct(
AddressFactory $addressFactory,
RequestInterface $request
) {
$this->addressFactory = $addressFactory;
$this->request = $request;
}
public function setDefaultShipping()
{
$params = $this->request->getParams();
$addressId = trim($params['addressId'] ?? '');
if (empty($addressId) || !is_numeric($addressId)) {
return ['msg' => 'Address ID is invalid'];
}
$address = $this->addressFactory->create()->load($addressId);
if (!$address->getId()) {
return ['msg' => 'Address not found'];
}
$address->setIsDefaultShipping(1);
$address->save();
return true;
}
}
Magento Commands
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
Testing the API
POST /rest/V1/customers/set-default-shipping
Headers:
Authorization: Bearer <customer_token>
Content-Type: application/json
Body:
{
"addressId": 12
}
Conclusion
Creating REST APIs in Magento 2 allows your store to communicate seamlessly with external systems. By defining interfaces, routes, and secure access control, you can expose clean and maintainable APIs without touching core files. This approach keeps your Magento store scalable, upgrade-safe, and integration-ready.