read

Magento 2 uses Swagger to automatically generate documentation for its REST API. You can view a published version on Magento devdocs. What's even better - Swagger is shipped with each installed instance of Magento 2 and you can simply visit http://example.com/swagger to see the API documentation that is relevant to exactly your site.

This is great as it allows us to, for example confirm, that our Extension Attributes show up in the required API responses.

As the API documentation could provide internal implementation details of a site, you first need to authenticate via an api key to obtain access to the full set of endpoints.

The api key to enter could be created via the integrationAdminTokenServiceV1 endpoint or via the below curl command:

curl -X POST 'https://example.com/index.php/rest/V1/integration/admin/token' \
     -H 'Content-Type:application/json' \
     -d '{"username":"admin", "password":"admin123"}'

The returned value then just needs to be pasted into the api_key field and hit apply. We can now start exploring the API endpoints.

Another neat feature is that Swagger allows you to execute the different endpoints directly from the browser to see what the return values are. Use the "Try it out" buttons.

Blog Comments powered by Disqus.
Kristof Ringleff

Kristof Ringleff

Founder and Lead Developer at Fooman


Published