Magento 2 Create API with samples - Snippet

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

To support better in Magento 2 module development, Mageplaza team attempts to build API tester in your hello world module of Magento 2 module development series. http://<magento_host>/swagger

What is API?

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.

As eCommerce stores basing on Magento 2 platform, you should look at two architectural kinds of the web APIs: REST (Representational State Transfer) and SOAP (Simple Object Access Protocol). However, in the official documentation, they only come with raw curl request without any example. In order to connect and use Magento 2 API effectively, this topic will show you PHP examples in specific.

Snippet

File: etc/webapi.xml

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
	<route method="GET" url="/V1/mageplaza-helloworld/post">
		<service class="Mageplaza\HelloWorld\Api\PostManagementInterface" method="getPost"/>
		<resources>
			<resource ref="anonymous"/>
		</resources>
	</route>
</routes>

File: etc/di.xml

<?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="Mageplaza\HelloWorld\Api\PostManagementInterface" type="Mageplaza\HelloWorld\Model\PostManagement"/>
</config>

File: Model/PostManagement.php

<?php 
namespace Mageplaza\HelloWorld\Model;
 
 
class PostManagement {

	/**
	 * {@inheritdoc}
	 */
	public function getPost($param)
	{
		return 'api GET return the $param ' . $param;
	}
}

File: Api/PostManagementInterface.php

<?php 
namespace Mageplaza\HelloWorld\Api;
 
 
interface PostManagementInterface {


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




Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
create
api
sample
sinppet
featured

Comments for Magento 2 Create API with samples - Snippet

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

People also searched for:

  • Magento 2 create api
  • Magento 2 API
  • API in Magento 2
  • How to create API in Magento 2
  • Create API in Magento
  • magento 2 api example
  • magento 2 api key
  • magento 2 api list
  • magento 2 api swagger
  • magento 2 api authentication
  • magento 2 api create product
  • magento 2 api url
  • magento 2 api integration
  • magento 2 api add product
  • magento 2 api search criteria
  • magento 2 api
  • magento 2 api documentation
  • magento 2 api create order
  • magento 2 api add configurable product
  • magento 2 api add product to cart
  • magento 2 api call
  • magento 2 api configuration
  • magento 2 api doc
  • magento 2 api extend
  • magento 2 api field name is a required field
  • magento 2 api get all products
  • magento 2 api get products
  • magento 2 api get orders
  • magento 2 api login
  • magento 2 api products
  • magento 2 api rest
  • magento 2 api soap
  • magento 2 api tutorial
  • magento 2 api token
  • magento 2 api update product
  • magento 2 api user
  • magento api uncaught soapfault exception 2 access denied
  • magento 2 api list
  • magento 2 rest api list
  • magento 2 soap api
  • magento 2 api swagger
  • magento 2 rest api authentication
  • magento rest api documentation
  • magento 2 soap api example
  • magento 2 swagger