Today, we are going to shed light on the Magento 2 API, so let’s explain what this term means. Application program interface is a special interface which consists of protocols, routines,and tools designed to simplify the process of software development. Since APIs’ major purpose is to specify the way how different software parts interact within a system, they are often used to program graphical user interface components. Good APIs provide all building blocks necessary for developing an application, so a programmer only have to put them together. Although Magento 2 APIs incorporate the same principle, there are tons of nuances related to the ecommerce platform. So, what is the Magento 2 API?
We will start our journey into the world of the Magento 2 API with core features of the Magento web API framework, since it provides developers with the means to use web services connected with the Magento 2 system. If you are trying to master the Magento 2 API you should know that:
you’ll have to deal with three types of authentication: OAuth 1.0a for third-party apps, tokens for mobile apps, and login credentials for admins and customers;
accounts and integrations are assigned to resources if there is access to them; the API framework checks whether a call is authorized to perform the request;
you only need a few lines of xml to configure any Magento or third-party service as a web API; all XML elements and attributes should be defined in a webapi.xmlfile, otherwise a service will not be exposed;
the Magento 2 API framework relies on two models – CRUD and search, but does not support web hooks;
it offers the web API responses field filtering for conserving mobile bandwidth;
Since Magento 2 APIs utilize integration style, a single web API call can run multiple services simultaneously.
Magento 2 API Opportunities
Getting Started With Magento 2 API
First of all, it is necessary to register a new web service on your Magento admin. In case of token-based authentication, create a new web service user under System/All Users/Add New User. For session-based and OAuth authentication, there is no need to do this.
Then, create a new integration under System/Integration/Add New Integration. Please note that it is extremely important to set up resources the integration can access.
The final step requires using a REST or SOAP client for further configuration.
API Integration in the Magento 2 backend looks as follows:
Stores/Configuration/Services/Magento Web API and OAuth:
Magento Web API
The Magento 2 REST API
Based on the HTTP protocol, Magento 2 REST API interactions incorporate functions aimed at make requests and receive responses. The caller issues an HTTP request with the following elements:
an HTTP header (for authentication and other instructions);
a verb (GET, PUT, POST, or DELETE);
an endpoint (URI that determines three important components: the server, the web service, and the acted resource);
a call payload (all input attributes and parameters supplied with the request).
Please note that Magento 2 returns both a response payload and an HTTP status code. For further information about Magento 2 REST API, check the following official guides:
Magento 2 Community Edition offers a set of REST APIs which are listed here. To generate live REST API documentation, install Swagger UI on your server. With the following link, you will be able to generate a JSON schema with third-party modules and extension attributes from your system: http://<magento_host>/rest/default/schema.
After generating the schema file, you should load it into your Swagger UI. You can find all REST schema endpoints for available services here.
Magento 2 SOAP API Reference
You can find the official Magento 2 SOAP API reference here: SOAP Reference. It provides a list of service names per each module. The SOAP WSDL endpoint format looks as follows:
If you don’t want to read a 31 thousand line JSON document, pay attention to Swagger, since it generates a more human friendly version of the schema by defining the JSON document format. Follow this link to check the official demo by Swagger. For further information on Swagger and Magento 2, examine this article: MAGENTO 2 REST API SWAGGER SCHEMA.