Versions Compared

Key

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

...

  1. Log in to the API Publisher and choose to design a new API.
  2. In the Design tab, give an API name, a context and a version, and click the Edit Source button under the API Definition section.
  3. The Swagger UI opens. Add the following code under the paths object and click Save. It adds a resource with two HTTP methods into the API.

    Tip

    Tip:

    In the below code, note that you have a resource defined with the URL pattern /CheckPhoneNumber under the paths object. This is followed by the HTTP methods GET and POST. For each HTTP method, the following parameters are defined.

    • responses: An object to hold responses that can be used across operations. See the Swagger specification for details.
    • x-auth-type: WSO2-specific object to define the authentication type of the method.
    • x-throttling-tier: WSO2-specific object to define the throttling tier of the method.responses: An object to hold responses that can be used across operations. See the Swagger specification for details.

     

    Code Block
    /CheckPhoneNumber:
        get:
          responses:
            '200':
              description: ''
          x-auth-type: "Application & Application User"
          x-throttling-tier: Unlimited
        post:
          responses:
            "'200"':
    {}          postdescription: ''
          x-auth-type: "Application & Application User"
          x-throttling-tier: Unlimited
          responses:
            "200": {}    
    Image Removed


    Image Added

  4. Click Apply Changes. Back in the API Publisher, note that a resource with two HTTP methods are added to the API.
    Image Added
    Image RemovedLet's say the backend of this API sends the response in XML format. Let's document this under the GET method in the resource that we just added. 
  5. Add Click Edit Source and add the following code under the GET method and click Save.

    Code Block
    produces: [
            - text/xml]

    Image RemovedImage Added

  6. Click Apply Changes. Back in the API Publisher, expand the GET method and note that the response content type has changed to XML. 

    Tip

    Tip: You can use this attribute to document the type of the response message that the backend sends. It doesn't do any message type conversion. You can add multiple values as a comma-separated list. E.g.,

    produces: "[
    - text/xml], [ application/json]"

    Image Removed

    Add Image Added

  7. Click Edit Source and add the following code under the GET method and click Save. It adds two parameters to the method.

    Code Block
    parameters:
            - description: Give the phone number to be validatedname: PhoneNumber
              in: query
              namerequired: PhoneNumbertrue
              type: string
              requireddescription: trueGive the phone number to be validated
        in: query   - name: LicenseKey
       - description: Give the license key as 0 for testing purposein: query
               namerequired: LicenseKeytrue
              type: string
              requireddescription: trueGive the license key as 0 for     in: querytesting purpose

  8. Click Apply Changes. Back in the API Publisher, note the two parameters with their descriptions that are added under the GET method.
    Image RemovedIn the Swagger UI, Image Added
  9. Click Edit Source again and add the following summary and description to the GET method and click Save.

    Code Block
    summary: Check the validity of your phone number
    description: "Phone Verification validates a telephone number and returns carrier information, location routing etc."

  10. Click Apply Changes. Back in the API Publisher, note the summary and description appearing under the GET method.
    Image RemovedImage Added
  11. In the Swagger UI, add the following code under the POST method and click Save. It adds two parameters as PhoneNumber and LicenseKey to pass the payload in. It also changes the datatypes of the parameters to application/x-www-form-urlencoded as the backend expects that datatype.

    Code Block
    parametersconsumes:
            - description: Give the phone number to be validated
    application/x-www-form-urlencoded
          parameters:
            - name: PhoneNumber
              requiredin: trueformData
              typerequired: stringtrue
              intype: formDatastring
             - description: Give the license key as 0 for testing purpose phone number to be validated
            - name: LicenseKey
              namein: LicenseKeyformData
              required: true
              type: string
              indescription: formDataGive consumes: the license key as 0 for    - application/x-www-form-urlencodedtesting purpose

  12. Click Apply Changes. Back in the API Publisher, note the parameters that you added are visible under the POST method.
    Image RemovedImage Added

  13. In the Swagger API, change the title of the API as follows. This is the title that is visible to the consumers in the API Console of the API Store, after the API is published.

    Code Block
    info:
      title: PhoneVerificationAPI


    You will see how this change is reflected in the API Store in the last step of this tutorial15. 

  14. Complete Click Apply Changes and complete the rest of the API creation process. Once you are done, click Save and Publish on the Manage tab to publish the API to the API Store.
  15. Log in to the API Store and click on the API that you just published.
    Image RemovedImage Added
  16. The API opens. Go to its API Console tab and note the changes that you did earlier now appearing in the API Store for consumers.
    Image RemovedImage Added

In this tutorial, you have seen how the integrated Swagger UI can be used to design, describe and document your API, so that the API consumers get a better understanding of what the API's functionality is.