Introduction to the Magento 2 Rest API – Setup and Working Example
Magento 2 provide set of predefined Rest APIs for different modules ranging from a catalog, customer, sales to checkout. Using Rest API Magento 2 communicate with the different third-party application and transfer data between several different servers over HTTP protocol.
Using REST API, we can create GET, PUT, POST or DELETE request from client side and get JSON or XML response from Magento 2 server side. Magento 2 support Token-based authentication and OAuth-based authentication. Using any of this authentication method Magento 2 authenticate the request and on successful authentication allows third party system to access the data.
In this tutorial, we will learn how to make a REST API call using Token-based authentication to fetch data.
Setup And Configure Access Token
To get an access token follow steps
1. Go to Magento 2 Admin > System > Integration
2. Create new integration by clicking Add New Integration
3. Enter your integration name in Name field and then enter your email address and current password in Email and Your Password field respectively.
4. Go to API tab and check mark the Resources for which you are giving access to third-party apps or select all the resources by selecting All option in Resource access field.
5. Click Save button. You will redirect to the Main page.
6. By default, your integration will be Inactivate. Activate the integration by clicking Activate link from grid row.
7. On the next screen click Allow. You will get your access token on next screens.
8. In this screen you will get Consumer Key, Consumer Secret, Access Token and Access Token Secret. Note down the Access Token. We will use this token to create a REST request.
Create a REST API request using REST client
In the Rest client, we need to configure the header as follows
Authorization Bearer <token>
Content-Type application/json
Here we need to pass the Access token that we got from step 8. In my case it is Bearer d6jtkgg3peyd3317am6dgjevcc58tfl1. Content-type would be application/json as we are going to send POST data in json format.
Example: Retrieve a product by SKU
In this request, we will retrieve a product information by passing SKU using GET request and /V1/products/:sku API call. In the REST client, we will configure following values in request tab.
Method: GET
Request URL: http://127.0.0.1/magento-2.1.9-sampledata/rest/V1/products/24-MB01
Headers:
Authorization Bearer <your access token>
Content-Type application/json
Here is the screenshot of how it looks like in REST client
After configuring this request when you hit the SEND button you will get response
Response:
{ "id": 1, "sku": "Custom-24-MB01", "name": "Joust Duffle Bag", "attribute_set_id": 15, "price": 34, "status": 1, "visibility": 4, "type_id": "simple", "created_at": "2017-10-08 05:29:30", "updated_at": "2017-10-08 05:29:30", ... ... .. }
Create a REST API request using CURL
If we want to call Magento 2 rest API using PHP code then we can use CURL command. In this example, we will call the same API using CURL command.
For the testing, purpose creates a script file let’s say test_api.php at the root of the Magento 2 installation and run the code.
Example: Retrieve a product by SKU using CURL
<?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); //Access token $token = 'd6jtkgg3peyd3317am6dgjevcc58tfl1'; $ch = curl_init("http://127.0.0.1/magento-2.1.9-sampledata/rest/V1/products/24-MB01"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token)); $result = curl_exec($ch); echo $result; ?>
In line no 17 we are passing the Request URL with SKU. Then after in line no 18 and line no 20 we are passing request option to GET and HTTP Header option to Content-Type: application/json, Authorization Bearer <your access token> respectively. By running this script you will get JSON string.
{"id":1,"sku":"Custom-24-MB01","name":"Joust Duffle Bag","attribute_set_id":15,"price":34,"status":1,"visibility":4,"type_id":"simple","created_at":"2017-10-08 05:29:30","updated_at":"2017-10-08 05:29:30","extension_attributes":{"stock_item":....
That’s it. We have successfully setup and run Magento 2 REST API. You can check the all the available REST API LIST on Magento 2 devdocs.
If you liked this post, then please like us on Facebook and follow us on Twitter.
Leave a Comment
(0 Comments)
Useful Magento 2 Articles
Author Info
Chirag
Connect With Me