Creating a custom API or extending the Core API

Last modified by erikwest on Mon, October 3, 2011 16:57
Source|Old Revisions  

This is an old revision of the document!


Creating a custom API or extending the Core API

The Core API allows you to manage a set of common resources used in Magento. However, you may choose to have your own set of resources to manage, or you may wish to extend the Core API to handle additional resources.

This tutorial leads you through the process of creating a custom API for a customer module that handles basic customer information.

Note: This tutorial applies to v1 of the API.

To learn more about the Core API, to read Magento Core API calls.

For general information about the Magento API, go to the Introduction.

1. Create an XML file that will define the API resource

Create a file named api.xml in the /etc folder in the customer module. Start with the empty structure, as follows:

  1. <config>
  2.     <api>
  3.         <resources>
  4.         </resources>
  5.         <acl>
  6.             <resources>
  7.                 <all>
  8.                 </all>
  9.             </resources>
  10.         </acl>
  11.     </api>
  12. </config>

2. Add a resource named customer

Add an element named customer in the <resources> element. Add a <methods> element, with elements for list, create, info, update and remove methods for customer resource.

Note that:

  • list will return all customers
  • create will create a new customer
  • info will return data on a specified customer
  • update will update data on a specified customer
  • remove will delete data on a specified customer
  1. <config>
  2.     <api>
  3. ....
  4.         <resources>
  5.             <customer translate="title" module="customer">
  6.                 <title>Customer Resource</title>
  7.                 <methods>
  8.                     <list translate="title" module="customer">
  9.                         <title>Retrive customers</title>                       
  10.                     </list>
  11.                     <create translate="title" module="customer">
  12.                         <title>Create customer</title>
  13.                     </create>
  14.                     <info translate="title" module="customer">
  15.                         <title>Retrieve customer data</title>
  16.                     </info>
  17.                     <update translate="title" module="customer">
  18.                         <title>Update customer data</title>
  19.                     </update>
  20.                     <delete>
  21.                         <title>Delete customer</title>
  22.                     </delete>
  23.                 </methods>
  24.                 <faults module="customer">                   
  25.                 </faults>
  26.             </customer>
  27.         </resources>
  28. ....
  29.     </api>
  30. </config>

3. Add faults

The resource can return some faults, so add a <faults> element in the customer element, and list the various faults.

  1. <config>
  2.     <api>
  3. ....
  4.         <resources>
  5.             <customer translate="title" module="customer">
  6. ....
  7.                 <faults module="customer"> <!-- module="customer" specifies the module which will be used for translation. -->
  8.                     <data_invalid> <!-- if we get invalid input data for customers -->
  9.                         <code>100</code >
  10.                         <!-- we cannot know all the errors that can appear, their details can be found in error message for call -->
  11.                         <message>Invalid customer data. Details in error message.</message>
  12.                     </data_invalid>
  13.                     <filters_invalid>
  14.                         <code>101</code >
  15.                         <message>Invalid filters specified. Details in error message.</message>
  16.                     </filters_invalid>
  17.                     <not_exists>
  18.                         <code>102</code >
  19.                         <message>Customer doesn't exist.</message>
  20.                     </not_exists>
  21.                     <not_deleted>
  22.                         <code>103</code >
  23.                         <message>Customer was not deleted. Details in error message.</message>
  24.                     </not_deleted>
  25.                 </faults>
  26.             </customer>
  27.         </resources>
  28. ....
  29.     </api>
  30. </config>

4. Describe the Access Control List (ACL) for the resource

In order to prevent unathorized access to our custom API, you must first list the resources that are restricted within the <acl> element.

  1. <config>
  2.     <api>
  3. ....
  4.         <acl>
  5.             <resources>
  6.                     <customer translate="title" module="customer">
  7.                          <title>Customers</title>
  8.                          <list translate="title" module="customer">
  9.                             <title>View All</title>
  10.                          </list>
  11.                          <create translate="title" module="customer">
  12.                             <title>Create</title>
  13.                          </create>                         
  14.                          <info translate="title" module="customer">
  15.                             <title>Get Info</title>
  16.                          </info>
  17.                          <update translate="title" module="customer">
  18.                             <title>Update</title>
  19.                          </update> 
  20.                          <delete translate="title" module="customer">
  21.                             <title>Delete</title>
  22.                          </delete>                       
  23.                     </customer>
  24.             </resources>
  25.         </acl>
  26.     </api>
  27. </config>

Then, map ACL resources to API resource methods by adding an <acl> element to each part of the resource that needs restricting:

  1. <config>
  2.     <api>
  3.         <resources>
  4.             <customer translate="title" module="customer">
  5.                 <title>Customer Resource</title>
  6.                 <acl>customer</acl>
  7.                 <methods>
  8.                     <list translate="title" module="customer">
  9.                         <title>Retrive customers</title>
  10.                         <acl>customer/list</acl>
  11.                     </list>
  12.                     <create translate="title" module="customer">
  13.                         <title>Create customer</title>
  14.                         <acl>customer/create</acl>
  15.                     </create>
  16.                     <info translate="title" module="customer">
  17.                         <title>Retrieve customer data</title>
  18.                         <acl>customer/info</acl>
  19.                     </info>
  20.                     <update translate="title" module="customer">
  21.                         <title>Update customer data</title>
  22.                         <acl>customer/update</acl>
  23.                     </update>
  24.                     <delete>
  25.                         <title>Delete customer</title>
  26.                         <acl>customer/delete</acl>
  27.                     </delete>
  28.                 </methods>
  29.               ....
  30.             </customer>
  31.         </resources>
  32. ....
  33.     </api>
  34. </config>

5. Create PHP code

Next, write some PHP code to access the resources. Start by creating a class called Mage_Customer_Model_Api that extends Mage_Api_Model_Resource_Abstract. Save it into a file called api.php.

  1. class Mage_Customer_Model_Api extends Mage_Api_Model_Resource_Abstract
  2. {
  3.    
  4.     public function create($customerData)
  5.     {
  6.     }
  7.  
  8.     public function info($customerId)
  9.     {
  10.     }
  11.  
  12.     public function items($filters)
  13.     {
  14.     }
  15.  
  16.     public function update($customerId, $customerData)
  17.     {
  18.     }
  19.  
  20.     public function delete($customerId)
  21.     {
  22.     }
  23. }

Note that you cannot create method “list” because it’s a PHP keyword, so instead the method is named items. In order to make this work, add a <method> element into the <list> element in api.xml, as shown below.

  1. <config>
  2.     <api>
  3.         <resources>
  4.             <customer translate="title" module="customer">
  5.                 <model>customer/api</model> <!-- our model -->
  6.                 <title>Customer Resource</title>
  7.                 <acl>customer</acl>
  8.                 <methods>
  9.                     <list translate="title" module="customer">
  10.                         <title>Retrive customers</title>
  11.                         <method>items</method> <!-- we have another method name inside our resource -->
  12.                         <acl>customer/list</acl>
  13.                     </list>
  14. ....                   
  15.                 </methods>
  16. ....
  17.         </resources>
  18. ....
  19.     </api>
  20. </config>

Now add some simple functionality to the Mage_Customer_Model_Api methods you created.

Create a customer:

  1.     public function create($customerData)
  2.     {
  3.         try {
  4.             $customer = Mage::getModel('customer/customer')
  5.                 ->setData($customerData)
  6.                 ->save();
  7.         } catch (Mage_Core_Exception $e) {
  8.             $this->_fault('data_invalid', $e->getMessage());
  9.             // We cannot know all the possible exceptions,
  10.             // so let's try to catch the ones that extend Mage_Core_Exception
  11.         } catch (Exception $e) {
  12.             $this->_fault('data_invalid', $e->getMessage());
  13.         }
  14.         return $customer->getId();
  15.     }

Retrieve customer info:

  1.     public function info($customerId)
  2.     {
  3.         $customer = Mage::getModel('customer/customer')->load($customerId);
  4.         if (!$customer->getId()) {
  5.             $this->_fault('not_exists');
  6.             // If customer not found.
  7.         }
  8.         return $customer->toArray();
  9.         // We can use only simple PHP data types in webservices.
  10.     }

Retrieve list of customers using filtering:

  1.     public function items($filters)
  2.     {
  3.         $collection = Mage::getModel('customer/customer')->getCollection()
  4.             ->addAttributeToSelect('*');
  5.  
  6.         if (is_array($filters)) {
  7.             try {
  8.                 foreach ($filters as $field => $value) {
  9.                     $collection->addFieldToFilter($field, $value);
  10.                 }
  11.             } catch (Mage_Core_Exception $e) {
  12.                 $this->_fault('filters_invalid', $e->getMessage());
  13.                 // If we are adding filter on non-existent attribute
  14.             }
  15.         }
  16.  
  17.         $result = array();
  18.         foreach ($collection as $customer) {
  19.             $result[] = $customer->toArray();
  20.         }
  21.  
  22.         return $result;
  23.     }

Update a customer:

  1.     public function update($customerId, $customerData)
  2.     {
  3.         $customer = Mage::getModel('customer/customer')->load($customerId);
  4.  
  5.         if (!$customer->getId()) {
  6.             $this->_fault('not_exists');
  7.             // No customer found
  8.         }
  9.  
  10.         $customer->addData($customerData)->save();
  11.         return true;
  12.     }

Delete a customer:

  1.     public function delete($customerId)
  2.     {
  3.         $customer = Mage::getModel('customer/customer')->load($customerId);
  4.  
  5.         if (!$customer->getId()) {
  6.             $this->_fault('not_exists');
  7.             // No customer found
  8.         }
  9.  
  10.         try {
  11.             $customer->delete();
  12.         } catch (Mage_Core_Exception $e) {
  13.             $this->_fault('not_deleted', $e->getMessage());
  14.             // Some errors while deleting.
  15.         }
  16.  
  17.         return true;
  18.     }

Create a custom adapter

In order to create custom webservice adapter, implement the Mage_Api_Model_Server_Adapter_Interface, which is shown below.

  1. interface Mage_Api_Model_Server_Adapter_Interface
  2. {
  3.     /**
  4.      * Set handler class name for webservice
  5.      *
  6.      * @param string $handler
  7.      * @return Mage_Api_Model_Server_Adapter_Interface
  8.      */
  9.     function setHandler($handler);
  10.  
  11.     /**
  12.      * Retrive handler class name for webservice
  13.      *
  14.      * @return string [basc]
  15.      */
  16.     function getHandler();
  17.  
  18.     /**
  19.      * Set webservice api controller
  20.      *
  21.      * @param Mage_Api_Controller_Action $controller
  22.      * @return Mage_Api_Model_Server_Adapter_Interface
  23.      */
  24.     function setController(Mage_Api_Controller_Action $controller);
  25.  
  26.     /**
  27.      * Retrive webservice api controller
  28.      *
  29.      * @return Mage_Api_Controller_Action
  30.      */
  31.     function getController();
  32.  
  33.     /**
  34.      * Run webservice
  35.      *
  36.      * @return Mage_Api_Model_Server_Adapter_Interface
  37.      */
  38.     function run();
  39.  
  40.     /**
  41.      * Dispatch webservice fault
  42.      *
  43.      * @param int $code
  44.      * @param string $message
  45.      */
  46.     function fault($code, $message);
  47.  
  48. } // Class Mage_Api_Model_Server_Adapter_Interface End

Here is an example implementation for XML-RPC:

  1. class Mage_Api_Model_Server_Adapter_Customxmlrpc
  2.     extends Varien_Object
  3.     implements Mage_Api_Model_Server_Adapter_Interface
  4. {
  5.      /**
  6.       * XmlRpc Server
  7.       *
  8.       * @var Zend_XmlRpc_Server
  9.       */
  10.      protected $_xmlRpc = null;
  11.  
  12.      /**
  13.      * Set handler class name for webservice
  14.      *
  15.      * @param string $handler
  16.      * @return Mage_Api_Model_Server_Adapter_Xmlrpc
  17.      */
  18.     public function setHandler($handler)
  19.     {
  20.         $this->setData('handler', $handler);
  21.         return $this;
  22.     }
  23.  
  24.     /**
  25.      * Retrive handler class name for webservice
  26.      *
  27.      * @return string
  28.      */
  29.     public function getHandler()
  30.     {
  31.         return $this->getData('handler');
  32.     }
  33.  
  34.      /**
  35.      * Set webservice api controller
  36.      *
  37.      * @param Mage_Api_Controller_Action $controller
  38.      * @return Mage_Api_Model_Server_Adapter_Xmlrpc
  39.      */
  40.     public function setController(Mage_Api_Controller_Action $controller)
  41.     {
  42.          $this->setData('controller', $controller);
  43.          return $this;
  44.     }
  45.  
  46.     /**
  47.      * Retrive webservice api controller
  48.      *
  49.      * @return Mage_Api_Controller_Action
  50.      */
  51.     public function getController()
  52.     {
  53.         return $this->getData('controller');
  54.     }
  55.  
  56.     /**
  57.      * Run webservice
  58.      *
  59.      * @param Mage_Api_Controller_Action $controller
  60.      * @return Mage_Api_Model_Server_Adapter_Xmlrpc
  61.      */
  62.     public function run()
  63.     {
  64.         $this->_xmlRpc = new Zend_XmlRpc_Server();
  65.         $this->_xmlRpc->setClass($this->getHandler());
  66.         $this->getController()->getResponse()
  67.             ->setHeader('Content-Type', 'text/xml')
  68.             ->setBody($this->_xmlRpc->handle());
  69.         return $this;
  70.     }
  71.  
  72.     /**
  73.      * Dispatch webservice fault
  74.      *
  75.      * @param int $code
  76.      * @param string $message
  77.      */
  78.     public function fault($code, $message)
  79.     {
  80.         throw new Zend_XmlRpc_Server_Exception($message, $code);
  81.     }
  82. } // Class Mage_Api_Model_Server_Adapter_Customxmlrpc End

Notes: The setHandler, getHandler, setController and getController methods have a simple implementation that uses the Varien_Object getData and setData methods.

The run and fault methods are a native implementation for an XML-RPC webservice. The run method defines webservice logic in this adapter for creating an XML-RPC server to handle XML-RPC requests. The “fault” method allows you to send fault exceptions for XML-RPC service when handling requests.

Common Error Messages

The following are common error messages that you might receive when creating your own custom API.

Invalid api path

This error occurs when the methods listed in the api.xml file do not correspond exactly with those used in your PHP file.

For example, in your api.xml file, you might have this:

  1. <config>
  2.     <api>
  3.         <resources>
  4.             <checkout_cart translate="title" module="checkout">
  5.                 <model>checkout/cart_api</model>
  6.                 <title>Cart API</title>
  7.                 <methods>
  8.                     <list translate="title" module="checkout">
  9.                         <title>Retrieve cart data</title>
  10.                         <method>info</method>       
  11.                     </list>
  12.                 </methods>
  13.             </checkout_cart>                 
  14.         </resources>
  15.     ...         
  16.     </api>
  17. </config>

You should have a corresponding info method in your PHP file.

  1. class Mage_Checkout_Model_Cart_Api extends Mage_Cart_Model_Api_Resource
  2. {
  3.     public function info()
  4.     {
  5.         ...
  6.     }
  7. }

If you are missing this method, the error “Invalid api path” will be returned.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs