Skip to main content

Build an Integration as API

TimeUnder 10 minutes
What you'll buildAn HTTP service that listens on /hello/greeting, calls an external API, and returns the response to the caller.

An HTTP service exposes your integration logic as a REST endpoint. This quick start shows the full cycle: create a service, add a resource, connect to an external API, and test it using the Try-It/Test panel in WSO2 Integrator.

Prerequisites​

A working WSO2 Integrator environment.

Step 1: Create the integration​

  1. On the project home page, click Create on Cloud.

    WSO2 Cloud project home showing the Create on Cloud optionWSO2 Cloud project home showing the Create on Cloud option
  2. The WSO2 Integrator cloud editor opens in your browser. Add an artifact to your integration, then deploy it to WSO2 Cloud from the Deploy to WSO2 Cloud panel on the right.

    WSO2 Integrator cloud editor landing page showing an empty integration and the Deploy to WSO2 Cloud panelWSO2 Integrator cloud editor landing page showing an empty integration and the Deploy to WSO2 Cloud panel

Step 2: Add an HTTP service​

  1. Select your integration from the project overview canvas.
  2. Select + Add Artifact in the design canvas.
  3. Select HTTP Service under Integration as API.
  4. Keep Service Contract as Design From Scratch.
  5. Set Service Base Path to /hello.
  6. Select Create.
Selecting HTTP Service in the Add Artifact panel and setting the base path to /helloSelecting HTTP Service in the Add Artifact panel and setting the base path to /hello

Step 3: Add a resource​

  1. In the HTTP service design view, select + Add Resource.
  2. Select GET.
  3. Set Resource path to greeting.
  4. Select Save.
Adding a GET resource at the /greeting path in the HTTP service designerAdding a GET resource at the /greeting path in the HTTP service designer

Step 4: Connect to an external API​

  1. Select + inside the resource flow.
  2. Select Add Connection.
  3. Select HTTP.
  4. Set Url to https://apis.wso2.com/zvdz/mi-qsg/v1.0.
  5. Set Connection Name to externalApi.
  6. Select Save Connection.
Configuring an HTTP connection to the external API in the Add Connection panelConfiguring an HTTP connection to the external API in the Add Connection panel

Step 5: Call the external API​

  1. Select + inside the resource flow.
  2. Select externalApi.
  3. Select Get.
  4. Set Path to /.
  5. Set Result to response.
  6. Set Target Type to json.
  7. Select Save.
Configuring the GET call on the externalApi connection with path / and result variable responseConfiguring the GET call on the externalApi connection with path / and result variable response

Step 6: Return the response​

  1. Select + inside the resource flow after the external API call node we just added.
  2. Select Return.
  3. Set Expression to response.
  4. Select Save.
Adding a Return node with the expression set to responseAdding a Return node with the expression set to response

Step 7: Run and test​

  1. Select Run.
  2. Select Test in the confirmation dialog.
  3. Select Execute.
  4. Confirm the response shows 200 OK with a Hello World body.
Running the integration and testing it with the Try It panel showing a 200 OK responseRunning the integration and testing it with the Try It panel showing a 200 OK response

Step 8: Deploy to WSO2 Cloud​

Deploy your integration to WSO2 Cloud. Check Save and deploy.

Skip ahead: deploy a ready-made sample

Rather not build it yourself? One-click deploy the HelloWorldAPI sample straight to WSO2 Cloud.

Deploy to WSO2 Cloud

Source code​

View the generated Ballerina code

The steps above generate the following complete, runnable Ballerina program:

import ballerina/http;

listener http:Listener httpDefaultListener = http:getDefaultListener();

final http:Client externalApi = check new ("https://apis.wso2.com/zvdz/mi-qsg/v1.0");

service /hello on httpDefaultListener {

resource function get greeting() returns json|error {
do {
json response = check externalApi->get("/");
return response;
} on fail error err {
// handle error
return error("unhandled error", err);
}
}
}