Rate this post

Here are the steps that you will follow in this How to implement API in Magento 2 (Magento 2.3):

  1. Declare new Magento 2 API function in webapi.xml
  2. Declare the API Inteface
  3. Implement the interface StaffRepositoryInterface
  4. Declare the interface & model in DI file

Magento 2 supports both of REST (Representational State Transfer)  & SOAP (Simple Object Access Protocol) api which allows to search, get, create, update & delete entity in Magento such as: product, customer, sales order, cms block, etc. These Magento 2 API allow you connect third party application with Magento easier.

However in some cases, we need to implement more APIs in Magento to allow 3rd application control the data of our modules, or rewrite default API of Magento. How can we do that?

It’ easy! You can follow three simple steps below to implement new API in Magento 2.

1. Declare new Magento 2 API function in webapi.xml

I declare the API which allows to get list of staffs in my module in config file:

/app/code/[NameSpace]/[ModuleName]/etc/webapi.xml

2. Declare the API Inteface

Create interface StaffRespositoryInterface

/app/code/[NameSpace]/[ModuleName]/Api/StaffRepositoryInterface.php

 

3. Implement the interface StaffRepositoryInterface

I implement interface StaffRepositoryInterface in my model:

/app/code/[NameSpace]/[ModuleName]/Model/StaffRepository.php

4. Declare the interface & model in DI file

Insert line below to DI file: /app/code/[NameSpace]/[ModuleName]/etc/di.xml

 

Your Magento website needs a powerful Magento ERP for stock control.

DON'T MISS THE CHANCE!

magento erp

Prompt Widget in Magento 2 - Magento 2.3
How to use Magento 2 event? - Magento 2.3

Leave a Reply

Your email address will not be published. Required fields are marked *