Need help buying now? +358 9-424-505-31 +1 888-886-76-81
My Cart

Your Cart is Empty

Need help buying now?
+358 9-424-505-31 +1 888-886-76-81

Swagger in Magento 2. Tips for developers

img_GoMge_Swagger

It is surely no surprise for the developers to work with HTML-pages on the server side and to assemble them on the client’s side. Magento 2 also attempts to provide such advantages and to differentiate data processing and data presentation. In this article, we review some tips. We don’t consider the use of the features of Swagger, we just give an example of how to use Swagger integrated with Magento 2, in the anonymous user mode and in the authenticated user mode.

Access to services

We deployed a Magento module at http://magento213.gomage.com/ (login: gomage; password: uQuo1xae). To start using Swagger in the anonymous user mode, you should go http://magento213.gomage.com/swagger/. At first, Swagger application is loaded and it is fast enough, and then there is loading of the web-services description, which is slow in Magento.

img_Swagger1

The JSON schema of Magento services can be obtained by reference:

http://magento213.gomage.com/rest/default/schema?services=all 

Anonymous requests

Then Swagger generates a list of services that are available to an anonymous client:

img_Swagger2

And it allows you not only analyze these services in a comfortable way:

img_Swagger3

but also it allows to form queries:

img_Swagger4

and analyze responses:

img_Swagger5

Access to the limited services

First, you need to make an extra user in Magento (specifying admin user for access to the service is not a good idea):

img_Swagger_Magento_6

and assign access rights (User Roles):

img_Swagger_Magento_75

We recommend to follow a certain procedure: first you should create a role and then you should create a new user, and assign this role to that user. In this situation, we must note that providing all rights through the role is not a correct approach, but it is possible. In our example, the role of "GoMage" is assigned to the user "GoMage" and full access to any resources has been assigned to this role.

Authentication via the web-interface is not available in Magento 2 and you have to authenticate manually through universal REST API client, through code or through the "curl":

curl -X POST "http://magento213.gomage.com/rest/V1/integration/admin/token" \
     -H "Content-Type:application/json" \
     -d '{"username":"GoMage", "password":"dsgdifksFlsjgkcxvgdrgxj"}'

 

All you need to do is to send some JSON-text with the parameters of authentication to a specific address (don’t forget about the header with request content type). In reply you receive the session code to access the limited services.

The code will be received in quotation marks, but you should use it without them. Then you should enter the code in the appropriate field:

img_Swagger_Magento_8

And you get access to an extensive list of services.

Further work with the services is no different from working in the anonymous user mode. The session key for authentication is automatically added by Swagger

There is no doubt that using Swagger makes the work much more convenient and gives a lot of advantages. If you have any questions or propositions, contact us at support@gomage.com.

Newsletter