Getting started with the Rest API in Magento 2
Users have the option to develop new services that can interface with third-party modules using the Magento 2 Web API framework. CRUD-based web services like REST and SOAP are supported by Magento 2 (create, read, update, and delete).
We will discuss the beginning stages of using the Magento 2 API in this article. These APIs increase the processing capability and make it easier to transfer data to a third-party system, such as data on items, clients, or orders. Additionally, it aids with inventory management.
Using APIs reduces the amount of code you need to write by automating the whole process.
WHAT ARE MAGENTO WEB APIs
For developers wishing to employ online services that aid in communication with the Magento system, it is important to understand that the Magento web API is fairly simple to comprehend. The API’s essential characteristics are as follows:
- SOAP (Simple Object Access Protocol) and REST (Representational State Transfer) are both supported by Magento
- It provides three different kinds of authentication: OAuth 1.0a for third-party application authentication, 2) the tokenization mechanism for mobile application authentication, and login credentials for management and user validation round out the authentication options.
- A few lines of XML can be used to configure any Magento or third-party web API.
- It enables web API field filtering, which preserves mobile response.
- The CRUD (create, read, update, delete) and search models are the foundation of the present system.
Getting started with REST API in Magento 2
You can create and set REST API in Magento 2 by following the steps explained down below:
STEP 1:
You can start by logging in to the Magento 2 Admin Panel
STEP 2:
Select SYSTEM > USER ROLES and then click the ADD NEW ROLE button to get started
STEP 3
Enter a name for the ROLE
STEP 4:
Put your current Magento 2 Admin password in the YOUR PASSWORD section.
STEP 5:
To access role resources, click the left-hand side. Only choose the options under Resource Access that are necessary for your store.
STEP 6:
Click the SAVE ROLE button, once you’re done with all of the aforementioned steps.
AUTHENTICATION FOR THE REST API IN MAGENTO 2
For this, we will use the token authentication mechanism to authenticate the REST API. In the initial connection, a username and password are sent, and in exchange, a token is received and for future calls, stored in a variable.
<?php
//API URL for authentication
$apiURL=”http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token”;
//parameters passing with URL
$data = array(“username” => “username”, “password” => “********”);
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: application/json”,”Content-Length: “.strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
Please note that in the above code, “username” and “password” are used for exemplary reasons only.
How to Get Modules Using REST API in Magento 2
You can also use the REST API in Magento 2, to fetch data.
<?php
//Using above token into header
$headers = array(“Authorization: Bearer “.$token);
//API URL to get all Magento 2 modules
$requestUrl=’YourgeneratedUrl’;
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
To get every module installed on the Magento 2 store, I already gave the API URL and the token (which was previously collected) in the code above.
The complete code is provided here as well:
<?php
//API URL for authentication
$apiURL=”URL”;
//parameters passing with URL
$data = array(“username” => “username”, “password” => “*********”);
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: application/json”,”Content-Length: “.strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array(“Authorization: Bearer “.$token);
//API URL to get all Magento 2 modules
$requestUrl=’YourURL’;
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
In this article, we primarily covered the REST API in Magento 2. All basics you need to know about the REST API in Magento 2, have been enclosed within. To get a firmer grip on the many folds of Magento 2, and its numerous extensions or to gain a more in-depth knowledge of REST API in Magento 2, click here <link>