Adding Documentation Using Swagger
Providing Interactive Documentation support helps users to understand and experience the APIs. WSO2 API Manager provides this functionality through the integration with Swagger. Swagger is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful Web services. In Swagger, when APIs are described in simple, static JSON representation, they can be loaded through the Swagger UI and made 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, Swagger UI discovers the API definition for each API and displays the interactive documentation under the API's Overview tab.
Explained below is how to get an interactive documentation for an API:
We use API named FindFeeds,
which is based on the online search functionality provided by Youtube (http://youtube.com/). We use http://gdata.youtube.com/feeds/api/standardfeeds as the Production Url/Endpoint.
Creating an API
- Log in to API Publisher Web interface (https://localhost:9443/publisher), and go to Add API page. Create new API with following information.
- Name: FindFeeds
- Context: /feeds
- Version: 1.0.0
- Appropriate image as the thumbnail
- Endpoint: http://gdata.youtube.com/feeds/api/standardfeeds
- Tiers: Gold, Silver, Bronze (select all 3 – this field supports multiple values)
- Business owner: Bruce Wayne
- Business owner e-mail: dark.knight@wayne-enterprises.com
- Technical owner: Peter Parker
- Technical owner e-mail: spidey@dailybugle.com
- Define API resources for the operations you need to perform.
Specify None as the Auth Type of OPTIONS
For each of the resource that has HTTP verbs requiring Authentication (i.e., Auth Type is not NONE), enable OPTIONS with None Auth type. For example, as the following screen shot shows, resources with /* URL Pattern has HTTP verbs with Auth Type as
Application & Application User
. Therefore, we must give None as the Auth Type of OPTIONS. This is to support CORS (Cross Origin Resource Sharing) between the API Store and Gateway. But, if no authentication is needed for any of the HTTP verbs, you don't have to specify None Auth type to OPTIONS. Publish the API to the API Store.
Invoking the interactive documentation
- Log in to the API Store Web interface (https://localhost:9443/store) and click the API published before.
- The API opens. Under the API's Try It tab, you can find the interactive documentation for
FindTweets
. - Click on GET operation to expand it. There, you can enter the Authorization Headers, Parameters and click Try it out to invoke.
- The response appears below. For example,
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
FindFeeds
API to see its API definition. 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.
- 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 https://github.com/wordnik/swagger-core/wiki/API-Declaration.
By default, all the POST and PUT operations have the Payload parameter, which you can use to send any payload when invoking the API. The GET, DELETE operations have Query parameters, which you can use to send URL-appended parameters. You can also add named parameters.
For example, to modify the path of the API created above with a parameter, edit the path and add the relevant parameter.
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 Key value description, that element will be deleted. The screenshot below shows path modified with a parameter.
The following screenshot shows parameter definition added for param and the default Payload parameter removed:
Also be sure to modify descriptions, notes, summary on each of the operations.
Once the modifications are done, save and Publish the API.
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 API Versioning.