The Totally Beginners Guide to Magento 2 REST API

Magento is a very popular, open source eCommerce tool. Magento 2 (like Magento 1) provides a REST API that you can use to create powerful applications harnessing the power of Magento. This guide will help you get up and running in a short amount of time in integrating the Magento 2 REST API in your application. The official documentation may feel insufficient to most beginners so in this article, I will be providing specific examples in PHP.

Magento supports REST and SOAP APIs although REST API will be better suited to beginners hence we will be implementing the REST API.

First, we must register a web service on Magento. Following are the steps listed in the official documentation:

1. Create a web services user on Magento Admin by selecting System > All Users > Add New User.

This step is required for token-based authentication. Since we will be performing this type of authentication, the first step needs to be done.

2. Create a new integration on Magento Admin. To create an integration, click System > Integration > Add New Integration. Be sure to restrict which resources the integration can access.

Before we start performing requests, we need some type of authentication to access our Magento Store. Magento provides three methods of authentication:
Token Based, Session Based or OAuth-based. Following is an example of token based authentication in PHP:

We establish the initial connection using the admin username and password and get the token. This token can then be used for requests until the token expires (by default, its valid for 4 hrs in case of admin and this duration can be changed).

Request Methods

There are three request methods: GET, POST and PUT which can be utilized according to the application requirements. The list of REST endpoints can be found here.

  1. GET


In the above snippet, 24-BB is the sku (unique product identifier) of a sample product. This request returns the detail of the product in json form:

Indeed, this is a lot of detail. To get only a few attributes, you can retrieve filtered responses by modifying the request as in the example below:

This request returns only three attributes: id, name and price.

Magento also provides the option of search through REST API. For example, if we want to retrieve the invoices created after a specified date, we could use the following request:

This retrieves the invoices created after (specified by gt) 2018–01–08 00:00:00.



The above code creates a new product in the Magento store with the attributes that have been specified.

3. PUT

Example: To edit the attributes of any product, we could use a PUT request. This time we would have to specify the sku of the existing product in the url:

In this article I demonstrated three fairly basic examples on the usage of Magento 2 REST API. These should be sufficient to get started and for more info, the official documentation is below:

Feel free to ask any any question in the comments regarding the Magento 2 REST API!



Software and Web App Developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store