Today to build a powerful Ecommerce store apart from choosing the right CMS, you also have to integrate and connect some third-party services like payment gateways and delivery partners. However, Magento comes with an interface that lets third-party applications read and write to a system using programming language constructs or statements. And, Magento supports and provides REST and SOAP. You can also create a dynamic REST API documentation set on your server with live data. By default, Magento uses Swagger to display REST APIs for all installed products and allows you to try out the APIs.

Many times it happens that you have to create your custom Rest API in your module to perform some action based on response. You still can use default API features Product API, Order API, Customer data API, etc. These all the APIs have multiple methods like GET, PUT, POST, etc. But if you want to create custom response apart from this you have no option left except creating custom API. So let’s learn how you can create your custom API in Magento 2.
But before we start first we need to generate Access Token for accessing Magento. To generate access token inside for your Magento 2 store simply navigate to System -> Extensions -> Integrations and generate it.

Now, we have to create a custom module by following the below steps.

Steps to Create Custom REST API in Magento 2:

Step 1: Firstly, we need to create a “registration.php” file inside our extension folder on this path.

app\code\Vendor\Extension app\code\Vendor\Extension\etc

Step 2: After that, we need to create “Module.xml” file inside the extension etc folder


Step 3: Now, we have to create one more file “webapi.xml” inside the same etc folder.


Step 4: And then, we need to create “di.xml” file in same etc folder of our extension.


Step 5: Later, create “PostManagementInterface.php” file inside extensions’s API folder to serve a response.


Step 6 : Lastly, Create “PostManagement.php” file inside model folder of extension.


Now, Go to Admin -> System -> Integrations and Get Access Token value and set authorization value into postman application.

Then, execute

Get Api Url : {base_url}/rest/V1/vender-extension

Post Api Url : {base_url}/rest/V1/vender-extension/post

this URL in postman.

And, you will able to fetch All the Parameter you want to set it.

Get API Response:

Get Api Response

Post API Response:

Post Api Responce

That’s it for today! You have successfully Implemented Custom Rest API in Magento 2 and you are free to customize this code according to your need for fetching data using REST API. Furthermore, You can also refer to this article: Everything You Need to Know About Magento 2 API.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issues while implementing this code.
Happy RESTing!

Click to rate this post!
[Total: 18 Average: 4.5]