Api

Create your own api. Test is with the build in api tester in your magento 2 installation. http://<yourmagento2website>/swagger

WARNING. This api is public. Acl will be added soon.

Use the snippet in the Magento 2 module creator.

Files

Model/TestManagement.php

<?php
declare(strict_types=1);

namespace Mage2Gen\Module\Model;

class TestManagement implements \Mage2Gen\Module\Api\TestManagementInterface
{

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

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 url="/V1/mage2gen-module/test" method="GET">
		<service class="Mage2Gen\Module\Api\TestManagementInterface" method="getTest"/>
		<resources>
			<resource ref="anonymous"/>
		</resources>
	</route>
</routes>

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="Mage2Gen\Module\Api\TestManagementInterface" type="Mage2Gen\Module\Model\TestManagement"/>
</config>

Api/TestManagementInterface.php

<?php
declare(strict_types=1);

namespace Mage2Gen\Module\Api;

interface TestManagementInterface
{

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