Categories: Magento News

How to create a custom API in Magento 2

I. Application Programming Interface

API (Application Programming Interface) is an extremely important part of systems in general and Magento 2 in particular. We will learn the way to create a custom API in Magento 2.

We need a tool to interact with the API, we will use Postman, one of the most popular tools.

You can try to Download

II. The way to create a custom API in Magento 2

First of all, we will create a new module with the following directory structure:

Firstly, we must define the API in etc/webapi.xml as below:

In there:

  • Route:
    + method: the way to get data, can be GET or POST
    + URL: Link address
  • Service:
    + class: link of the handler class
    + method: which method to handle this request
  • Method

Next, we need to create an interface class  as Api/FirstApiInterface.php:

The Mode/firstApi.php has the below content:

Finally, we need to create a preference at etc/di.xml as follow: 

We need to run the following commands for the module to work: 

  • bin/magento setup:upgrade
  • bin/magento setup:di:compile
  • bin/magento cache:flush

To verify the result, we create a request in Postman with its URL: 

https://localhost/rest/all/V1/landofcode-firstapi/hello?name=LandofCode

The results response: 

Good luck to you!

Read more:

Joe Kun

Recent Posts

Streamoz Review 2025: What Twitch Growth Services Reveal About the Future of eCommerce Visibility

Daily, millions watch live streams on Twitch in the form of games, music, IRL content,…

2 days ago

How Powerful Social Media Boosting Tools Like Jaynike Music Marketing Are Transforming eCommerce Marketing in 2025

In the contemporary setting of competitive musicality, even the most talented are having unspeakable trouble…

2 days ago

TikTokstorm Review 2025: Powerful Social Media Boosting Tool Shaping the Future of eCommerce

In the dynamic world of eCommerce, visibility is everything. Whether you’re running a small Shopify…

2 days ago

Top Magento 2 SEO Extensions to Supercharge Enterprise SEO in 2025

Magento 2 SEO Extensions are essential for enterprises in 2025 to boost visibility, optimize performance,…

1 week ago

Life Insurance In The Digital Age: How Technology Is Changing Protection

The best life insurance should always be about one thing: financial security for our loved…

1 week ago

Automation in the Workforce: 6 Essential Jobs That Will Stay and 6 at Risk

Automation in the Workforce: The Invisible Shift That's Already Here I’ve spent over a decade…

1 week ago