How to Create Custom Rest API in Magento 2

Rest API in Magento 2 defines a set of functions used by the developers to perform requests and get responses using the HTTP protocol. By default, Magento 2 provides some predefined rest APIs like Product API, Order API, Customer data API with which you can take virtual control of everything happening on the site. You can read our complete guide on Magento 2 API if you are just getting started.

Even though Magento 2 has these many rest APIs, they are not enough when it comes to using the custom data and their values. To manage your custom data and fields, you need to create custom rest API in Magento 2 and today, I have come up with the steps for the same 🙂

Steps to Create Custom Rest API in Magento 2:

  1. Create module.xml at app/code/Meetanshi/CustomApi/etc/module.xml with the below code:
  2. Create registration.php at app/code/Meetanshi/CustomApi/registration.php and paste the below code:
  3. Create webapi.xml at app/code/Meetanshi/CustomApi/etc/webapi.xml with the below code:
  4. Create di.xml at app/code/Meetanshi/CustomApi/etc/di.xml with the below code:
  5. Create CustomInterface.php in path app/code/Meetanshi/CustomApi/Api/CustomInterface.php
  6. Create Custom.php in path app/code/Meetanshi/CustomApi/Model/Api/Custom.php

Finally, run the setup upgrade and deploy commands and you are done with creating a custom rest API in Magento 2. You can check the created custom rest API using [webiste/domain]/swagger

Custom Rest API in Magento 2


Don’t forget to share your suggestions and queries through the comment section below. Also, let me know which type of custom rest API did you create and how you used the data. Don’t forget to share this simple guide to help your fellow Magento developers!

(based on 31 Reviews)
How to Create Custom Rest API in Magento 2Author Magento Badge

Sanjay Jethva

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.


Leave a Reply

Your email address will not be published.