Magento2 REST API – Magento is best open-source e-commerce tool which is written in PHP. Magento recently upgraded version 1.x to 2.0 which is officially released and when you run Magento 2.0 then you will be feel like “ooooo shiny new toy”.
REST integration in Magento2 is “Simply Easy”. Magento provides a very simple REST API integration.
- Go to Magento System -> Permissions -> User Roles and add a simple rule over there lets say “WebKul”.
- Go to Magento System ->Permissions -> All Users and add a new user or you can use existing user with the detailed info most important fact is to set the permission of user to all instead of custom. Lets assume user is “admin”.
- Create a py file outside the magento and add the sample code.
import xmlrpclib import json import requests ##################For Token Based Authentication################# url = 'http://domain/index.php/rest/V1/integration/admin/token' credentials = { "username": "admin", "password": "webkul" } credentials = json.dumps(credentials) headers = {'Content-Type' : 'application/json'} response = requests.post(url, data=credentials, headers=headers) #############################HTTP Status Code <200> will assure you about successful connection ############ print response
- To check response use “.text” for example: response.text .
- You specify the token in the Authorization request header with the Bearer HTTP authorization scheme.
token = "Bearer " + json.loads((response.text))
- Now fetch list of Magento websites using token based authentication.
store_view_url = 'http://domain/index.php/rest/V1/store/websites' headers = {'Authorization':token, 'Content-Type':'application/json'} magento_websites = requests.get(store_view_url, headers=headers) magento_websites = json.loads(magento_websites)
Be the first to comment.