How to build custom Magento Rest API effortlessly in less than 2 hours : Part 1
![](https://steemitimages.com/640x0/http://www.ipragmatech.com/wp-content/uploads/2017/06/Magento_Rest_Api.png)
Sooner or later you’ll run into a case where you need REST API for your online store and you found that Magento does not support all Magento REST API. What should you do now? How can you write REST API for your online store as per client need?
The Magento REST API allows you to use various data entities such as customer, catalog and order information to third party applications. I’d like to share it with you a quick, simple, well written and easy solution with will explains how you can write an extension for Magento REST API.
Magento REST API Features
- Simplistic design approach
- Testing and troubleshooting is easier
- Better performance than ever
- Ability to manage customers, addresses, orders, inventories and products using HTTP verbs such as GET, POST, PUT and DELETE.
- Data requests and responses can be in XML or JSON format.
Custom Extension for REST API
Create new custom module on your Magento server with the following structure to create a custom module with REST support.![rest-api-structure](https://steemitimages.com/640x0/http://www.ipragmatech.com/wp-content/uploads/2017/06/Magento_RestApi_Structure.png)
A Glance at the File Setup
Here's the list of files required for the desired setup, Here Ip is the package and Productapi are the module name. You can edit as per your need:
- app/code/local/Ip/Productapi/etc/config.xml: It's a module configuration file.
- app/code/local/Ip/Productapi/etc/api2.xml: It's a file which declares the APIs provided by our module.
- app/code/local/Ip/Productapi/Helper/Data.php: It's a file used by the Magento translation system.
- app/code/local/Ip/Productapi/Model/Api.php: It's a model file which implements the logic of our API methods.
- app/code/local/Ip/Productapi/Model/Api2/Product/Productlist/Rest/Guest/V1.php: It's a file to support Magento's v1 REST API.
Magento REST API Step 1: Enabling Extention
The first thing to now do is create your module definition file in app/etc/modules (not in the above folder diagram). For my extension, this is call Productapi.xml<?xml version="1.0"?> <config> <modules> <Ip_Productapi> <active>true</active> <codePool>local</codePool> <version>0.1.0</version> </Ip_Productapi> </modules> </config>
Magento REST API Step 2: Extensions Configuration
Now, we need to start setting up our extensions configuration. We need two files for this in our extension's etc folder; config.xml and api2.xml.Config.xml:
Create the config.xml file and put in the Ip -> Productapi -> etc directory and paste the below code. Here we are configuring Helper and Model file for the REST API and no need to change in Magento core file.<?xml version="1.0"?> <config> <modules> <Ip_Productapi> <version>0.1.0</version> </Ip_Productapi> </modules> <global> <helpers> <productapi> <class>Ip_Productapi_Helper</class> </productapi> </helpers> <models> <ip_productapi> <class>Ip_Productapi_Model</class> </ip_productapi> <api2> <rewrite> <request>Ip_Productapi_Model_Api2_Request</request> </rewrite> </api2> </models> </global> </config>
api.xml:
Create api2.xml and put in the Ip -> Productapi -> etc > api2.xml and paste the below code:<config> <api2> <resource_groups> <ip_productapi translate="title" module="ip_productapi"> <title>Ip Products</title> <sort_order>10</sort_order> </ip_productapi> </resource_groups> <resources> <ip_productapi_list translate="title" module="ip_productapi"> <group>ip_productapi</group> <model>ip_productapi/api2_product_productlist</model> <title>Product List</title> <sort_order>11</sort_order> <privileges> <guest> <retrieve>1</retrieve> </guest> <customer> <retrieve>1</retrieve> </customer> </privileges> <attributes translate="attribute1 attribute2 attribute3" module="ip_productapi"> <attribute1>attribute1</attribute1> <attribute2>attribute2</attribute2> <attribute3>attribute3</attribute3> </attributes> <routes> <route_entity> <route>/ip/V1/products</route> <action_type>entity</action_type> </route_entity> </routes> <versions>1</versions> </ip_productapi_list> </resources> </api2> </config>On the Model(under Resource), we are telling Magento the name of the models used in the API calls. privileges section say what our users(guest, customer) can do here.
On the attributes section, we are set up the attributes which we need on the REST API response. For the moment we are returning three attributes(attribute1, attribute2, attribute3).
The last section of api2.xml is the route. On the route section, we are telling the front controller name of the REST API. Magento API runs on the route and whenever gets API call it uses the route and pass request to the definition. That’s all from extension configuration and now all we need to do is write the API class to handle the REST request.
Magento REST API Step 3: Helper file
Next, we’ll need to create the Helper file to make sure the translation system of Magento works . It’s almost an empty file, but should be there as per the conventions!
<?php /** * Copyright © 2015 Ipragmatech . All rights reserved. */ namespace Ipragmatech\Ipreview\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { public function __construct(\Magento\Framework\App\Helper\Context $context ) { parent::__construct($context); } }
Creating Magento REST API Model file
we need to create a model file to support the Magento V1 API. Let’s create a model file with the following contents. On this file, we will write our logic and return the response.<?php /** * Override for Magento's Catalog REST API */ class Ip_Productapi_Model_Api2_Product_Productlist_Rest_Guest_V1 extends Mage_Catalog_Model_Api2_Product_Rest { /** * Return item GET */ protected function _retrieve() { // Write your code Here } }
Note
So that’s it as far as the file setup is concern for the custom API implementation. If you’re curious, enable the module from the back-end and don’t forget to clear the cache. In the next Article, we’ll go ahead and know how to configure the application with the Admin role.Conclusion
It's important to remember that HTTP was conceived to communicate between systems, which share nothing but an understanding of the protocol. In general, the fewer assumptions beyond HTTP you make, the better: this allows the widest range of programs and devices to access your API. If you are planning to add Magento REST API functionality on your website or mobile app then this article gives you the complete guide for creating Magento REST API structure in less time, effort and cost. You can also join the conversation if you have any other solution or idea which we can use to make it more attractive and easy. That’s all folks and feel free to contact us in case if you have any query.References
For further reading
- How to extend and customize magento REST API
- How to validate payment through PayPal using Magento REST API ?
- Tips to create magento custom attribute
Posted from my blog with : https://www.ipragmatech.com/magento-rest-api-two-hours-part-one/
If I work on Magento, do I have to contact the developers every time to add something new to the platform? It seems to me that there must be simpler solutions.
Look, you're right, because there's no need for ongoing developer services right now. The good thing about Magento is that there are a lot of ready-made extensions that are quickly and easily integrated into the platform. As for me, I prefer to work with Amasty specialists, and their latest extension that I installed is magento 2 social login extension. For me, this is a way to attract more new clients through social networks and make my service more convenient for buyers.