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)
b.) XML-RPC

To access SOAP web service, load WSDL from URL as
http://magentohostname/api/soap/?wsdl

for accessing Magento API V2
http://magentohostname/api/v2_soap/?wsdl=1

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.

Magento

|–app
|–code
|      |—-local
|              |—-Testapi
|                       |—-Customer
|                                  |—-Model
|                                  |       |—-Customer
|                                  |                   |—-Group
|                                  |                   |        |—-Api
|                                  |                   |                |—-V2.php
                 |                                  |                   |——Api.php
                 |                                  |—-etc
|                                          |—-api.xml
                 |                                          |—-config.xml
                 |                                          |—-wsdl.xml
                 |–etc
|—-modules
|–Testapi_Customer.xml

 I. Tell Magento about your model

app/etc/modules/Testapi_Customer.xml

Tell Magento about your model

Tell Magento about your model

II. Create config.xml file of your Model

testapi/customer/etc/config.xml

Create config.xml file of your Model

Create config.xml file of your Model

III. Create api.xml file
testapi/customer/etc/api.xml

Create api.xml file

Create api.xml file

IV. Create Model for API Method

create testapi/customer/model/customer/group/api/V2.php

Create Model for API Method

Create Model for API Method

Create testapi/customer/model/customer/Api.php

Create Model for API Method

Create Model for API Method

V. Create wsdl.xml file

testapi/customer/etc/wsdl.xml

Create wsdl.xml file

Create wsdl.xml file

Create wsdl.xml file

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 :

For V1:
$res = $client->call($session_id, ‘customer.info’, array());

For V2:
$res = $client->customerCustomerInfo($session_id, argument);

Create your PHP file to call your web service

Create your PHP file to call your web service

Written By: – Naincy Gupta, Magento Developer, Mindfire Solutions

Posted on November 27, 2013, in Magento and tagged , , , , , , , , . Bookmark the permalink. 3 Comments.

  1. Great Work Nancy… Keep it up… 🙂

  2. really helpfull….

  3. salahuddin quazi

    nice Work Naincy …….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: