Magento 2 Create API

Creating API means helping online retailers to generate an Application Programming Interface for the own using. The API is a set of routines, protocols and other tools to design software applications. API is a mandatorily required element to connect among the data if you ask for any program or service from other websites. By creating API in Magento 2, you can easily get all building blocks to initiate a program successfully.

What is API in Magento 2?

API stands for “Application Programming Interface” to allow you access the data from an application. API can be called as a middleman between a programmer and an application. When the programmer calls for a request via the middleman, if the request is approved, the right data will be turned back.

It should look like two architectural kinds of the web APIs: REST (Representational State Transfer) and SOAP (Simple Object Access Protocol). In Magento 2, the web API coverage is the same for both REST and SOAP.


File: etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
	<route method="GET" url="/V1/magemeta-helloworld/post">
		<service class="Magemeta/HelloWorld/Api/PostManagementInterface" method="getPost"/>
			<resource ref="anonymous"/>

File: etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	<preference for="Magemeta/HelloWorld/Api/PostManagementInterface" type="Magemeta/HelloWorld/Model/PostManagement"/>
File: Model/PostManagement.php
namespace Magemeta/HelloWorld/Model;
class PostManagement {

	 * {@inheritdoc}
	public function getPost($param)
		return 'API GET returns $param as ' . $param;

File: Api/PostManagementInterface.php
namespace Magemeta\HelloWorld\Api;

interface PostManagementInterface {

	 * GET for Post api
	 * @param string $param
	 * @return string
	public function getPost($param);

Leave a Reply