Step 1: Create API

To create the API, follow the steps below.

  • To go to the API section, click “API” under the “APIs and Integration” menu on the left-side panel.

  • To create a new API, click the [+ API] button.

  • The “Details” tab is preselected. To define the API, add the following information (steps illustrated in the picture below):

    1. Enter the API name as “{Username}_numbercalculation”.

    2. Select the API method as “GET”.

    3. Optionally add a description for the API.

  • Create a new Group Name by clicking [+].

    1. Enter the Group name as “APIcalculation".

    2. Enter the Group base URL as “APIcalculation”.

    3. To save the Group name details, click [Save].

  • Add a constant API Path as “numbercalculation” and press Enter. It will result in the API path in a blue bubble.

  • The URL for the API is generated.

  • Enable “public documentation” (at the bottom left). This defines the API documentation to be accessed publicly.

  • To define the Request body and Response body details, go to the “Inputs & outputs” tab.

  • To define request parameters, under “Parameters”, go to the “Query strings” section. To add a Query string, click the [+ Query string] button.

  • To define Query string for user to enter the values for Number1, Number2, and the type of operation, add and define the following query strings:

    1. Query string name: “Number1”, Data Type: “integer”, Example of Query string: 50

    2. Query string name: “Number2”, Data Type: “integer”, Example of Query string: 100

    3. Query string name: “operation”, Data Type: “string”, Example of Query string: operation

  • To define the response body, click on “Response body”. By default, a response body is already created.

  • Expand the section. To define the sample Json in the Response, in the Sample Box, type: {"result": "Result of addition"}

  • To define the actions, go to the “Code” tab. An Action box is already created.

  • To label the Action box, click on the “onExit” text and update it to “Perform the Calculations”.

  • Expand the Action box. By default, the Action “API Respond” with response selected as “defaultResponseJsonBody” is already created.

  • To define the variables to be used in the actions in this “Action box”, go to the “Variables” tab. To add a new variable, click on the [+ Variable] button.

  • To hold the values of the numbers to be calculated, the operation to be performed and, the result to be stored, create four (4) variables.

  • Define the first variable as: Variable Name: “N1”, Data Type: “integer” This variable holds a value of integer type for the first number.

  • Define the second variable as: Variable Name: “N2”, Data Type: “integer” This variable holds a value of integer type for the second number.

  • Define the third variable as: Variable Name: “operation”, Data Type: “string.” Initialize it to the operation received in the Query String “operation” by selecting Variables>Request>querystrings>operation. It displays as “Request.querystrings.operation”. This variable holds a value of string type for the operation to be performed on the two (2) digits stored in “N1” and “N2”.

  • Define the fourth variable as: Variable Name: “result”, Data Type: “integer” This variable holds a value of integer type for the result of the numbers/digits stored in “N1” and “N2”.

  • To define the actions for this calculation, go to the “Code” tab in the “Action box”. Remove the existing action “API Respond” for now; it will be added later after all the actions are defined. (as no Action is executed after “API Respond”) To add an Action, click the (+) sign in the “Action box”.

  • To update the value of “Number1” received in the query string to the variable “N1”, define the first action as follows:

    1. Select Action as “Update Variable”.

    2. Select the Target variable as “N1”.

    3. Select Value variable as Variables>Request>QueryString>Number1>toInteger(). It is displayed as “Request.QueryString.Number1.toInteger()”.

  • To update the value of “Number2” received in the query string to the variable “N2”, add and define an Action as follows:

    1. Select Action as “Update Variable”.

    2. Select the Target variable as “N2”.

    3. Select Value variable as Variables>Request>QueryString>Number2>toInteger(). It is displayed as “Request.QueryString.Number2.toInteger()”.

  • To verify if the value of “operation” received in the query string is “Add”, add and define an Action as follows:

    1. Select Action as “Condition action”.

    2. Add description as “Add Operation”.

  • To check if the operation value entered by the user is for addition, add a Criteria bar in the “Criteria” tab by clicking (+). (If the criteria returns True, the Actions in the Code tab>On True section will be executed.)

  • To verify if the value of the operation is “Add”, define the following fields:

    1. Select Source as variables>Operation. It is displayed as “Operation”.

    2. Select Operator as “Is Equal”.

    3. Enter constant text as “Add” and press Enter. It displays in a blue bubble.

  • Go to the Code tab>On True section of the “Condition action” and add a new Action.

  • To define the actions if the criteria returns True, in the “On True” section, define the action to add the values stored in the Query Strings, “N1” and “N2.”

    1. Select Action as “Calculate”.

    2. Select Target variable as “result”. This action adds the values of the variables N1 and N2 and stores them in the variable “result”.

    3. Select Left Value as Variables>N1. It displays as “N1”.

    4. By default, the Operator is selected as “Add”. Keep it as is.

    5. Select Right Value as Variables>N2. It displays as “N2”.

  • To convert the value of result into a string data type and update it to the response, add another Action and define it as follows:

    1. Select Action as “Update Variable”.

    2. Select Target Value as Response>defaultResponseJsonBody>result. It displays as “Response.defaultResponseJsonBody.result”.

    3. Select Value as Variables>result>toString(). It displays as “result.toString()”.

  • To send the response, add the last action and define it as follows:

    1. Select Action as “API Respond”.

    2. Select Response as “defaultResponseJsonBody”.

  • The Code tab>On True section of the Condition action should look as per the image below:

  • .To save the API, click the [Save] button.

  • To publish the API, click the [Publish] button.

  • To add the API gateway for this API, go to the Details tab and click the [+] button with the field “Select or add default API gateway.” To define an API gateway for this API, add the API gateway name as “UserName_numbercalculation” and save the API again.

  • To make the API publicly available, it must be enabled. In the Details tab, toggle the “Disabled” button to change it to “Enabled”.

Last updated