Create and Publish an API from Swagger definition
Swagger definition is a format to describe REST APIs. In this tutorial you create and publish an API in WSO2 API Manager using Swagger definition, when you have an existing API.
- Sign in to the WSO2 API Publisher.
https://<hostname>:9443/publisher (ex: https://localhost:9443/publisher ).
Use admin as username and password. - In the APIS menu, click Add New API .
- Select I Have an Existing API. Select Swagger URL and type the "http://petstore.swagger.io/v2/swagger.json" URL in the text box. Click Start Creating
Give the information in the table below.
Field Sample value Name Petstore Context /petstore Version 1.0.0 Visibility Public Tags pets
- 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 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 belowSwagger - Get resource//remove the following code snippet security: - api_key: []
- After removing the security tags, click Apply Changes to save the changes.
Click Next: Implement.
Click the Managed API option. Enter the information in the table under the Implement tab. Click Next: Manage.
Field Sample value Endpoint type HTTP/REST endpoint Production endpoint http://petstore.swagger.io/v2/
Sandbox endpoint Providing production endpoint only is suffieient. Select options for Transports and Subscription Tiers.
The options are described in the table given 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 swagger defintion. Lets now invoke the API from API Store.
Invoking the API
Sign in to the WSO2 API Store.
https://<hostname>:9443/store (ex: https://localhost:9443/store).
Use admin as username and password- Select the PetStore API from the Store.
- Subscribe to the API using the "DefaultApplication" and using "Gold" tier. For more information, see Subscribe to an API
- Click on View Subscriptions in the pop up.
You can view the APIs that the Default Application has subscribed.
Click on Petstore - 1.0.0 from the API list.
Token generation
Generate the token for Default application, if you have not generated a token already/
- Go to the API Console for the PetStore API
Click on the
POST pet
resource and give the following example as the request body and click Try it out. Click to see the response.
You have now successfully invoked the Petstore API.