Create a Custom API in Magento
Magento provides ability to manage your e-Commerce store by providing calls for working with resources such as customers, categories, products and sales order. Also, you can define your own API or you can extend Core API. Sometimes, we need to provide our own API from our own website to other parties; from here this web service concept came into picture.
Here, I am going to tell you how to create your own API. Magento core API supports –
a.) SOAP (by default)
To access SOAP web service, load WSDL from URL as
for accessing Magento API V2
I am here explaining it with V2.
Basic Steps for Making Custom API
Before, going to creation of API; first create your web service user and assign role to it.
1.) Create role in Magento Admin
2.) Create web service user
3.) Assign role to user
Steps for creating own API
1.) Tell Magento about your model
2.) Create config.xml file of your Model
3.) Create api.xml file
4.) Create Model for API Method
5.) Create wsdl.xml file
6.) Create your php file to call your web service.
Create role in Magento Admin
In Magento admin, go to System->Web Service->SOAP/XML-RPC – Roles
Define role name as myrole and Save Role. Then, you can define resources to Role under Role Resource tab.
Create web service User
In Magento admin, go to System->Web Service->SOAP/XML-RPC – Users
Define all required fields, and save user (remember Username and apiKey it is useful further for calling)
Assign Role to User
Now, you can assign role to user which you defined, under User Role tab. Save your user. Now, we start with creating API; we are creating API for getting information of customer.
Module directory structure:
Here, I described the structure of your directory module. Make sure, your structure should be in right format.
| | |—-Customer
| | |—-Group
| | | |—-Api
| | | |—-V2.php
| | |——Api.php
I. Tell Magento about your model
II. Create config.xml file of your Model
III. Create api.xml file
IV. Create Model for API Method
V. Create wsdl.xml file
After making your wsdl, just run http://magentowebsite/api/v2_soap/?wsdl=1 on browser and see that whatever you defined message, operation, etc. it is showing or not. If it’s showing then you created your web service successfully. Now, we move towards that how client call this.
VI. Create your PHP file to call your web service
Create this file in your root folder of Magento as anyname.php. In this username and password is same as when u defined ifor web service user i.e. Username and ApiKey. Create an object of SoapClient. After Running You can view all the details of the customer with id as 1.
Here the difference of V1 and V2 come in, when we call our webservice :
$res = $client->call($session_id, ‘customer.info’, array());
$res = $client->customerCustomerInfo($session_id, argument);
Posted on November 27, 2013, in Magento and tagged Create role in Magento Admin, custom API in Magento, e-Commerce Development, Magento, Mindfire Solutions, Module directory structure in Magento, Nancy Gupta, SOAP, Web service. Bookmark the permalink. 3 Comments.