create a new Web Service V2 API with magento

What is Magento Web Service:

Magento has a very rich set of Web Service APIs available with it. But still it’s  great that it wouldn’t limit us with that only. We do have other options available to extend this treasure with more diamonds as well.



1)      I assume that you must be aware about Web Service and their functionality but still its my pleasure to write two lines

“Web Service is basically an interface through which you can connect through other application that is not under your control. But still you can consume their services to either retrieve or input your values to that third party application based on available permission to use that web service”

2) I understand you already have any existing magento extension under which you are going to add new web services so that some third party can use these web services for their own requirement. If you need any detail about how to create new magento extension

If you are beginner then you must have to go through our earlier blogs regarding
i) How to Setup a Web Service on Magento

ii) How to call Web Service using PHP

iii) How to call Web Service using Javascript

In this series today I am going to explain how to create new new Magento API (v2):

Before we go further into actual implementation of Web Service. I am going to list the files in which are required to be created on your existing module :

i)                 –Module-Path/etc/api.xml

ii)                –Module-Path/model/api.php

iii)               –Module-Path/etc/wsdl.xml

In this tutorial we will create a web service to “delete shopping cart rule by name” . Below are the codes written into each file

Code written into –Module-Path/etc/api.xml (See Code Description below the code)

<?xml version="1.0"?>






<PromoCode translate="title" module="Namespace_PromoCode">

<title> Promo Code Api</title>



<deletecartrulebyname translate="title" module="Rads_CoreApi">

<title>Delete Cart Rule by name</title>




















Code Analysis : This file (api.xml) is the primary files that tells the magento extension that this module has some external API available. All the codes under this files are always comes into  <config><api> tag only.

Line 7-9: its creates resources for this API. These resources are binded together under Module Name,Title and Model tag .

Line 10: From this section you can see all functions (API functions) are written into <methods> tag only.

Line 11-15 : This section contain actual function name . You can see each function inherits core modules “Rads_CoreApi”. And each API function has 3 parts

  • Title : Title of function
  • Method : It contain actual method name that will be available under your PHP file
  • ACL : it contain access control for this function. This is for prevent unauthorized access to our custom API.

Line 21-29 : Under this code we define the prefix of PHP function. So all the methods defined above on api.xml should be defined on v2.php file . But PHP function name should have function name like

Public function PrefixMethod().

Code written into –Module-Path/etc/wsdl.xml (See Code Description below the code)

<?xml version="1.0" encoding="UTF-8"?>

<definitions xmlns:typens="urn:{{var}}" xmlns:xsd="" xmlns:soap=""

xmlns:soapenc="" xmlns:wsdl="" xmlns=""

name="{{var}}" targetNamespace="urn:{{var}}">

<message name="PromoCodeDeletecartrulebynameRequest">

<part name="sessionId" type="xsd:string"/>

<part name="sku" type="xsd:string"/>

<part name="identifierType" type="xsd:string"/>


<message name="PromoCodeDeletecartrulebynameResponse">

<part name="result" type="xsd:string" />



<portType name="{{var wsdl.handler}}PortType">

<operation name="PromoCodeDeletecartrulebyname">

<documentation>Delete Shopping Cart Rule by name. </documentation>

<input message="typens:PromoCodeDeletecartrulebynameRequest" />

<output message="typens:PromoCodeDeletecartrulebynameResponse" />



<binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">

<soap:binding style="rpc" transport="" />

<operation name="PromoCodeDeletecartrulebyname">

<soap:operation soapAction="urn:{{var wsdl.handler}}Action" />


<soap:body namespace="urn:{{var}}" use="encoded" encodingStyle="" />



<soap:body namespace="urn:{{var}}" use="encoded" encodingStyle="" />




<service name="{{var}}Service">

<port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">

<soap:address location="{{var wsdl.url}}" />




Code Analysis: This file contain the code for wsdl (Web Service Descriptive Language) file. The code written on this file are directly accessible from Magento’s wsdl file path. Actual use of  this file to let developer know that what are the available methods on the API and what are the input as well as output of each and every web service API function.

Line 6-10 : Under this block you can see all possible input parameter for the API function with there name as well as type of input.

Line 11-13: Under this block you can see all possible output parameter for the API function with there name as well as type of input.

Line 17 -21 : As we have seen that  for each API function we define possible input and output parameters. These input and output are merged together under an operation.

Source Code written into –Module-Path/model/api.php (See Code Description below the code)

class NameSpace_PromoCode_Model_Api extends Mage_CatalogRule_Model_Rule



Code Analysis : If we need any common function that needs to interact with db then we can define that function into API.php class. For now we haven’t used any other function.

Source Code written into –Module-Path/model/api/v2.php (See Code Description below the code)

class Namespace_PromoCode_Model_Api_V2 extends Namespace_PromoCode_Model_Api



public function Deletecartrulebyname($name)


Mage::Log('in PromoCode');

$model = Mage::getModel('salesrule/rule')


->addFieldToFilter('name', array('eq'=>$name))






$return = "Promo Code Deleted successfully";




$return = "Unable to delete Promo Code";




$return = "No Promo Code available with name:".$name;

return $return;



Code Analysis: Under this file v2.php we write actual function where we perform actual implementation and return the output as defined on wsdl file.

Note: Since magento is case sensitive so keep your eyes on case sensitivity defining under xml files. It should be Camelcased.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...