How to use REST API in Magento 2?

REST API plays an important role in web apps and is one of the most popular API designs used recently. But for using REST API in Magento 2, you have to know the flow to call APIs in PHP.

In case you want to use token-based REST API in Magento 2, you will need authenticate, get the token then pass it in the header of every request you perform. Additionally, you also need to create a User Role web service then register the role to a new Admin User in Magento 2.

We will show you all the tutorials to help you use REST API in Magento 2.

At first, we need to create Web Service Role and Web Service User in Magento 2

  • Create Web Service Role:
  • Login Admin Panel> System> User Roles> Add New Role
  • Add the Role Name and your current password of Admin in Your Password field
  • Tap Role Resources
  • Choose what are required for service of your web in Resource Access
  • Tap Save Role
  • Create Web Service User in Magento 2

This user is used for the role you’ve created

  • Go to System> All Users> Add New User
  • Fill in all the necessary information
  • Tap User Role then choose which you’ve created
  • Tap Save User

The user above will used to REST API web service in Magento 2.

Next, we will get starting with Magento 2 REST API.

  • Authenticate REST API through Token authentication:Use following code:
    <?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" => "apiaccess", "password" => "cloudways123");
    
    $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); 
    
    ?>

    This code will help pass username and password with API URL in order to authenticate Magento 2 REST API as well as saved the token.
    *Note: The username and password are of what you’ve created

  • Get modules in Magento by REST API: By using REST API, you can get most of things in Magento 2. You can check in list of REST APIs for Magento EE and CE.
  • Use following code to demonstrate the API:
    <?php
    
    //Using above token into header
    
    $headers = array("Authorization: Bearer ".$token);
    
    //API URL to get all Magento 2 modules
    
    $requestUrl='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
    
    $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);
    
    ?>

    With the code above, you passed the token in the header with the API URL in order to get all the modules installed on Magento 2 store.

  • Use following code to print the result:
    <?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" => "apiaccess", "password" => "cloudways123");
    
    $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='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
    
    $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);
    
    ?>

We’ve brought you many of tutorials related to Magento 2 REST API. Hope that they are helpful for you.

Thanks for reading!