Site icon R-bloggers

Accessing Magento 2 REST API from R

[This article was first published on R – Alex Levashov – eCommerce Consultant (Melbourne, Australia), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Magento is my bread and butter and I like to do data analysis with R. Hence I though, can I access Magento 2 data through R directly, without need of export or connect to the database?

Magento 2 has API, so I’ve decided to give it a try.

Magento (including Magento 2) supports both SOAP and REST APIs and for this exercise I used REST API.

There is no any ready wrapper/package that can be used to access Magento from R, at least that I aware, so I had to use more generic approach.

The R packages required are httr and jsonlite.

I took as a base access couple examples from Magento API documentation, just adopted them to R.

So let’s begin.

Preparation. Create Magento admin user

There are number of ways of authentication for Magento API, you may find them in the documentation above. Since I run my scripts from local machine, so in relatively secure environment, I decided that it is fine to use Magento admin user login and password for authentication.

So go to your Magento admin interface (System/Permission) and create there a new user. It is much better than use your regular admin user for security reasons. You may decide what permissions you give this user through Roles, for example give only access to orders. More details about that can be found in Magento documentation.

For the next steps you need to have Magento admin user name and password.

Now let’s get to the R.

Authentication

We will use our Magento user to create a token that is later used for API requests. Magento 2 REST API has different end points, they look like:

In the code below I’ve used URL of Magento Extensions Store that I run, https://shop.altima.net.au

Naturally you need to change it for your store specific URL.

Here is the code

library(jsonlite)
library(httr)

#get auth token

url <- "https://shop.altima.net.au/index.php/rest/V1/integration/admin/token"
# don't forget to change domain in the URL above to your store here and in other URLs below 

myquery = list (username = "myusername", password = "mypassword") 

# Magento admin user credentials above
# change them to yours



myqueryj = toJSON(myquery, pretty = TRUE, auto_unbox = TRUE)

req <- POST(url, add_headers ("Content-Type" = "application/json"), body = myquery, encode = "json")

token <-rawToChar(req$content[2:33])



Now we have token stored in token variable, we will use it to get access to the API.

Getting data

We are ready for more fun, pull some data from Magento to R.

Orders

Let’s start from getting order details, probably the most common use of Magento and other eCommerce APIs. The code below retrieves list of orders after certain date/time.

url <- "https://shop.altima.net.au/index.php/rest/V1/orders?"

auth <- paste0("Bearer ", token)

myquery <- list("searchCriteria[filter_groups][0][filters][0][field]"="created_at",
 "searchCriteria[filter_groups][0][filters][0][value]"="2017-01-01 00:00:00",
 "searchCriteria[filter_groups][0][filters][0][condition_type]"="gt")

request <- GET(url, add_headers ("Content-Type" = "application/json", "Authorization" = auth), query = myquery)

orders <- content(request, as ="parsed")

As you may notice query to API contains search criteria. The are number of options available there, to find out more visit this Magento 2 Documentation page.

As a result we’ll get orders in R list orders. Magento orders is quite a complex thing, so this list contains several levels of nesting inside.

Products

Next often required operation is to get certain products. The logic of the code below is quite similar with getting orders, it just uses different end point and search parameters.

url <- "https://shop.altima.net.au//index.php/rest/V1/products?"
myquery <- list("searchCriteria[filter_groups][0][filters][0][field]"="name",
 "searchCriteria[filter_groups][0][filters][0][value]"="%lookbook%",
 "searchCriteria[filter_groups][0][filters][0][condition_type]"="like")

request <- GET(url, add_headers ("Content-Type" = "application/json", "Authorization" = auth), query = myquery)
products <- content(request, as = "parsed")

As a result here we have all products, where product name includes the text “lookbook”. Percents before and after keyword indicate wildcart search. The products are also exported as R list called products.

Conclusion

That is the end of this post, hope it helps you to start using R for Magento 2 eCommerce data analysis. Good follow-up can be explore access to other data available through API, but I leave it with you. Full code is available at Github repository. Feel free to use it.

The post Accessing Magento 2 REST API from R appeared first on Alex Levashov – eCommerce Consultant (Melbourne, Australia).

To leave a comment for the author, please follow the link and comment on their blog: R – Alex Levashov – eCommerce Consultant (Melbourne, Australia).

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.