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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ "name": "belvg/customapi", "description": "CustomAPI", "repositories": [ { "type": "composer", "url": "https://repo.magento.com/" } ], "require": { "php": "~5.5.0|~5.6.0|~7.0.0", }, "type": "magento2-module", "version": "1.0.0", "autoload": { "files": [ "registration.php" ], "psr-4": { "Belvg\\CustomAPI\\": "" } } } |
registration.php:
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Belvg_CustomAPI', __DIR__ ); |
Magento 2 Extensions
Take your online store to the next level with BelVG Magento 2 Extensions
Visit the storeetc/module.xml:
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Belvg_CustomAPI" setup_version="1.0.0" /> </config> |
The second step is – create an interface for custom API. Let’s create Api/TestInterface.php file and save it with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace Belvg\CustomAPI\Api; interface TestInterface { /** * Test function * * @api * @return string */ public function test(); /** * Test function * * @api * @param string $param * @return string */ public function test1($param); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace Belvg\CustomAPI\Model; class Test implements \Belvg\CustomAPI\Api\TestInterface { /** * Test function * * @api * @return string */ public function test() { return 'test'; } /** * Test function * * @api * @param string $param * @return string */ public function test1($param){ return $param; } } |
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:
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Belvg\CustomAPI\Api\TestInterface" type="Belvg\CustomAPI\Model\Test" /> </config> |
Then we create a path to call our API. For that we write the following in etc/webapi.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd"> <route url="/V1/test/" method="GET"> <service class="Belvg\CustomAPI\Api\TestInterface" method="test"/> <resources> <resource ref="anonymous"/> </resources> </route> <route url="/V1/test1/:param" method="GET"> <service class="Belvg\CustomAPI\Api\TestInterface" method="test1"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes> |
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.
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.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>:
1 2 3 |
<resources> <resource ref="anonymous"/> </resources> |
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>:
1 2 3 4 5 6 |
<resources> <resource ref="self"/> </resources> <data> <parameter name="customerId" force="true">%customer_id%</parameter> </data> |
Thus we should add a parameter in our interface and model, which will receive customer_id value.
Api/TestInterface.php:
1 2 3 4 5 6 7 8 9 |
/** * Test function * * @api * @param int $customerId * @param string $param * @return string */ public function test1($customerId, $param); |
Model/Test.php:
1 2 3 4 5 6 7 8 9 10 11 |
/** * Test1 function * * @api * @param int $customerId * @param string $param * @return string */ public function test1($customerId, $param) { return $customerId; } |
That’s all. We’ve just created custom API and now it’s ready to use.
Magento Development Services
Take your online store to the next level with BelVG Magento development
Click to visit the pageLooking for an expert Magento 2 development team? Turn to BelVG!
Hi, Tejas!
We have checked your issue, and everything is working fine at our Magento 2.3. Please, check the system once again.
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?
Thanks. Its working now.
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;
}
“
calling api rest/V1/test1 throwing “Request does not match any route.” error.
first one is working.
Hi!
I’ve just checked everything on Magento 2.1.7 and it works fine.
Here is the link to repository https://bitbucket.org/garpik/m2-api
Please, verify everything once again.
What a dabba code you’ve written? It’s not working at all. ………..