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

extension=oauth.so

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.

phpinfo-oauth

 

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


rest-api-structure

Step 3: Create api2.xml  file

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

<config>
    <api2>
        <resource_groups>
            <ipragmatech_gtm translate="title" module="ipragmatech_gtm">
                <title>Ipragmatech</title>
                <sort_order>10</sort_order>
            </ipragmatech_gtm>
        </resource_groups>
        <resources>
            <ipragmatech_gtm translate="title" module="ipragmatech_gtm">
                <group>ipragmatech_gtm</group>
                <model>ipragmatech_gtm/api2_product</model>
                <title>Gemtree Product</title>
                <sort_order>10</sort_order>
                <privileges>
                    <guest>
                        <retrieve>1</retrieve>
                    </guest>
                </privileges>
                <attributes translate="" module="ipragmatech_gtm">
                    <entity_id>Product ID</entity_id>
                    <type_id>Product Type</type_id>
                    <bidtype_id>Bid Type</bidtype_id>
                    <name>name</name>
                    <description>description</description>
                    <short_description>short description</short_description>
                    <sku>SKU</sku>
                    <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>
                    <apple_colour>Color</apple_colour>
                    <colour_percentage>Color Percentage</colour_percentage>
                    <pieces>Pieces or Size</pieces>
                    <pressure>Pressure</pressure>
                    <sugar>Brix(Sugar)</sugar>
                    <auction_total_bid>Bids</auction_total_bid>
                    <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>
                </attributes>
                <routes>
		    <route_entity>
                        <route>/ipragmatech/gtm/product/:id</route>
                        <action_type>entity</action_type>
                    </route_entity>
					
                    <route_collection>
                        <route>/ipragmatech/gtm/products/:grower_id/:bidtype_id</route>
                        <action_type>collection</action_type>
                    </route_collection>

                </routes>
                <versions>1</versions>
            </ipragmatech_gtm>
        </resources>
    </api2>
</config>

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"?>
<config>
  <modules>
    <Ipragmatech_Gtm>
      <version>0.1.0</version>
    </Ipragmatech_Gtm>
  </modules>
  <global>
      <helpers>
            <ipragmatech_gtm>
                <class>Ipragmatech_Gtm_Helper</class>
            </ipragmatech_gtm>
            
        </helpers>
    <models>
       <ipragmatech_gtm>
             <class>Ipragmatech_Gtm_Model</class>
        </ipragmatech_gtm>
    </models>
  </global>
</config>

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 :

<?php
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.

role-access

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.

attribute-access

Step 8: Test rest API

Test the Rest API on postman or any other rest client.
GET: http://youdomain.com/api/rest/ipragmatech/gtm/product/1
You will get the response in JSON format.

Conclusion:

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.

References:

  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: September 3rd, 2016 by Ajay Mehta