Follow Bellow Steps to Create a Custom Api With Authentication key in Magento2 :
➤ Create file in : code\Mageget\CustomApi\etc\webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route method="GET" url="/V1/getorder/:id">
<service class="Mageget\CustomApi\Api\GetOrderRepositoryInterface" method="getOrderById"/>
<resources>
<resource ref="Mageget_CustomApi::order_details_get"/>
</resources>
</route>
</routes>
➤ Create file in : code\Mageget\CustomApi\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="Mageget\CustomApi\Api\GetOrderRepositoryInterface" type="Mageget\CustomApi\Model\Api\OrderRepository"/>
</config>
➤ Create file in : code\Mageget\CustomApi\etc\acl.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Mageget_CustomApi::order" title="Mageget Get Order Details Using Id"
translate="title" sortOrder="110">
<resource id="Mageget_CustomApi::order_details_get" title="Order Details Get"
translate="title" sortOrder="30" />
</resource>
</resource>
</resources>
</acl>
</config>
➤ Create file in : code\Mageget\CustomApi\Api\GetOrderRepositoryInterface.php
<?php
namespace Mageget\CustomApi\Api;
interface GetOrderRepositoryInterface
{
/**
* Returns Order Info
*
* @api
* @return int
*/
public function getOrderById(int $id);
}
➤ Create file in : code\Mageget\CustomApi\Model\Api\Api\OrderRepository.php
<?php
namespace Mageget\CustomApi\Model\Api;
use Mageget\CustomApi\Api\GetOrderRepositoryInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magecomp\Extrafee\Block\Adminhtml\Sales\Totals;
class OrderRepository implements GetOrderRepositoryInterface
{
/**
* @var OrderRepositoryInterface
*/
protected $orderRepository;
protected $totals;
public function __construct(
OrderRepositoryInterface $orderRepository,
Totals $totals
) {
$this->orderRepository = $orderRepository;
$this->totals = $totals;
}
public function getOrderById(int $id)
{
$response = ["success" => false];
// $fee = $this->totals->initTotals();
$order = $this->orderRepository->get($id);
$order->getIncrementId();
$order->getState();
$order->getStatus();
$order->getStoreId();
$order->getGrandTotal();
$order->getSubtotal();
$order->getTotalQtyOrdered();
$order->getOrderCurrencyCode();
$order->getShippingPrice();
// get customer details
$custLastName = $order->getCustomerLastname();
$custFirsrName = $order->getCustomerFirstname();
// get Billing details
$billingaddress = $order->getBillingAddress();
$billingcity = $billingaddress->getCity();
$billingstreet = $billingaddress->getStreet();
$billingpostcode = $billingaddress->getPostcode();
$billingtelephone = $billingaddress->getTelephone();
$billingstate_code = $billingaddress->getRegionCode();
try {
$orderarray = [];
$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
$orderarray[] = [
"entity_id" => $order->getEntityId(),
"increment_id" => $order->getIncrementId(),
"sku" => $item->getSku(),
"name" => $item->getName(),
"qty" => $item->getQtyOrdered(),
"price" => $this->formatePrice($item->getPrice()),
"extraFee" => $this->formatePrice($order->getFee()),
"shippingAmount" => $this->formatePrice(
$order->getShippingAmount()
),
"grandTotal" => $this->formatePrice(
$order->getGrandTotal()
),
"customer_name" => $custFirsrName . " " . $custLastName,
"billingcity" => $billingcity,
"billingstreet" => $billingstreet,
"billingpostcode" => $billingpostcode,
"billingtelephone" => $billingtelephone,
];
}
$response = ["success" => true, "order" => $orderarray];
// $response = ['success' => true, 'order' =>$item->getData()];
} catch (\Exception $e) {
$response = ["success" => false, "message" => $e->getMessage()];
$this->logger->info($e->getMessage());
}
$returnArray = json_encode($response);
return $response;
}
public function formatePrice($price)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
$priceHelper = $objectManager->create(
"Magento\Framework\Pricing\Helper\Data"
); // Instance of Pricing Helper
return $priceHelper->currency($price, true, false);
}
}