This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Create and Publish an API from a Swagger Definition
A Swagger definition is a format that describes REST APIs. In this tutorial, you create and publish an API in WSO2 API Manager using a Swagger definition for an existing API.
- Sign in to the WSO2 API Publisher.
https://<hostname>:9443/publisher
- In theĀ APISĀ menu, click Add New API.
- SelectĀ I Have an Existing API.Ā ClickĀ Swagger URLĀ and give
http://petstore.swagger.io/v2/swagger.json
Ā as theĀ URL. ClickĀ Start Creating.
Edit the information as given below.
Field Sample value Name Petstore Context /petstore Version 1.0.0 - Notice that all the API resources are createdĀ automatically when the Swagger URL is specified.
- Click Edit SourceĀ to edit the Swagger file and remove security headers. This is required to invoke the API in the Store using the Swagger UI.
Remove the security tag from theĀ
/pet
POST resource given below.Ā This is required to enable API invocation using the API (store) console.Swagger - Post resource//remove the following code snippet security: - petstore_auth: - 'write:pets' - 'read:pets'
Remove the securityĀ
pet/{petId}
Ā GET resource given below:Swagger - Get resource//remove the following code snippet security: - api_key: []
- After removing the security tags, clickĀ Apply ChangesĀ to save the changes.
Then, clickĀ Next: Implement. Click theĀ Managed APIĀ option.Ā Enter the information shown below and click Next: Manage.
Field Sample value Endpoint type HTTP/REST endpoint Production endpoint http://petstore.swagger.io/v2/
Sandbox endpoint Providing only the production endpoint is sufficient. Select the options described in the table below.
Field Sample value Description Transports HTTP and HTTPS The transport protocol on which the API is exposed. Ā Both HTTP and HTTPS transports are selected by default. If you want to limit API availability to only one transport (e.g., HTTPS), un-check the other transport. Subscription Tiers Gold, Silver The API can be available at different levels of service. They allow you to limit the number of successful hits to an API during a given period of time. For more information on API authentication (e.g., non authentic API invocation), see HTTP methods.
- ClickĀ Save & Publish.Ā
Now you have successfully published an API using a Swagger defintion.
Let's invoke the API from theĀ API Store.
Invoking the API
Sign in to the WSO2 API Store.
https://<hostname>:9443/store
- Select the PetStoreĀ API to open it.
- Subscribe to the API using the DefaultApplicationĀ and the GoldĀ tier.
- ClickĀ View SubscriptionsĀ toĀ view all the APIs to which the Default Application has subscribed.
Click the Production Keys tab and generate a token.
- Navigate to theĀ API Console for the PetStore API.
Expand the
POST
Ā resource, click Try it out, and give the following example as the request body. Click Execute and view the response.You have successfully invoked the Petstore API.