Back to Top

Magento 2 RESTful API with Swagger UI

Updated 6 March 2024

Today we are going to understand how to use magento2 swagger ui, magento 2 provides REST API for accessing magento resources from any other devices, magento 2 REST API is based on OpenAPI Specification(OAS) (or Swagger Specifications), OAS are meant for how we can describe, structure, and visualize api resources.

Any one who follows swagger specifications can easily use many open source tools to automatically generate API documentation, code and test cases. Swagger specifications are meant for standardising the RESTful API  development and consumption.

You can learn more about swagger specifications from here:

http://swagger.io/specification/

Magento 2 RESTful API is based on swagger specification and uses swagger ui tool to generate and visualize API documentation so it can be easily understood and used.

Searching for an experienced
Magento 2 Company ?
Find out More

Magento 2 provides Magento_Swagger module to list, visualize and at the same time test the API resources.

If you have installed Magento 2 on your server then you can easily access all the api resouces listed on your website by just hitting this url:

http://website-base-url/swagger

example: http://www.xyz.com/swagger

after hitting the above url you will see this interface:

swa-1

All the API resources will be listed like in the above image, you can click on each resource to check all the details about the api, resource url, method, params and even you can execute the API call on the same place and check the response.

You will be thinking, with this help anyone can check any details on your Magento 2 website by just going though the swagger link, but that’s not the case, you can only have guest level resources from here, untill you provide a valid api key.

Using Magento 2 token based authentication you can generate api key, and use that api key directly on swagger interface to access even restricted resources.

Suppose you are an admin and want to test admin level resources and test them directly on swagger interface, first you need to generate admin level access key with following command with cURL like below:

curl -XPOST -H 'Content-Type: application/json' http://magento-url/rest/V1/integration/admin/token -d '{ "username": "user-name", "password": "admin-password" }'

Then you will get the access key to access admin level resource, you can fill the API key in the swagger ui to access the resources like below:

sw

After filling the api key click on apply and you will get admin level access in the swagger ui. Same goes for accessing customer level resources just generate the customer level access key and use it on the swagger ui.

Hope it will help you if you are trying to use Magento2 REST API.

Thanks ­čÖé

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Vineet
    • ashutosh srivastava (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home