Get Free Audit

How to Create Custom API in Magento 2

Nov 24, 2016

5070 Andrey Litvin

How to Create Custom API in Magento 2

Today I’d like to guide you through the process of creating Custom API in Magento 2.

At first, we must create the primary files of a module.

composer.json:


registration.php:


Extensions for Magento 2

Magento 2 Extensions

Take your online store to the next level with BelVG Magento 2 Extensions

Visit the store

etc/module.xml:


The second step is – create an interface for custom API. Let’s create Api/TestInterface.php file and save it with the following content:


That’s how we created an interface with the following functions:

test() function, it doesn’t receive any parameters;

test1($param), it does receive 1 parameter in order of the call to our API.

Now we must create a model, that would work through our interface. Let’s create Model/Test.php file and write the following code into it:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


We have just created a model, which is inherited by our interface and we described functions that are called in it. After that, we must connect our interface with the model. Please do the following to perform it:

Create etc/di.xml file with the following content:


Then we create a path to call our API. For that we write the following in etc/webapi.xml file:


Here we specified 2 paths to our functions:

/V1/test/ , which calls test method in TestInterface
/V1/test1/:param, which calls test1 method in TestInterface, receives parameter and transfers it into the function.

API is called through the GET method, but you can change it to POST, DELETE or some other methods if you want.

Andrey_Dubina

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

Also you are able to specify users group, that have permission to call your API or you can specify your own rule through the following structure, which is described in <route></route>:


And it’s possible to indicate Magento_Customer::group, self, Magento_Customer::admin and other rules, instead of anonymous.

It is possible to indicate additional parameters as well. For example we need to transfer customer_id parameter into the function through the interface.

For that purpose we will write the following between <route></route>:


Thus we should add a parameter in our interface and model, which will receive customer_id value.

Api/TestInterface.php:


Model/Test.php:


That’s all. We’ve just created custom API and now it’s ready to use.

magento development services

Magento Development Services

Take your online store to the next level with BelVG Magento development

Click to visit the page

Looking for an expert Magento 2 development team? Turn to BelVG!

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

7 Comments

  1. Hi, Tejas!
    We have checked your issue, and everything is working fine at our Magento 2.3. Please, check the system once again.

  2. This is not returning the response in Magento 2.3 When I try to run execute the endpoint through postman it is showing status code 200 but no response. Any idea why this is happening in Magento 2.3?

  3. Hi Jafar,

    Thank you for your comment. There was a misprint in the article. Try to replace the following lines in the TestInterface.php file:
    * @param $param string
    * @param int $customerId
    * @return string

    by
    * @param int $customerId
    * @param string $param
    * @return string

    The Test.php file will contain the following function:
    /**
    * Test1 function
    *
    * @api
    * @param int $customerId
    * @param string $param
    * @return string
    */
    public function test1($customerId, $param) {
    return $customerId.$param;
    }

  4. calling api rest/V1/test1 throwing “Request does not match any route.” error.

    first one is working.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *