How to Set Rest API in Magento 2?

3

From here you can learn how use Rest API with Magento 2


Magento 2 supports REST (Representational State Transfer) and SOAP (Simple Object Access Protocol) web services, also this framework allows developers to create new services which interacting with Magento 2 stores.

Check below steps to create simple example for Rest API Web service

Step 1: Create Web API Route

First Create webapi.xml in the following path: app/code/Yournamespace/Modulename/etc/

<?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/test/item/:testid" method="GET">
		<service class="Yournamespace\Modulename\Api\TestInterface" method="getTestInfo"/>
		<resources>
			<resource ref="self" />
		</resources>
	</route>
	<route url="/V1/test/ids/" method="GET">
		<service class="Yournamespace\Modulename\Api\TestInterface" method="getAllTestIds"/>
		<resources>
			<resource ref="self" />
		</resources>
	</route>
</routes>

Step 2: Now, create an Interface file

Create TestInterface.php in the following path: app/code/Yournamespace/Modulename/Api/

<?php
namespace Yournamespace\Modulename\Api;
interface TestInterface {            
     public function getAllTestIds();
     public function getTestInfo();
}

Step 3: Create model file which implements interface

Create TestManagement.php in the following path: app/code/Yournamespace/Modulename/Model

<?php
namespace Yournamespace\Modulename\Model;
use Yournamespace\Modulename\Api\TestInterface;

class TestManagement implements TestInterface {
	
	public function getTestInfo() {
		/*======== ADD YOUR LOGIC HERE ========*/
	}

	public function getAllTestIds(){        
		/*======== ADD YOUR LOGIC HERE ========*/
	}
}

Once, you are done with the all above steps I will explain to you how can you test or check your created method will send the proper response or not.

But to check that, you should have knowledge of the Postman Desktop Application.

First, to RUN any API we need the “access token” which we get it after admin authentication. To authenticate please follow the below steps.

Step 1: Open Postman Desktop Application

Step 2: First, you need to RUN the admin access token command

Step 3: Now, set the below parameter at the proper place in Postman Desktop Application.

A) URL : http://[domain]/index.php/rest/V1/integration/admin/token
B) Method Type: POST
C) Body :
In the Request section, you can find the “Key : Value” column, here you need to set the admin credentials.
username : [YOUR-ADMIN-USERNAME]
password : [YOUR-ADMIN-PASSWORD]

After sending the above request you will get the access token. Please copy this access token because that will use it in the other requests.

Access Token:
Response : “ w0ptutejk1992dmoqvdfrenpay2e7jja”

Now, create the new postman request to retrieve data.

Step 1: Open Postman Desktop Application

Step 2: Now, set the below parameter at the proper place in Postman Desktop Application

A) Method: GET
B) Url: https://[Domain]/index.php/rest/V1/test/items
C) Body
In the Request section, you can find the “Key : Value” column, here you need to set the Access Token & Content-Type.

Authorization : Bearer [Access Token]
Content-Type : application/json

Try it, apply it and let me know what you have learn.

About the author

I’m Magento Certified Developer having quite 5 years of commercial development expertise in Magento as well as in Shopify. I’ve worked primarily with the Magento and Shopify e-commerce platform, managing the complexities concerned in building e-commerce solutions tailored to a client’s specific desires.

Related Posts

Leave a Reply