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?

1 Answer 1


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.

            <yoursite_custom translate="title" module="yoursite_custom">
                <title>Custom api</title>
            <yoursite_custom translate="title" module="yoursite_custom">
                <attributes translate="" module="yoursite_custom">
                    <entity_id>Entity ID</entity_id>

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:


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:


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.


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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.