Getting started with the Rest API in Magento 2

Avatar image of Maarten Maarten
March 21, 2023
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

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:

  1.  SOAP (Simple Object Access Protocol) and REST (Representational State Transfer) are both supported by Magento 
  2. 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.
  3. A few lines of XML can be used to configure any Magento or third-party web API.
  4. It enables web API field filtering, which preserves mobile response.
  5. 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

43WSwHmcndhl0dXB7ccEHN86LHrUbCEOa rV8uwTsL vJhkRKkWlYdilhGe11Fj5e2eNFhF6MlGIRKNouRXhtsilgEGlnP6rNmjMURZ2KTmbv6yF0wePa1BZHoYOdYOJePAgvzhzShs9ptO8GZXC8I

STEP 3

Enter a name for the ROLE

UkOMO7XM3KfxmR2qZhQ7aMO ciIo3CskSEVOuwDu5 Qp98Rb9ll2J8RLdEaH1X 3HK31 mEHHcKWDL1GZy puLWZPf3upBc1qiWV1s4e5R 8w9NhMwajau qm0 XNNR3VY8i iO45vDgyfhYqP4wQs

STEP 4:

Put your current Magento 2 Admin password in the YOUR PASSWORD section.

oxns2UQDbLruZO2DUYfhFgU9QTdb8mqkTiNhlVr3pphEBBq4C0fhlfvsIkpGACoZInCNBy7qVhqHVz2W r3kXKwdK 0W7FGruV k4L7JetD8ZfNd IZpU

STEP 5:

To access role resources, click the left-hand side. Only choose the options under Resource Access that are necessary for your store.

vgdpf mOCk52g 1YSNhsO mnM8Pdq1gJ0M0jJMdEK8bqO mPOZhruVNW22Lb2A roBWS4BJVLielnwJ47qtl6otEAzEhdA80pZsQZqiOuKhYamkDXUvBtY epHUvtzYzTmzsa52c02zLmTgsN2tIAmg

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.

kpLlpmC0C14JB D2CKsE1c5qMJXQ7EcikuJQTYsK2dsx8A6 zzO6LdEuooihfRWPPXIuLxSX8Gt VJGByqe7py61jLzxRfuKHGCuzAKBnZPsddWpc1sHBJc8EQqsPRSM

<?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.

IZYJjRwmpO4jBG0pXrOyqcBZFvHFCzn 4WfmR09R1yvnG

<?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:

zv6dLbDzco4rht3qfX uMaz9nFKRxY bjBY mzLgmjTUI66afXSBhZjpUJRgJ7wa1X1pdMRNC39o8uHKEP8hbGkDDP4NMwPY0tA1B6IgJV2QQhLt0hOorLZlMEAgIjyFXFRwwzGr8yGuU8gB5DP 98g

<?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>

Industry news straight to your inbox

Get the latest commerce news, trends, and strategies to grow your business
Subscribe to the newsletter
By submitting this form, you agree to receive promotional messages from Magstack. Unsubscribe any time by clicking the link in our emails.
Select location and language

Contact Our Sales Team

Learn more about our products, features, and pricing options.
By submitting this form, you agree to receive promotional messages from Shopify about its products and services. You can unsubscribe at any time by clicking on the link at the bottom of our emails.
Product
Pricing
Use cases
Recourses
Documentation
Log in
Start a free trial