Follow the instructions given below to generate a REST API using a Swagger definition. Alternatively, you can create a REST API using WSO2 Integration Studio.
Note that this feature is currently available through the management console of the ESB profile.
Prerequisites
To use the Swagger UI you need to configure CORS in the ESB server.
Open the synapse.properties
file (stored in the <EI_HOME>/conf/
directory) and add the following configuration. Note that the synapse.rest.CORSConfig.Access-Control-Allow-Origin
property points to https://localhost:9443 as the value, and thereby enables CORS for localhost. You can add all the required URLs as a comma-separated list.
synapse.rest.CORSConfig.enabled=true synapse.rest.CORSConfig.Access-Control-Allow-Origin=https://localhost:9443,http://localhost:8280 synapse.rest.CORSConfig.Access-Control-Allow-Headers=content-type
Step 1: Generating the API
Follow the steps given below to generate the API using Swagger.
- Start the ESB profile of WSO2 EI.
- Open the management console of WSO2 EI, go to the Main tab, and click API to open the Deployed APIs screen.
- Click Generate API as shown below.
- The Swagger UI will then open. The Swagger definition of a sample API is shown on the left and the details of the API is shown on the right. You can edit this Swagger definition. Alternatively, you can import your own Swagger definition using a URL or by uploading a definition file through the File menu of the Swagger editor.
- Click Generate API to generate the API as depicted below.
The synapse configuration of the generated API will then be shown. - Save the API. You are now redirected to the Deployed APIs section.
- The following options are available for the API:
- 1 - The Swagger option directs to the Swagger definition of the API.
- 2 - The Try This API option directs to the TryIt tool, which allows you to test the API.
Step 2: Editing the API (Optional)
Once you have generated the API using Swagger, you have the option of editing the API as follows:
- Click Edit to open the Edit API view, and then click Edit Swagger to view the Swagger editor.
Once you finish editing, click Apply Changes and you will be redirected to the Edit API view.
Save the changes in the Edit API view. You will now be directed back to the Deployed APIs view.
Step 3: Trying out the API
Let's test the API by executing a sample request:
In the Deployed APIs view, click Try This API for the API. The Try it out tool opens in a new browser tab.
Note that this UI is accessible even without signing in to WSO2 EI.
- In the Try it out view, defined services of the API are shown. When you click on a service, it will expand and show the evaluate view of the selected service.
- Click Try it out and you will get the following view:
- You can edit the payload and also set the content type of the request and the response. Click Execute to send the request.
The response will be shown along with several other details such as the cURL command used, request URL, response code, etc.