Magento – Simple REST API

apimagento-1.9modulerest

I want to create a RESTful api for my products.
I have set up the roles and settings in admin panel for rest api.

Now where to create the API PHP code and where to place the PHP file in the Magento directory structure?

Best Answer

Let's say you have your custom modules under magento/app/code/local/YOURSITE. Create custom module and add api2.xml under Custom/etc folder.

<config>
    <api2>
        <resource_groups>
            <yoursite_custom translate="title" module="yoursite_custom">
                <title>Custom api</title>
                <sort_order>10</sort_order>
            </yoursite_custom>
        </resource_groups>
        <resources>
            <yoursite_custom translate="title" module="yoursite_custom">
                <group>yoursite_custom</group>
                <model>yoursite_custom/api2_products</model>
                <title>Products</title>
                <sort_order>10</sort_order>
                <privileges>
                    <admin>
                        <create>1</create>
                        <retrieve>1</retrieve>
                        <update>1</update>
                        <delete>1</delete>
                    </admin>
                    <customer>
                        <retrieve>1</retrieve>
                    </customer>
                    <guest>
                        <retrieve>1</retrieve>
                    </guest>
                </privileges>
                <attributes translate="" module="yoursite_custom">
                    <entity_id>Entity ID</entity_id>
                    <name>Name</name>
                    <price>Price</price>
                    <qty>Qty</qty>
                </attributes>
                <routes>
                    <route_collection>
                        <route>/products</route>
                        <action_type>collection</action_type>
                    </route_collection>
                </routes>
                <versions>1</versions>
            </yoursite_custom>
        </resources>
    </api2>
</config>

Then in Custom module folder create Model folder, then inside Model folder create Api2 folder. Inside Api2 folder create Products.php file with following code:

<?php

class YOURSITE_Custom_Model_Api2_Products extends Mage_Api2_Model_Resource
{
}

Inside Api2 module also create folder Products then in Products folder create folder Rest and in folder Rest create folder Admin. Inside Admin folder create file V1.php with code:

<?php

class YOURSITE_Custom_Model_Api2_Products_Rest_Admin_V1 extends YOURSITE_Custom_Model_Api2_Clubs {

    public function _retrieveCollection() {
        $collection = Mage::getModel('YOURSITE_Custom/products')->getCollection();
        $products = $collection->load();

        return $products->getItems();
    }
}

With this kind of folder structure you can retrieve what you want.

-app
--code
---local
----YOURSITE
-----Custom
-------etc
---------api2.xml
-------Model
---------Api2
-----------Products
-------------Rest
---------------Admin
-----------------V1.php
---------Products.php

Magento already support this kind of api, to retrieve products. But I've used similar approach when I needed custom api to retrieve clubs and that was my custom entity in Magento. Also inside of Rest folder you can create Guest folder with V1.php file and write code there. In that case you can test faster your code on dev environment with just local url like yoursite.local/api/rest/products. (If you give gest privilege to retrieve products via api. One more note. Attributes in api2.xml are attributes that will be retrieved with this api. (You just need to check them from Magento admin configuration)