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 Pragmaapps > Gtm > etc > api2.xml and add the below code:

            <pragmaapps_gtm translate="title" module="pragmaapps_gtm">
            <pragmaapps_gtm translate="title" module="pragmaapps_gtm">
                <title>Gemtree Product</title>
                <attributes translate="" module="pragmaapps_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 Pragmaapps > 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 Pragmaapps > 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


Leave a Comment

Scroll to Top