Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Follow the instructions given below to generate a REST API using a Swagger definition and then try it out from the Swagger UI. Alternatively, you can create a REST API using WSO2 Integration Studio.

Note

Note that this feature is currently available through the management console of the ESB profile.

Table of Contents

Prerequisites

The To use the Swagger UI requires you need to have configure CORS configured in order to function properly. The following steps should be followed to correctly configure CORS. Open the in the ESB server.

Open the synapse.propertiesfile (stored in the <EI_HOME>/conf/synapse.propertiesfile and add the following configuration 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.

Code Block
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

Notice the synapse.rest.CORSConfig.Access-Control-Allow-Origin property has a value of https://localhost:9443 and this will enable CORS for localhost. You can define as many URLs as desired as comma-separated values so that CORS will be enabled for all defined URLs.

Step 1: Generating the API

Follow the steps given below to generate the API using Swagger.

  1. Start the ESB profile of WSO2 EI.
  2. Open the management console of the WSO2 Enterprise Integrator and click on APIsin the Maintab. Click on Generate APIas depicted in the image WSO2 EI from the following: https://10.100.5.12:9443/carbon/
  3. Go to the ​Maintab, and click API to open the Deployed APIs screen.
  4. Click Generate APIas shown below.
    Image Modified
  5. The Swagger UI will then open.  On the left side, the The Swagger definition of a sample API is shown and on the right side, left and the details of the API is shown on the right.
    Image Added
  6. You can edit the Swagger definition as desired or import your own Swagger definitions by importing via a URL or uploading a definition file through the File menu of the Swagger editor.
    Image Removed
    Once done, click on the Generate API button or import a new one:
    1. Use the Edit menu on the Swagger editor to edit the current definition.:
      Image Added
    2. Use the File menu of the Swagger editor to import your own Swagger definition. You can point to a URL or you can upload a new file: 
      Image Added

  7. Click Generate API to generate the API as depicted below.
    Image Modified
    The synapse configuration of the generated API will then be shown.
    Image Modified
  8. Clicking Save will save the API and redirect . You are now redirected to the Deployed APIssection.
    Image Modified

     
  9. The following options are available for the API: 
    • 1 - The swaggerbutton will direct Swaggeroption directs to the swagger Swagger definition of the API.
    • 2 - The Try This APIbutton will direct option directs to the TryIt tool, which allows testing 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:

  1. Click Edit to open the Edit API view. Click on the Edit swagger button to , and then click Edit Swagger to view the Swagger editor.
    Image Modified
    On the left side, the editable swagger definition is shown and on the right side, API details along with the Try it out options are shown. More details about the Try it out tool is explained in the next sections of this guide.
  2. Once done with the editing, click on the Apply Changes button and it will redirect Once you finish editing, click Apply Changes and you will be redirected to the Edit API view. Click Save
    Image Added

  3. Save the changes in the Edit API view and the API . You will be saved and you will be redirected now be directed back to the Deployed APIsview.
    Image Removed

Step 3: Trying out the API

Let's test the API by executing a sample request:

  1. In the Deployed APIs view,

    click on the

    click Try This API

    to try out

     for the API

    with sample requests and responses. Clicking this will open the Try out tool

    . The Swagger Try It tool opens in a new browser tab. 

    Tip

    Note that this UI is accessible even without

    logging

    signing in to

    the

    WSO2 EI.


    Image Modified

  2. In the ​Try it out view, defined services of the API are shown and once you click on a service, it will expand and show the evaluate view of the selected serviceClick a service of the API to view the details.
    Image Modified
    Image Modified
  3. Click on the Click Try it outbutton and it will show the following view.
    Image Removed

    You can then edit the payload and also , set the content type of the request and the response. Click on response, and then click Executeto  to send the request.
    Image RemovedImage Added
  4. The response will be shown along with

...

  1. other details such as the cURL command used, request URL, response code

...

  1. , etc.
    Image Added