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.

WSO2 Cloud setup

Sign up and launch WSO2 Integrator in the browser-based cloud editor.

Step 1: Create the integration​

Note

In the cloud editor, you're already inside a project. Skip to Step 2.

  1. Open WSO2 Integrator.
  2. Select the Create New Integration card.
  3. Set Integration Name to HelloWorldAPI.
  4. Set Project Name to integration-as-api.
  5. Select Create Integration.
Create new integration form with Integration Name set to HelloWorldAPI and Project Name set to integration-as-apiCreate new integration form with Integration Name set to HelloWorldAPI and Project Name set to integration-as-api

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);
}
}
}