If you’re a developer or someone who have been tasting the basics of software development, you might have indeed come across the term API – Application programming interface. Ideally, API is defined as the software mediator between two software applications to communicate with each other. API includes a different set of rules, protocols, and tools that allow every software application (eg: a client) to get or write information to the information provider (eg: web server).
Why SOAP for eCommerce?
When it comes to e-Commerce websites, APIs play the big role of reading and writing information from and to the server. Be it a customer’s name or his already saved credit card details, every piece of information shown to the end user has to either read from or written to the web server. This is taken care by REST and SOAP APIs. REST and SOAP are models for web services, however, one that’s most recommend for eCommerce websites. Though REST is fast, efficient and simple, SOAP is standardized, secure and apt for payments.
The long explanations end right here and what comes next are downright codes that will help you understand the procedure of creating a custom SOAP API in Magento 2.
The complete code is available on GitHub: Creating a custom SOAP API.
Creating a Custom Module for SOAP API
Create a custom module based on the Magento 2 structure in the folder app/code
Magento 2 root directory app/code/Dckap/Customapi
Create a registration.php file in the below path to register a module in your Magento 2.
Magento 2 root directory/app/code/Dckap/Customapi/registration.php
Create a module.xml file in the below path to define your Magento 2 extension
Magento 2 root directory/app/code/Dckap/Customapi/etc/module.xml
Two API configurations are needed to create an API module: webapi.xml and di.xml
Create a webapi.xml file in the below path to configure access rights and API interface that specific method to use.
Magento 2 root directory/app/code/Dckap/Customapi/etc/webapi.xml
*/V1/customapi/name/* calls name method in CustomapiInterface
*/V1/customapi/name/:name* calls name method in CustomapiInterface, receives parameter(name), and transfers it into function.
API is called through the GET method, but you can change it to POST, DELETE or some other methods later if needed.
Create a di.xml file in the below path. In di.xml, we define the particular model to which the interface will call to.
Magento 2 root directory/app/code/Dckap/Customapi/etc/di.xml
Create an interface in the below path.
Magento 2 root directory/app/code/Dckap/Customapi/Api/CustomapiInterface.php
Create a model in the below path
Magento 2 root directory/app/code/Dckap/Customapi/Model/Customapi.php
Create an acl.xml to add your module to the Magento resources. Also, add the code to the Magento role.
Magento 2 root directory/app/code/Dckap/Customapi/etc/acl.xml
Create a test file to communicate a new api call.
[result] => Hello, Sathishraj
Communicating a new API call for an anonymous user
[result] => Hello, DCKAP
Hope this blog helped you understand the procedure of creating a Custom SOAP API in Magento 2. This is just a basic procedure and usage example, however, an ocean of applications and methods can be built with the aforementioned example as a foundation.
Looking for best Magento developers who can build your dream eCommerce store? Get it done from Magento 2 Certified Professionals.