How to extend and customize magento REST API

Recently, we were working on eCommerce platform for with auction support on magento . Our client wanted an android application for their platform. After requirement analysis, we decided to use magento and decided to use its Rest API for android application. Magento have a few Rest API implementation and one need to use their SOAP webservices which are very complex and dying technology. Moreover, client’s requirements for the app was different than the standard APIs so we decided to write custom APIs and expose them for the android app. In this article, we have demonstrated, How we can write Custom  API for our requirements and extend the magento REST API ? Follow below steps to create custom Restfull API in magento:

Step 1: Installing Oauth Extension

Before installing the OAuth make sure our apt-get is updated properly ? else try to run those command which will update the apt-get also install the PECL support

apt-get update
apt-get install php-pear php5-dev

After that we need to run pecl install command for Oauth

pecl install oauth

After that add oauth entry in php.ini file

and final and most important step restart your apache

/etc/init.d/apache2 restart

Open phpinfo.php page in a web browser, the following figure shows an example of a properly set up OAuth extension.



Step 2: Create new module for customize Magento REST API

Create new module on magento server with the following structure to create custom module with REST support


Step 3: Create api2.xml  file

Create api2.xml and put in the iPragmatech > Gtm > etc > api2.xml and add the below code:

            <ipragmatech_gtm translate="title" module="ipragmatech_gtm">
            <ipragmatech_gtm translate="title" module="ipragmatech_gtm">
                <title>Gemtree Product</title>
                <attributes translate="" module="ipragmatech_gtm">
                    <entity_id>Product ID</entity_id>
                    <type_id>Product Type</type_id>
                    <bidtype_id>Bid Type</bidtype_id>
                    <short_description>short description</short_description>
                    <attribute_set_id>Attribute Set</attribute_set_id>
                    <stock_data>Inventory Data</stock_data>
                    <image_url>Default Image</image_url>
                    <is_saleable>Salability Status</is_saleable>
                    <total_reviews_count>Total Reviews Count</total_reviews_count>
                    <url>Product URL</url>
                    <buy_now_url>Buy Now URL</buy_now_url>
                    <is_in_stock>Stock Status</is_in_stock>
                    <colour_percentage>Color Percentage</colour_percentage>
                    <pieces>Pieces or Size</pieces>
                    <auction_bidder>Current Bider</auction_bidder>
                    <auction_current_price>Current Price</auction_current_price>
                    <auction_closing_time>Closing Date</auction_closing_time>
                    <product_owner>Product Owner</product_owner>


In this xml file we have set retrieve privilege to the Guest. Apart from that we have add some attributes in the xml, which we want to give privilege to the Guest. The routes tag in xml is used to created the route or url for the method used in custom API.

Step 4: Create config.xml  file

Step4 : Create the config.xml file and put in the iPragmatech > Gtm > etc directory and and the below code:

<?xml version="1.0"?>

In config.xml file we will define our Model and helper class.

Step 5: Create V1.php file

Create V1.php file under iPragmatech > Gtm >Model > Api2 > Product > Rest > Guest and add below code :

class Ipragmatech_Gtm_Model_Api2_Product_Rest_Guest_V1 extends Mage_Catalog_Model_Api2_Product_Rest {
	protected function _retrieve() 
		$product = $this->_getProduct (); 
                return $product->getData();

        protected function _retrieveCollection()
              /*Your Custom code*/
              return $products->toArray();

V1 file extends the Mage_Catalog_Model_Api2_Product_Rest class. Because we want to use the properties of magento catalog product.

Step 6: Setting a REST Role for Custom API

1. Log in to the Magento Admin Panel as an administrator.

2. Click System > Web Services > REST – Roles.

3. Click on the Guest from the list on Rest Roles page.

4. Select the Ipragmatech Gemtree Product and then Click Save Role button.


Step 7: Setting a REST Attributes for Custom API

1. Log in to the Magento Admin Panel as an administrator.

2. Click System > Web Services > REST – Attributes.

3. Click on the Guest from the list on Rest Attribute page.

4. Select the Ipragmatech Gemtree product attributes which want in Rest API call and then Click Save button.


Step 8: Test rest API

Test the Rest API on postman or any other rest client.
You will get the response in JSON format.


Hence, using this we can extend the Magento Rest API and create our new feature according to the client requirement. Feel free to contact us if you have any further query.


  1. How to Extend the Magento REST API to Use Coupon Auto Generation
  2. Introduction to Magento REST API


How to extend and customize magento REST API was last modified: August 17th, 2020 by Ajay Mehta
8 replies
  1. satish sojitra
    satish sojitra says:

    This is useful post. But how to call it from android and how to use oAuth to authenticate ? Is there any need to create Rest Consumer ? or any needs to generate access token to use rest api from android ? Please help me out on these questions.. Can you please share module code if possible?

      • satish sojitra
        satish sojitra says:

        One more question ajay. I think access token needs to create once only. So in android app every time I need to generate or I can store it in database. I seen your above given link I guess it’s storing in local storage.
        Thanks a lot ajay. It’s going to help me a lot. 🙂 happy coding !

        • ajaymehta
          ajaymehta says:

          Yes, token generate only once. No need to regenerate token. You can store it in the database or in local storage.

  2. Angus
    Angus says:

    Hi ajay,
    your post exactly fits my need but i have some trouble trying to use it.
    I did create the same module as yours, followed your steps etc. and then when i want to add attributes to my guest in rest attributes, the custom attributes doesn’t show.
    Thanks for your post 😉

    • Nati Dan
      Nati Dan says:

      I’m having the issue.
      custom Ipgragmatech attributes are missing in guest attributes.

      Did you manage to solve it?

  3. Joan
    Joan says:

    Are we able to create custom name to the methods?, for example use this route : /test/:tesname and in the v1.php have a _test method?

    • Ajay Mehta
      Ajay Mehta says:

      Hello Joan,
      You can not create custom name to the method. By default magento allow only three methods _retrieve(), _retrieveCollection() and _create() . For example : /test/:tesname and action_type is collecton then _retrieveCollection() method would be call. Likewise if you use action_type entity then _retrieve() method would be called. This is the standard of magento Rest API. But still you want to create the custom method then you need to change the magento core files ( Not recommended ) .

Comments are closed.