How to Create Web REST API in Magento 2

Blog
Spread the love

In this blog we learn about How to Create Custom Rest API in Magento2. Using API you can speed up getting, sending and processing data of any e commerce store. Magento API is a framework that provides developers with web services which communicate well with the Magento system and it also provides the ability to manage ecommerce stores effectively.

Before you begin: You should have a custom module, in that you need to create webapi and other related files and folders

Create webapi.xml at app/code/VenderName/ModuleName/etc/webapi.xml with the below code:

request-type: HTTP – defines several methods that indicates the desired action to be performed on a resource. Most commonly are: GET, POST, PUT, and DELETE, but there are several others.

So {{request-type}} can be replace with GET/POST/PUT/DELETE.

{{endpoint}}: This is the endpoint that will be use in URI

Service Class: This class should be interface. In this specify what methods a class should implement. You need to define all the API methods that you want to expose to the web in the interface. All the methods should have a doc-block. If your method expects parameters than all the parameters must be defined in the doc-block as @params {{type}} {{param}} {{description}}. Return type of the method must be defined as @return {{type}} {{description}}

request-method-name: this is the specific method which will be called by the endpoint.

Resource-id: the resource tag defines the access control these can be several level of access:

  • Admin: for admin level access you need to define admin resource in the resource tag.
  • Customer: for customer level access you need to set self in the resource.
  • Guest: for guest level resources you need to define anonymous in the resource tag.
  • In the case if you have any resource id for specific access control, you have to simply put that resource id like as Magento_Catalog::catalog

Create di.xml at app/code/VenderName/ModuleName/etc/di.xml with the below code:

Here VenderName/ModuleName\Model\Api\Custom.php is implements customInterface class by using Magento 2 prefrence node.

Create CustomInterface.php in path VenderName/ModuleName/CustomApi/Api/CustomInterface.php

Create Custom.php in path app/code/VendorName/ModuleName/Model/Api/Custom.php

For the demonstration here we’ll going to create a custom RestAPI to add product in Magento2

Before you begin create a module Bluethink_CustomApi in app/code/

Let’s start: Please follow the below steps:

Step1: Create webapi.xml at app/code/Bluethink/CustomApi/etc/webapi.xml with the below code:

Step2: Create di.xml at app/code/Bluethink/CustomApi/etc/di.xml with the below code:

Step3: Create ProductManagementInterface.php in path app/code/Bluethink/CustomApi/Api/ProductManagementInterface.php

Step4: Create ProductManagement.php in path app/code/Bluethink/CustomApi/Model/Api/ProductManagement .php

Now test api in postman

In the resource node we are giving ‘admin’ so we need a admin authorization token to access the API To get the admin token in Magneto select POST http request and write {{base-url}}/rest/all/V1/integration/admin/token in the url In body param send username and password, finally you will get a bearer token something like below:

“ietbemsauu26ssltdon4yvhtu9v2hvu3

installed apache2 on ubantu

Now again select POST http request and write {{base-url}}/rest/V1/addproduct

In Authorization section select Bearer Token and paste here bearer token.

installed apache2 on ubantu

Send payload

You will get Response like below:

“{\”success\”:true,\”message\”:\”3\”}”</>

installed apache2 on ubantu
bluethinkinc_blog

bluethinkinc_blog

2022-07-12

1

One Reply to “How to Create Web REST API in Magento 2”

  • Avatar
    Aakash Mishra

    It was helpful for me.

    2022-07-12 at 10:09 pm Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Customize lead qualification for a seamless experience

Spread the love When the 2024 Release Wave 2 was launched,

Create a Custom Console Command in Spryker

Spread the love In Spryker, developers can create custom

SALESFORCE

Spread the love Salesforce is a cloud-based software

CRUD Operation in Spryker Backoffice with a Custom Module

Spread the love Spryker is a highly flexible and modular

How to create custom module on Spryker for Backoffice?

Spread the love Creating a custom module in Spryker

How to Install Spryker Project with Docker on Local Machine

Spread the love Spryker is a flexible and scalable commerce

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

bluethinkinc Blogs