9/1/2015: IMPORTANT UPDATE: This blog post is now out of date. Please see this newer post for a better approach to explore the available APIs.
So you have Magento 2 but want to explore its new RESTful APIs. There is the Getting Started with Web APIs guide that introduces topics such as authentication. But sometimes you just want to see the list of available URLs. This blog contains a little script to make API visualization easier.
Please note that proper REST API documentation is planned, but not available yet. This post is to help in the short term.
For example, this script prints out URLs, but does not display any information about the content of any JSON or XML data structures passed to or from REST API calls.
The first step is to put the following PHP program into a file “rest.php”. This script must be run from the Magento 2 home directory for it to work correctly. (It has the path app/code hard coded into it.)
<? foreach (glob("app/code/Magento/*/etc/webapi.xml") as $webapixml) { $module = preg_replace('(app/code/Magento/(.*)/etc/webapi.xml)', '$1', $webapixml); print "==== $module ====\n\n"; $xml = simplexml_load_file($webapixml); foreach ($xml as $key => $value) { $attr = $value->attributes(); $method = (string) $attr["method"]; $url = (string) $attr["url"]; $line = " ".(str_pad($method, 6))." ".$url; print "$line\n"; } print "\n"; }
The script will output the URLs and HTTP actions supported by each module. This is useful to understand the coverage of the API. The following shows the URLs for the first few modules.
==== Backend ==== GET /V1/modules ==== Bundle ==== POST /V1/bundle-products/:sku/links/:optionId PUT /V1/bundle-products/:sku/links/:id GET /V1/bundle-products/:productSku/children DELETE /V1/bundle-products/:sku/options/:optionId/children/:childSku GET /V1/bundle-products/:sku/options/all GET /V1/bundle-products/options/types GET /V1/bundle-products/:sku/options/:optionId POST /V1/bundle-products/options/add PUT /V1/bundle-products/options/:optionId DELETE /V1/bundle-products/:sku/options/:optionId ==== Catalog ==== POST /V1/products PUT /V1/products/:sku DELETE /V1/products/:sku GET /V1/products GET /V1/products/:sku GET /V1/products/attributes/types GET /V1/products/attributes/:attributeCode GET /V1/products/attributes
Also of note is there is a directory of API tests in dev/tests/api-functional. In particular, you can turn on unit tests writing sample API REST calls and responses to disk by editing dev/tests/api-functional/phpunit.xml.dist to change the following constant to “true”.
<!--Generate documentation from REST tests and put it into var/log/rest-documentation directory--> <const name="GENERATE_REST_DOCUMENTATION" value="false" />
This constant is used by dev/tests/api-functional/framework/Magento/TestFramework/TestCase/Webapi/Adapter/Rest.php. Running the tests with PHP unit will generate quite detailed sample input and output parameter values based on the invoked test cases.
Hi Alan,
I’m changed GENERATE_REST_DOCUMENTATION = true and I run the rest.php file the Get & Post is displayed but sample document is not created from var/log/rest-documentation.
magentoroot/run.php
$value) {
$attr = $value->attributes();
$method = (string) $attr[“method”];
$url = (string) $attr[“url”];
$line = ” “.(str_pad($method, 6)).” “.$url;
print “$line\n”;
}
print “\n”;
}
Please suggest me is this procedure is correct or else wrong?
If it is wrong suggest me how can i do this?
If possible give me a example how to invoke REST & SOAP Magento 2 APIs?
Probably easiest to wait – a better way is coming really soon…
Hi Alan!
Tell me please, why REST API not return the delivery address.
No idea. If you think it could be a bug, try reporting on github. If you can provide repeatable instructions that can help a lot. Note github is not for support – forums are for that. But if you think it should be working but it is not, that crosses the line into a bug. Sample code repeating the problem (eg sample curl command) can make a big difference in repeating the issue and pushing through to resolution.