How to Create Custom Rest API in Magento 2

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.

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!

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

(based on 30 Reviews)

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.

12 comments On How to Create Custom Rest API in Magento 2

  • I have a question all the events in Magento2 will work regardless of REST API, frontend, and admin?

  • After upload this module, I can see this issue.

    PHP Parse error: syntax error, unexpected ‘¬†‘ (T_STRING), expecting ‘)’ in /var/www/html/testmage2/app/code/Meetanshi/CustomApi/registration.php on line 3

    Please let me know about this issue reason.
    Best Regards.

    • Hello Harry,
      There may be an issue in copying the code.
      Please try as:

      Thank You.

  • getting 404 error
    “message”: “Request does not match any route.”,
    “trace”: null

  • Failed to load API definition.

    • Hey,
      Can you explain the issue or provide a screenshot of any error you are facing?
      It will help me to solve the issue quicker.
      Thank you.

  • after hitting url, I am getting this
    “message”: “\”%fieldName\” is required. Enter and try again.”,
    “parameters”: {
    “fieldName”: “value”

    • Hello,
      You have not passed the parameter in API created which is causing the error.
      You won’t get the output by directly hitting the URL.

  • unexpected ‘¬†‘ (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /app/app/code/ciscomodule/custom/Api/Custom.php on line 9

Leave a reply:

Your email address will not be published.