Magento 2 Rest API: What Is It and How to Use It to Create a Custom API Module
Last Updated | June 12, 2024
Table of Contents
Magento 2 REST API
Magento 2 offers a REST API (Representational State Transfer Application Programming Interface) that facilitates interaction with its e-commerce functionalities. This API can be leveraged to integrate various platforms and ERPs such as Dynamics, NetSuite, and others. It empowers external systems to utilize Magento’s robust e-commerce features for managing customers, products, orders, and more.
Read Also: Magento API Integration: Guide to Effective Usage – 2023
Magento also provide several authentication methods to securely access the REST API’s :
– OAuth 1.0: This is a widely used standard for authentication, especially for third-party applications. It involves the exchange of tokens between the client and the server to establish identity.
– Token-Based Authentication: Magento 2 allows administrators and customers to authenticate using tokens. Tokens are generated and used for subsequent requests, eliminating the need to send sensitive credentials with each request.
– Session-Based Authentication: For logged-in user sessions, Magento 2 supports session-based authentication. This method allows clients to maintain a session with the server, similar to how a web browser maintains a session with a website.
Magento 2 REST API Usage
Magento 2 REST API can be used to perform CURD(Create, Read, Update, and Delete) operations on entities like Category, Product, Customer, Order,etc. In this session we will focus on using REST API. Here’s a general guide on how to use the Magento 2 REST API:
Prerequisites to use Magento 2 REST API is to create the credentials, different methods can be used to obtain the credential, We will be using the Integration token to Use the API
Creating API Credentials
– Log in to the Magento Admin Panel.
– Navigate to System > Extensions > Integrations.
– Click Add New Integration.
– Fill in the required information (name, email, etc.).
– Under the API tab, select the resources you want to access.
– Save and activate the integration.
– Copy the Consumer Key, Consumer Secret, Access Token, and Access Token Secret.
Magento 2 Swagger
Magento 2 also provides Swagger, a tool that allows you to view and interact with all available APIs in a graphical interface. Swagger makes it easier to explore the APIs and understand their functionality. To access Swagger in Magento 2, you need to put your application in developer mode. you can access the Swagger interface by navigating to the following URL in your web browser: http://your-magento-store.com/swagger.
Product API
For product operation Magento provide multiple endpoint for create, update, delet the products information in Magento
Example (/V1/Products)
The GET /V1/products endpoint in Magento 2 allows you to retrieve a list of products from your store. This endpoint is highly flexible and can be used to fetch all products or a specific subset of products based on various search criteria. This endpoint is designed to retrieve a list of products, which can include detailed information about each product. It supports filtering, sorting, and pagination, making it a powerful tool for integrating product data into your applications.
Magento 2 provides a robust REST API that allows you to interact with your store programmatically. In addition to retrieving product data, you can also create, update, and delete products, along with many other operations. Here’s a brief overview of the key endpoints:
1. Product GET Call: Use the GET /V1/products endpoint to retrieve a list of products. You can filter, sort, and paginate the results based on your requirements.
2. Product CRUD Operations:
– Create: Use the POST /V1/products endpoint to create a new product. Provide the necessary product data in the request body.
– Update: Use the PUT /V1/products/{sku} endpoint to update an existing product. Specify the product SKU in the URL and provide the updated data in the request body.
– Delete: Use the DELETE /V1/products/{sku} endpoint to delete a product. Specify the product SKU in the URL.
3. Other Endpoints:
– Customers: Manage customer data with endpoints like /V1/customers.
– Orders: Retrieve and manage orders with endpoints like /V1/orders.
– Categories: Manage product categories with endpoints like /V1/categories.
How to create custom API
Magento allow to create custom REST API to extend the functionality or create a new functionality full fill the business need. Here are the step to create the custom REST API in Magento 2:
1. create the etc/webapi.xml in etc folder and define route and service.
<?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/folio3_/customapi/" method="POST"> <service class="Folio3\CustomApi\Api\ContentInterface" method="saveDetail"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
2. In etc/di.xml define preference for interface used in webapi.xml
<preference for="Folio3\CustomApi\Api\ContentInterface" type="Folio3\CustomApi\Model\Content" />
3. define class and implement the required logic in it
<?php namespace Folio3\CustomApi\Model; class Content implements \Folio3\CustomApi\Api\ContentInterface{ public function saveDetail(){ //Your Logic here } }
Conclusion
The Magento 2 REST API provides a powerful way to integrate your Magento store with other applications and systems. Whether you need to retrieve product data, manage customers, or process orders, the REST API offers a flexible and efficient solution. By leveraging the API effectively, you can enhance the functionality of your Magento store and streamline your business processes
Read Also: Everything You Need to Know Magento Commerce Cloud: Top Benefits, Features, and Pricing