Getting Started With Magento 2 REST API

by Fayyaz Khattak  May 8, 2017

Magento 2 API framework allows developers to create new services for communicating with Magento 2 stores. It supports REST and SOAP web services and is based on CRUD operations (Create, Read, Update, Delete) and a Search Model.

Given the importance of REST API in web apps, It is surprising that a lot of Magento developers ask the question, What is Magento 2 REST API?

Magento 2 Rest API

At the moment, Magento 2 uses the following three authentication methods:

  • OAuth 1.0a authentication for third-party applications.
  • Tokens to authenticate mobile applications.
  • Admins and customers authentication with login credentials.

These authentication methods can only access the resources assigned to them. Magento 2 API framework first checks whether the call has appropriate authorization to perform the request. The API framework also supports field filtering of API responses to preserve cellular bandwidth.

Developers use Magento 2 APIs for a wide range of tasks. For instance, you can create a shopping app and integrate it with your Magento 2 store. You can also build a web app which your employee could use to help customers make purchases. With the help of APIs, you can integrate your Magento 2 store with CRMs, ERPs or POS systems.

Using REST API in Magento 2

Using REST API in Magento 2 is a piece of cake. But for that, you need to understand the flow to call APIs in PHP.

If you want to use token-based Magento 2 REST API, first you will need to authenticate and get the token from Magento 2. Then, you will have to pass it in the header of every request you perform.

To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User. Keep in mind that creating a new role and user is necessary because it’s not a good practice to use Magento Owner User in a web service.

For the purpose of this tutorial, I will also use this REST API to get all the modules installed on your Magento 2 store.

I assume that you already have your Cloudways server launched with Magento 2. If not, signup at the Cloudways Platform right now.

Create Web Service Role in Magento 2

To create a web service role in Magento 2, follow these steps:

  1. Login to the Magento 2 Admin Panel.
  2. Go to System >> User Roles and tap the Add New Role
  3. Enter the Role Name.
  4. In Your Password field, enter the current password of your Magento 2 Admin.
  5. Now, on the left side, click Role Resources.
  6. In the Resource Access, select only those that are required for your web service.
  7. Once done, hit the Save Role

Create Web Service User in Magento 2

Now, create a new user for the newly created role through these steps:

  1. Go to System >> All Users and hit the Add New User
  2. Enter the required information including User Name, First and Last Name, Email, Password, etc.
  3. Now, on the left side, click User Role and select the newly created role.
  4. Once done, click the Save User

That’s all; now I will use this user to the Magento 2 REST API web service.

Magento 2 REST API Authentication

As I mentioned earlier, I will authenticate REST API through Token authentication. This means that I will pass a username and password in the initial connection and receive the token . This token will be saved in a variable, which will be passed in the header for further calls.

Here is the code:

In the above code, I passed username and password with the API URL to authenticate Magento 2 REST API and then saved the token in a variable for further use. Remember, you have to use the username password of the new user (created earlier).

Get Modules Using REST API in Magento 2

You can fetch almost everything using Magento 2 REST API. The List of REST APIs for Magento EE and CE is a good guide on this topic.

To demonstrate the API, I am going to get all the installed modules on a Magento 2 store. Here is the script:

In the above code, I passed the token (fetched earlier) in the header with the API URL to get all the modules installed on Magento 2 store.

Next, I will print the results. Here is the complete code:

Once you execute this code (with relevant information), you will see output similar to the following:

Magento 2 REST API Output

Conclusion

This is a very broad topic that requires a series of tutorial for complete coverage. The goal of this tutorial was to provide a simple example and demonstrate how easy it is to use REST API for Magento 2. I hope this tutorial proves useful for you and you could answer the question What is Magento 2 REST API? Feel free to get in touch if you need any help with the topic. I would love to answer your question. Happy coding! 🙂

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Stay Connected:

You Might Also Like...