Magento SOAP API V1 and V2

In this blog post we look into an overview of how to create soap api in magento.

I hope you are familiar with basic concepts creating magento module, what is SOAP and how to create a SOAP User, Role from magento. There are many tutorials available online for these hence won’t be covering these again.

First create a simple magento module with a config.xml file, a helper, block folder and model folder.
Lets assume the name of module to be Excellence_Test


We need to create a api.xml file in etc/ folder of the module. api.xml is the main configuration file for your SOAP Api.

Write the following code in it

<?xml version="1.0" encoding="UTF-8"?>
            <xyz translate="title" module="excellence_test">
                <title>Demo API</title>
                    <list translate="title" module="excellence_test">
                        <title>List of products</title>
                <test translate="title" module="excellence_test">
                    <title>Test ACL Title</title>
                    <api translate="title" module="excellence_test">
                        <title>Test SOAP API ACL</title>

Above is the most basic api.xml configuration for only SOAP V1 api. We will look into the xml configuration in detail later.


Create the api model file. In the api.xml file we have mentioned the model to be “excellence_test/api” so we need to create the file

class Excellence_Test_Model_Api extends Mage_Api_Model_Resource_Abstract {

    public function items() {
        $arr_products = array();
        $products = Mage::getModel("catalog/product")
                ->setOrder('entity_id', 'DESC')

        foreach ($products as $product) {
            $arr_products[] = $product->toArray(array('entity_id', 'name'));
        return $arr_products;


The model needs to extend Mage_Api_Model_Resource_Abstract and it needs to define the function written in method tag in our api.xml file


Next we need to create api user and role. This can be created from Admin -> System Configuration -> Web Services -> SOAP-XML/RPC User/Role. This section is self explanatory and there are many tutorials online for this.
After doing this you should have “username” and “api_key” with you.

At this point we have a running V1 SOAP API. To test if the api is working, we need to write a simple php code.

Create a test.php file, in the magento root folder.
Write this code

$client = new SoapClient('');
$session = $client->login('api_username', 'api_password');
$result = $client->call($session, 'test_product.list', array(array()));
echo '<pre>';

If you run this file, it should return output.


Add the below xml configuration next to the resource_alias tag.



Create wsdl.xml file in etc/ folder of your module.
Paste this code in your xml file

<?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}}">
        <schema xmlns="" targetNamespace="urn:Magento">
            <import namespace="" schemaLocation="" />
            <complexType name="fieldInfo">
                    <element name="entity_id" type="xsd:string"/>
                    <element name="name" type="xsd:string"/>
            <complexType name="fieldInfoArray">
                    <restriction base="soapenc:Array">
                        <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:fieldInfo[]" />
            <complexType name="newFieldInfo">
                    <element name='test' type="xsd:int"></element>
    <message name="excellenceTestListRequest">
        <part name="sessionId" type="xsd:string" />
    <message name="excellenceTestListResponse">
        <part name="products" type="typens:fieldInfoArray" />
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="excellenceTestList">
            <documentation>List of products</documentation>
            <input message="typens:excellenceTestListRequest" />
            <output message="typens:excellenceTestListResponse" />
    <binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
        <soap:binding style="rpc" transport="" />
        <operation name="excellenceTestList">
            <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}}" />


We need to creat a model file as well. For V2 calls, magento automatically adds suffix V2 to existing model defined.
In our case the model we defined was “excellence_test/api”, magento will automatically add “excellence_test/api_v2”

So we need to create a V2 file

class Excellence_Test_Model_Api_V2 extends Excellence_Test_Model_Api {


This model simply extends the V1 api, so this file will be empty only.

At this point even your SOAP V2 api should be ready.
To test add this code to your previous test.php file

$client = new SoapClient('');
$session = $client->login('api_username', 'api_key');
$result = $client->excellenceTestList($session);
echo '<pre>';