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

How the eBay Cassini Algorithm Evaluates Listing Quality in 2026?

Is your eBay growth strategy aligned with how Cassini evaluates performance, or based on assumed…

21 hours ago

eCommerce Branding: Why It Matters More Than Ever for Growing Brands Today

Growing a company today is much harder than most executives expect, especially when it comes…

2 days ago

How to Prevent Stockouts in Webstore: Proven Inventory Management Tips

Prevent stockouts in webstore operations should be a top priority for any online business. If…

2 days ago

Shipping Rules That Reduce Support Tickets: Table Rate Shipping Setup Plus Proactive Delivery SMS Updates

Every ecommerce marketer knows the frustration: your inbox floods with "Where is my order?" messages…

1 week ago

5 Powerful Enterprise Software Development Companies in NYC You Should Know (2026)

When enterprise-scale projects demand flawless execution, the margin for error disappears. New York's business landscape…

1 week ago

10 Best API Integration Companies in the U.K. for Scalable Business Growth

In today’s digital-first economy, businesses rely heavily on seamless system connectivity to stay competitive. APIs…

2 weeks ago