How to implement custom REST API in magento?

How to implement custom REST API in magento?

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to implement custom REST API in magento?

Creating Custom REST API in Magento: A Comprehensive Guide

Hello everyone! Today I’m going to guide you through creating a custom REST API in Magento. This tutorial will be particularly useful for those who want to extend the built-in capabilities of Magento’s REST API or need to access specific data points not covered by default endpoints.

Prerequisites

Before we begin, make sure you have the following:

  • A local or remote Magento installation (2.x or later)
  • Basic knowledge of PHP and XML configuration
  • Familiarity with OAuth for authentication

Step 1: Create Your Custom API Module

In your Magento root directory under app/code/local, create a new folder structure for your module:

Custom/
└── Restapi/
    ├── etc/
    │   ├── config.xml
    │   └── api2.xml
    └── Model/
        ├── Api2/
        │   └── Restapi.php
        └── Api2/
            └── Restapi/
                └── Rest/
                    └── Admin/
                        └── V1.php

Step 2: Configure Your Module

config.xml

The configuration file tells Magento about your module. Open config.xml in the etc folder and add the following content:

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Restapi>
            <version>0.1.0.0</version>
        </Custom_Restapi>
    </modules>
    <global>
        <models>
            <restapi>
                <class>Custom_Restapi_Model</class>
            </restapi>
        </models>
    </global>
</config>

api2.xml

The api2.xml file defines your API endpoints. Open it and add the following:

<config>
    <api2>
        <resource_groups>
            <restapi translate="title" module="Custom_Restapi">
                <title>Custom Rest API</title>
                <sort_order>10</sort_order>
            </restapi>
        </resource_groups>
        <resources>
            <restapi translate="title" module="Custom_Restapi">
                <group>restapi</group>
                <model>restapi/api2_restapi</model>
                <title>Testing My Rest API</title>
                <sort_order>10</sort_order>
                <privileges>
                    <admin>
                        <create>1</create>
                    </admin>
                </privileges>
                <routes>
                    <route_entity>
                        <route>/custom/createwebsite/:s</route>
                        <action_type>entity</action_type>
                    </route_entity>
                </routes>
                <versions>1</versions>
            </restapi>
        </resources>
    </api2>
</config>

Step 3: Create Your Custom API Model

Restapi.php

Create the base model file Restapi.php in Model/Api2/ with the following content:

class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource {}

V1.php

Now, create the API endpoint-specific model V1.php in Model/Api2/Restapi/Rest/Admin/V1.php:

class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi
{
    protected function _create()
    {
        return json_encode(array("testing", "hello"));
    }

    protected function _retrieveCollection()
    {
        return json_encode(array("testing", "hello"));
    }
}

Step 4: Configure OAuth for Your API

  • Create an OAuth role (e.g., “Custom Rest API Role”) in the Magento admin panel under System > Permissions > Roles.
  • Assign this role to the Custom_Restapi module.
  • Create a consumer with the correct keys and secrets.

Step 5: Test Your Custom API

Create a simple PHP script using the OAuth client library to make requests to your custom endpoint. You can find an example script in the question above.

Conclusion

Creating a custom REST API in Magento allows you to extend its functionality and access data in ways not possible with built-in endpoints. By following this guide, you should now be able to create and test a simple custom API.

Remember that Magento’s built-in API documentation (https://adobe-commerce.redoc.ly/) is an invaluable resource for understanding the available methods and parameters.

Don’t forget to check out Laravel Company for more comprehensive guides on Magento development, including advanced topics like creating custom controllers and improving performance.

If you found this guide helpful, please share your feedback in the comments below. Happy coding!

API Extensions Admin Configuration

Our network