Get Free Audit

Shortly About Magento 2.0 Web APIs

Mar 2, 2016

2695 Siaržuk Piatroŭski

Shortly About Magento 2.0 Web APIs

APIs* can be used for a wide variety of tasks, from creating shop applications to CRM*/ERP* integrations. It’s quite easy to start working with APIs. Let’s take a look at creating products and categories using token-based* authentification. To do this we need Composer and Magento 2.0.

Let’s create a simple API call example for importing product and category to the webstore database.

First of all lets create a separate user for API calls and limit it’s access to catalog operations. Roles and users management interface is located under System -> Permissions menu tab and is self-explanatory.

I’ll use the Guzzle library for sending  API request. In order to install this tool via composer use instruction
:


Now we can start creating the application (in this post I tried to complicate the code). 

First of all we need client class for processing our requests:


Client allows us to run token-based auth and initialize API session.

Now let’s try to make some API calls.

The first one is for adding a product:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


In this example we created $testProduct array with required product information for import and send POST request to the appropriate URL.

As a response we should get a JSON object that contains the information about the newly created product.

And now we can make similar request for category creation
:


After some modification and optimization this code can be used for importing products from a CSV or some remote source.

To find out more about the possibilities of Magento 2 Web APIs, I recommend you to check source texts in Magento 2.0 root: dev/tests/api-functional/testsuite

Good luck!

 

API – in computer programming, an application programming interface (API) is a set of routines, protocols, and tools for building software and applications.

CRM – customer relationship management (CRM) is an approach to managing a company’s interaction with current and future customers.

ERP – enterprise resource planning (ERP) is a category of business-management software—typically a suite of integrated applications—that an organization can use to collect, store, manage and interpret data from many business activities.

Token-based authentication is a process of getting a token (session identifier) using login and password in order to work with remote website resources during a defined timeframe (without entering login and password).

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

3 Comments

  1. Nice article, I only have one suggestion:

    You shouldn’t use str_replace to remove the quotes, because it’s a JSON string. json_decode will return the proper result and should be used uniformly when dealing with the M2 REST API.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *