Magento 2: API

I am writing this post to describe how to use Magento API through php script and also for the APP. So it helps to develop APP and also to test in Magento side.

It is easy steps as follows:

Step 1: Create admin login

Step 2: Get token
You can use either postman or swagger or any tool to test

Call this URL to get token
URL:
http://www.website.com/index.php/rest/V1/integration/admin/token

Method: Post

Body section:
{
“username”: “admin”,
“password”: “admin123”
}

You can use admin credentials like username and password.

Screenshot: Authentication header

 

 

Screenshot: Body

After sending this request, you will get API token e.g. 1iu52qh8xw2yfbnt6r6agwoqxoxx2a2h

Step 3: Get or Post required data
Authentication: ‘No Authentication

Request Headers####
Header: Authorization
Value: Bearer 1iu52qh8xw2yfbnt6r6agwoqxoxx2a2h
Note: The value of Bearer will be your generated token

URL: I want to get product details of specific SKU so that URL like as
Get product details:
http://www.website.com/index.php/rest/V1/products/testsku

Screenshot: Product detail

Get Product list, ex. for getting 2 products
rest/V1/products?searchCriteria[page_size]=2

Get product by category id
rest/V1/categories/24/products

Get category
rest/V1/categories/

Get Customer
rest/V1/customers/me

Get product details by filtering category id. For example 24 is category id
rest/V1/products?searchCriteria[page_size]=5&searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=24

You can also try through PHP.

<?php 
define('BASEURL','http://www.website.com/'); 
$apiUser = 'admin'; 
$apiPass = 'admin123'; 
$apiUrl = BASEURL.'index.php/rest/V1/integration/admin/token'; 
/* Magento 2 REST API Authentication */ 
$data = array("username" => $apiUser, "password" => $apiPass);                                                                    
$data_string = json_encode($data);                       
try{
    $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);
    $token = json_decode($token);
    if(isset($token->message)){
        echo $token->message;
    }else{
        $key = $token;
    }
}catch(Exception $e){
    echo 'Error: '.$e->getMessage();
}


echo $key;




/*
    Get Product By SKU REST API Magento 2
    Use above key into header
*/
$headers = array("Authorization: Bearer $key");
//test is the sku. 
$requestUrl = BASEURL.'index.php/rest/V1/products/test';
// get total 10 products
// $requestUrl = BASEURL.'index.php/rest/V1/products?searchCriteria[page_size]=2';
// 24 category id
//$requestUrl = BASEURL.'index.php/rest/V1/categories/24/products';
//get all products
//$requestUrl = BASEURL.'index.php/rest/V1/products?searchCriteria=';

$ch = curl_init();
try{
    $ch = curl_init($requestUrl); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

    $result = curl_exec($ch);
    $result = json_decode($result);

    if(isset($result->message)){
        echo $result->message;
    }else{
		echo "

<pre>";
        print_r($result);
		echo "</pre>

";
    }
}catch(Exception $e){
    echo 'Error: '.$e->getMessage();
}

?>

Example: Customer Authentication using Email & Password


<?php

	try {   

	    $baseUrl = 'http://www.website.com/';
	    $apiUrl = $baseUrl . 'rest/V1/integration/customer/token'; 
	 
	    /* REST API Authentication */
	    $data = array("username" => 'tilkor.team@gmail.com', "password" => 'admin@123');
	    $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))                                                                       
	    );       
	    $curlData = curl_exec($ch);
	    $curlData = json_decode($curlData);
	    $access_token = '';
	    $message = '';
	    if(isset($curlData->message)){
	        $message = $curlData->message;
	    }else{
	        $access_token = $curlData;
	    }
	 
	    curl_close($ch);
	 
	    echo "Access Token ## ". $access_token;
	    echo "<br>";
	    echo "Error ## ". $message;


	} // end of try
	catch(Exception $e) {
		print_r($e->getMessage());
	}

?>


References:
https://devdocs.magento.com/guides/v2.2/get-started/bk-get-started-api.html
https://devdocs.magento.com/guides/v2.2/rest/list.html
https://devdocs.magento.com/guides/v2.2/get-started/order-tutorial/order-intro.html