This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

How to Create Custom API in Magento

Updated on December 24, 2021

3 Min Read

The usage of APIs in Magento is one of the most important and useful features, which allows us to integrate third party systems. Core APIs in Magento allow Magento developers to create and manage a set of common resources usage and extend the Core API to handle additional resources.

Magento Custom API

In this article, I’ll show you the process of creating a custom API Module.

Firstly, let’s prepare a directory for your custom API. Here is the file structure for custom API:

Custom API in Magento

The usage of APIs in Magento is one of the most important and useful features, which allows us to integrate third party systems. Core APIs in Magento allow Magento developers to create and manage a set of common resources usage and extend the Core API to handle additional resources.

In this article, I’ll show you the process of creating a custom API Module.

Firstly, let’s prepare a directory for your custom API. Here is the file structure for custom API:

<?xml version="1.0"?>
<config>
<modules>
<Cloudways_ cwcustomapi >
<active>true</active>
<codePool>community</codePool>
</cloudways_ cwcustomapi >
</modules>
</config>

In this XML file I used “Cloudways” as my module namespace and “customapi” as my module name. After this we need to create module configuration file, config.xml.

Create and save this file under app/code/community/Cloudways/custom-api/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Cloudways_ cwcustomapi >
<version>1.0</version>
</Cloudways_ cwcustomapi >
</modules>
<global>
<models>
<cwcustomapimodule>
<class>Cloudways_cwcustomapi_Model</class>
</cwcustomapimodule>
</models>
<helpers>
<cwcustomapimodule>
<class>Cloudways_cwcustomapi_Helper</class>
</cwcustomapimodule>
</helpers>
</global>
</config>

In the local.xml file I declared the “Model” and “Helper” classes according to the Magento traditions. Now, creating api.xml under app/code/community/Cloudways/custom-api/etc/api.xml.

The reason for creating “api.xml” file is, the usage of this file declares the API methods disclosed by module.

<?xml version="1.0"?>
<config>
<api>
<resources>
<cwcustomapi_product translate="title" module="cwcustomapi">
<model>cwcustomapi/product_api</model>
<title>Demo CWcustommoduleapi</title>
<acl>cwcustomapi/product</acl>
<methods>
<list translate="title" module="cwcustomapi">
<title>List of products</title>
<method>items</method>
</list>
</methods>
</cwcustomapi_product>
</resources>
<resources_alias>
<product>cwcustomapi_product</product>
</resources_alias>
<v2>
<resources_function_prefix>
<product>cwcustomapiProduct</product>
</resources_function_prefix>
</v2>
<acl>
<resources>
<cwcustomapi translate="title" module="cwcustomapi">
<title>Products</title>
<sort_order>5</sort_order>
<product translate="title" module="cwcustomapi">
<title>Product data</title>
</product>
</cwcustomapi>
</resources>
</acl>
</api>
</config>

Next, we need to create “wsdl.xml” file under code/community/Cloudways/custom-api/etc/wsdl.xml. The usage of this file defines the API method definitions as per the SOAP syntax.

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:typens="urn:{{var wsdl.name}}" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
  name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">
  <types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
      <import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />
      <complexType name="fieldInfo">
        <sequence>
          <element name="entity_id" type="xsd:string"/>
          <element name="name" type="xsd:string"/>
        </sequence>
      </complexType>
      <complexType name="fieldInfoArray">
        <complexContent>
          <restriction base="soapenc:Array">
            <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:fieldInfo[]" />
          </restriction>
        </complexContent>
      </complexType>
    </schema>
  </types>
  <message name="cwcustomapiProductListRequest">
    <part name="sessionId" type="xsd:string" />
  </message>
  <message name="cwcustomapiProductListResponse">
    <part name="products" type="typens:fieldInfoArray" />
  </message>
  <portType name="{{var wsdl.handler}}PortType">
    <operation name="cwcustomapiProductList">
      <documentation>List of products</documentation>
      <input message="typens:cwcustomapiProductListRequest" />
      <output message="typens:cwcustomapiProductListResponse" />
    </operation>
  </portType>
  <binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="cwcustomapiProductList">
      <soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
      <input>
        <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </input>
      <output>
        <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </output>
    </operation>
  </binding>
  <service name="{{var wsdl.name}}Service">
    <port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
      <soap:address location="{{var wsdl.url}}" />
    </port>
  </service>
</definitions>

The rest of the tags will make method appear when we call http:// magentostore.com/api/v2_soap?wsdl=1

After this will need to create Data.php file under app/code/community/Cloudways/custom-api/Helper/Data.php. The reason for creating this is that it just determines whether the translation system of Magento works perfectly or not.

<?php
class Cloudways_customapi_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Now let’s create a api.php file under app/code/community/Cloudways/custom-api/Model/Products/api.php

<?php
// app/code/community/Cloudways/custom-api/Model/Products/api.php
class Cloudways_customapi_Model_Product_Api extends Mage_Api_Model_Resource_Abstract
{
  public function items()
  {
    $arr_products=array();
    $products=Mage::getModel("catalog/product")
      ->getCollection()
      ->addAttributeToSelect('*')
      ->setOrder('entity_id', 'DESC')
      ->setPageSize(5);
    foreach ($products as $product) {
      $arr_products[] = $product->toArray(array('entity_id', 'name'));
    }
    return $arr_products;
  }
}

Now we need to create v2.php model file to support the Magento v2 API. Create this file under app/code/community/Cloudways/custom-api/Model/Products/api/v2.php

<?php
// app/code/community/Cloudways/custom-api/Model/Products/api/v2.php
class Cloudways_customapi_Model_Product_Api_V2 extends Cloudways_customapi_Model_Product_Api
{
}

That’s it, we’re done after creating this file.  Finally, just enable the module from the Magento back-end and clear Magento cache.

When you visit http://magentostore.com/api/v2_soap?wsdl=1 page, you should see method “cwcustomapiProductList” and those I used in this tutorial along with the other APIs.

I hope you enjoyed following this Magento tutorial. Got any questions about the process? Ask away in the comments, I’ll try my best to clear your confusion. Furthermore, Cloudways offers optimized Magento hosting which delivers blazing fast speed. Try out our free trial and feel the difference a world class Managed Cloud Hosting Platform can make.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Cloudways

Cloudways is a European MSP that provides custom cloud design, deployment and management solutions on leading cloud providers.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now