Skip to content

How to Expose an Integration Service as a Managed API

What you'll build

In this tutorial, you'll define an integration service using Micro Integrator Extension for Visual Studio Code (MI for VS Code) and expose it as a managed API to the API marketplace. API consumers then discover the API from the marketplace, subscribe to it, and use it for application development.

exposing integration service as a managed api

This demonstrates how the integration components and API management components of WSO2 API Manager work together to enable API-led integration. The following diagram illustrates the various API Manager components and the different user roles that are involved in implementing this process:

exposing integration service as a managed api

  1. An Integration Developer creates the service using Micro Integrator Extension for Visual Studio Code (MI for VS Code) and deploys it in the Micro Integrator runtime.

    Note

    The integration service is designed to communicate with a back-end service (representing a Hospital) and get details of available doctors for various specializations.

  2. An API Creator converts the integration service to a managed API (apply security, rate limiting, etc.).

  3. An API Publisher publishes the API to the API marketplace (Developer Portal).
  4. An API Consumer (application developer) discovers and uses this API from the Developer Portal.

Concepts and artifacts used

The following concepts and artifacts are used in this tutorial:

Let's get started!

Follow the steps given below to build this use case and try it out.

Step 1: Set up the workspace

The following software and configurations are required to proceed with this tutorial:

  • Visual Studio Code (VS Code): with the Micro Integrator extension installed.
  • Java Development Kit (JDK): Ensure the JDK is properly configured in your system's PATH environment variable.
  • Apache Maven: Ensure Apache Maven is installed and its path is correctly set within the system's PATH environment variable.

Info

Follow the Install Micro Integrator for VS Code documentation for a complete installation guide.

Step 2: Develop the integration artifacts

Follow the instructions given in this section to create and configure the required artifacts.

Create an Integration project

The Integration project will contain all the required artifacts for the integration solution.

  1. Launch VS Code with the Micro Integrator extension installed.

  2. Click on the Micro Integrator icon on the Activity Bar of the VS Code editor.

    MI VS Code Extension

  3. Click Create New Project on Design View.

    Design View Pane Create New Project

    Next, the Project Creation Form will be opened.

  4. In the Project Creation Form, enter ServiceCatalogSample as the Project Name.

  5. Provide a location under Select Project Directory.

    create new project

  6. Click Create.

Now let's start designing the integration by adding the necessary artifacts.

Create HTTP Connection

  1. Navigate to Micro Integrator Project Explorer.

  2. Click on the + symbol to open the Add Artifact pane.

    add artifact

  3. Click + View More under Create an Integration.

  4. Select Connections under Other Artifacts to open the Connector Store Form.

  5. Select Http.

  6. In the Add New Connection form, specify the following values to create the new HTTP connection.

    Property Value Description
    Connection Name QueryDoctorConn The name of the connection.
    Base URL http://localhost:9090/healthcare The base of the request URL for the back-end service.

  7. Click Add.

Create a REST API

A REST API is required for receiving the client requests and the REST resource within the API will define the mediation logic that will send requests to the Healthcare back-end service and retrieve the available doctor information.

  1. Go to MI Project Explorer > + Add Artifact > API.

  2. Specify values for the required REST API properties:

    Property Value Description
    Name HealthcareAPI The name of the REST API.
    Context /healthcare Here you are anchoring the API in the /healthcare context. This will become part of the name of the generated URL used by the client when sending requests to the Healthcare service. For example, setting the context to /healthcare means that the API will only handle HTTP requests where the URL path starts with http://host:port/healthcare.

    synapse API artifact

  3. Click Create. This opens the Service Designer interface.

    You can now start configuring the API resource.

  4. Click on the GET API resource under Available resources on the Service Designer.

    You will now see the graphical view of the HealthcareAPI with its default API Resource.

  5. Click the Edit icon to edit the API resource.

    edit icon

  6. Specify values for the required resource properties:

    Property Description
    Resource Path /querydoctor/{category}
    This defines the request URL format. In this case, the full request URL format is http://host:port/querydoctor/{category} where {category} is a variable.
    Methods GET
    This defines that the API resource only handles requests where the HTTP method is GET.

    edit API resource

  7. Click Update.

Create the mediation logic

You can now configure the mediation logic to handle requests.

  1. To get started, click on the + icon to add the first mediator to the sequence.

    add log

  2. Select Log mediator in the Mediators section. The Log mediator logs messages when the request is received by the API resource. In this scenario, let's configure the Log mediator to display the following message: “Welcome to the HealthcareService”.

  3. Once you select the Log mediator, the Log pane will be opened. Fill in the information in the table below:

    Field Value Description
    Log Category INFO Indicates that the log contains an informational message.
    Message Welcome to HealthcareService Indicates the message.
    Description Request Log The Description field provides the name that appears for the Log mediator icon in the design view.

  4. Click Submit to save the Log mediator configuration.

    Let's configure a GET operation to send the request message to the HealthcareService endpoint and receive the response message.

  5. Click on the + icon in the sequence to add a GET operation for QueryDoctorConn after the Log mediator. From the palette, select Mediators > HTTP > GET operation.

    http connector get operation

  6. Once the GET operation form is opened, fill in the information in the table below:

    Field Value Description
    Connection QueryDoctorConn The connection to be used to execute the operation.
    Relative Path /${params.pathParams.category} Indicates the relative path added to the base URL of the connection. In this case, the variable 'category' that needs to be included in the request for querying doctors is represented as {params.pathParams.category} in the relative path.

  7. Click Submit.

    Now let's add a Respond mediator at the end of the sequence to send the response message from the healthcare service back to the client.

  8. Click on the + icon below the GET operation in the sequence to add a Respond mediator.

  9. From the Palette, select Respond mediator under the Mediators section.

  10. Click Submit.

You have successfully created all the artifacts that are required for sending a request through the Micro Integrator to the back-end service.

integration sequence with http connector

Step 3: Configure the Micro Integrator

Prerequisites

Before you begin, install Micro Integrator on your machine:

  1. Go to the WSO2 Micro Integrator web page, click Download, provide the necessary details, and then click Zip Archive to download the Micro Integrator distribution as a ZIP file.

  2. Extract the ZIP file. The extracted folder will be referred to as the <MI_HOME> folder.

The Micro Integrator can automatically publish artifacts to the Service Catalog in the API Publisher portal.

Let's enable this feature in the Micro Integrator.

  1. Uncomment the [[service_catalog]] section as shown below and change the APIM server configurations accordingly in the <MI_HOME>/conf/deployment.toml file.

    Tip

    The default username and password for connecting to the API gateway is admin.

    [[service_catalog]]
    apim_host = "https://localhost:9443"
    enable = true
    username = "admin"
    password = "admin"
    

Note

Since the integration developer may not know the API URL when it gets deployed in the Micro Integrator, the URL of the API will be parameterized as https://{MI_HOST}:{MI_PORT}/healthcare which will be resolved later using environment variables MI_HOST and MI_PORT respectively. By default, localhost will be used as the MI_HOST and 8253 as the MI_PORT. Depending on your deployment, you may need to update these environment variables.

Step 4: Start the API Manager runtime

Let's start the API Manager runtime before starting the Micro Integrator.

  1. Download and set up WSO2 API Manager.
  2. Start the API-M server.

Step 5: Build and run the artifacts

  1. Click on the Command Palette at the top of the VS Code.

  2. Type > to show the available commands.

  3. Select MI: Add MI server.

  4. Select Add MI server.

  5. Select the folder where <MI_HOME> is located. This will be set as the current server path.

    Current server path

  6. Run the project.

    Click the Build and Run icon located in the top right corner of the VS Code.

    Build and run

When the Micro Integrator starts, the integration service will be deployed to the Service Catalog during server startup. You will see the following in the server start-up log.

Successfully updated the service catalog

Step 6: Create and Deploy the API

Create the API

Let's expose the integration service as a managed API.

  1. Sign in to the API Publisher portal: https://localhost:9443/publisher.

    Tip

    Use admin as the username and password.

  2. You can also click the hamburger icon on the upper-left and click Services to see the available services.

    open service catalag

  3. Open HealthcareAPI from the above list.

    API created from service catalog

  4. Click Create API in the above screen to open the Create API dialog box.

    create api dialog box

  5. Specify an API name, context, and version, and then click Create API.

    Tip

    You will find these values already populated based on the information in the integration service.

You can now see the new API's overview page.

new api view

Note

  • You can use the left-hand navigation to explore the new API.
  • Click Endpoints in the left-hand navigator. You will see that the new API uses the integration service deployed in the Micro Integrator as the endpoint (backend).

    endpoint view

Select business plans

Let's allocate some business plans for the API.

  1. Go to the API overview and click Business Plan.

    click to add business plan

  2. Select at least one business plan for the API and save.

    add business plans to api

Deploy API in the Gateway

Let's deploy the API in a gateway environment.

  1. Go to the API overview and click Deploy.

    Tip

    This opens the Deployment tab in the left-hand navigator.

    open the deployment options

  2. Click Default to specify the gateway environment and host.

    Tip

    This setting deploys the API in Production as well as Sandbox gateways. Find out more about gateway environments.

    select gateways for the deployment

  3. Optionally, you can add a description.

  4. Click Deploy.

You will now see the deployment as the first revision of the API:

api first revision

Step 7: Publish the API

Go to the API overview in the Publisher portal and click Publish for the HealthcareAPI as shown below.

The API is now available in the Developer portal for consumers to access.

Step 8: Use the API

Before you begin

Let's start the back-end hospital service.

  1. Download the JAR file of the back-end service from here.
  2. Open a terminal and navigate to the location of the back-end service
  3. Execute the following command to start the service:

    java -jar Hospital-Service-JDK11-2.0.0.jar
    
  1. Sign in to the Developer portal: https://localhost:9443/devportal/apis.

    Tip

    Use admin as the username and password.

  2. Go to the API tab. The HealthcareAPI is listed as shown below.

  3. Select the HealthcareAPI to open the API overview.

  4. Click API Console under Try Out for the HealthcareAPI in the Developer portal as shown below.

  5. Enter the username and password for Basic Authentication.

    Tip

    Use admin as the username and password.

  6. Expand the /querydoctor/{category} resource and click Try it out.

  7. Specify surgery as the doctor category.
  8. Click Execute.

You will get the response message from the Healthcare service, if you send the category as surgery:

[
    {
        "name":"thomas collins",
        "hospital":"grand oak community hospital",
        "category":"surgery",
        "availability":"9.00 a.m - 11.00 a.m",
        "fee":7000.0
    },
    {
        "name":"anne clement",
        "hospital":"clemency medical center",
        "category":"surgery",
        "availability":"8.00 a.m - 10.00 a.m",
        "fee":12000.0
    },
    {
        "name":"seth mears",
        "hospital":"pine valley community hospital",
        "category":"surgery",
        "availability":"3.00 p.m - 5.00 p.m",
        "fee":8000.0
    }
]

Now, check the OUTPUT tab of VSCode and you will see the following message:

INFO {LogMediator} - {api:HealthcareAPI} Log Property message = "Welcome to HealthcareService"

Tip

For detailed instructions see Invoke an API using the Integrated API Console.