Interactive documentation support helps users to understand and experience the APIs better. WSO2 API Manager provides this functionality through the integration of Swagger (https://developers.helloreverb.com/swagger). Swagger is a specification and a complete framework implementation for describing, producing, consuming, and visualizing RESTful Web services. You can load APIs that are described in simple, static JSON representation through the Swagger UI and and make them available as interactive documentation.
The idea of this interactive console is allowing users to test the APIs and get to know how they respond without subscribing to the APIs. When an API is created in API Publisher, the JSON representation of that API is automatically generated and saved into the registry as an API definition. This API definition describes the API using the information provided at the time it is created. You can modify the API definition using the Doc tab in the management console. In API Store, the Swagger UI discovers the API definition for each API and displays the interactive documentation in the API's Documentation tab.
The sections below explain how to create an interactive documentation for an API:
Enabling cross-origin resource sharing
Swagger-based interactive documentation allows you to try out APIs from the documentation itself. It is a Java Script client that runs in the API Store and makes Java Script calls from the Store to the API Gateway. Since the API Store and Gateway run on two different ports, you must enable cross-origin resource sharing (CROS) between the two using CORS configuration in
<APIM_HOME>/repository/conf/api-manager.xml file. Given below is a sample configuration of CROS and a description of its XML elements:
|<Enabled>||True/False||Used to enable/disable sending CORS headers from the Gateway. By default, CORS is enabled (True). This is needed for Swagger to function properly.|
|<Access-Control-Allow-Origin>||HTTP and HTTPS Store Address. Change the Host and Port for correct values of your store. For example, ||The value of the |
Header values you need to pass when invoking the API. For example,
|Default values are sufficient for Swagger to function.|
|<Access-Control-Allow-Methods>||GET, POST, PUT, DELETE, OPTIONS||Methods required to be supported from the Swagger client.|
Creating an API
Updating the API definition
The API creator can update/customize the automatically generated API definition for each API.
- In the API Publisher, go to the Doc tab of
PhoneVerifyAPI and click Edit Content under Swagger Documentation.
API definition contains the JSON representation of the API. You can easily modify existing content, add/remove elements, change paths and parameters etc. using the JSON editor. A sample API definition is given below:
- API Manager 1.5.0 onwards has integrated JSONMate as the editor for modifying the API Definition.
- For the Swagger specification of API declaration, see
There are Two ways to modify the API definition. You can either use the Text Editor given, or the graphical tree editor.
By default, all the POST and PUT operations have the
Payloadparameter, which you can use to send any payload when invoking the API. You can use the
Query parametersin GET, DELETE operations to send URL-appended parameters (e.g.,: v=2&length=200). You can also add named parameters. For example, to modify the path of the API created above, edit the path and add the relevant parameter. I.e., if you want to send a query parameter called 'v' in the GET request of
top_ratedresource. The screenshot below shows
pathmodified with a parameter. Also, the default Query Parameter is changed to a parameter called
You can just modify the JSON content according to Swagger Specification.
Shown below is an example of the graphical editor. To add new elements, click Add New Value and provide the value. To delete elements, delete the value of Key. For example, if you delete the Key value named
description, that element will be deleted. Also be sure to modify descriptions, notes, summary on each of the operations.
Once the modifications are done, save.
Log in to the API Store and click on GET operation to expand it. There you can enter the parameters and try it out.
Next, see Adding Documentation Using Swagger.