🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Create Custom Rest API in Magento 2

By Sanjay JethvaUpdated on May 21, 2025 3 min read

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 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 Magento 2 API if you are just getting started.

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 🙂

Also Read: How to Create a Custom Theme in Magento 2?

Steps to Create Custom Rest API in Magento 2:

1. Create module.xml at app/code/Meetanshi/CustomApi/etc/module.xml with the below 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="Meetanshi_CustomApi" setup_version="1.0.0" />
</config>

 2. Create registration.php at app/code/Meetanshi/CustomApi/registration.php and paste the below code:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Meetanshi_CustomApi',
    __DIR__
);

3. Create webapi.xml at app/code/Meetanshi/CustomApi/etc/webapi.xml with the below 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/:value">
        <service class="Meetanshi\CustomApi\Api\CustomInterface" method="getPost"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

4. Create di.xml at app/code/Meetanshi/CustomApi/etc/di.xml with the below 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="Meetanshi\CustomApi\Api\CustomInterface" type="Meetanshi\CustomApi\Model\Api\Custom"/>
</config>

 5. Create CustomInterface.php in path app/code/Meetanshi/CustomApi/Api/CustomInterface.php

<?php

namespace Meetanshi\CustomApi\Api;

interface CustomInterface
{
    /**
     * GET for Post api
     * @param string $value
     * @return string
     */

    public function getPost($value);
}

 6. Create Custom.php in path app/code/Meetanshi/CustomApi/Model/Api/Custom.php

<?php

namespace Meetanshi\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; 
    }
}

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 [webiste/domain]/swagger

Custom Rest API in Magento 2

Don’t forget to share this simple guide to help your fellow Magento developers!

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.