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):
Enter the API name as โ{Username}_numbercalculationโ.
Select the API method as โGETโ.
Optionally add a description for the API.
Create a new Group Name by clicking [+].
Enter the Group name as โAPIcalculation".
Enter the Group base URL as โAPIcalculationโ.
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:
Query string name: โNumber1โ, Data Type: โintegerโ, Example of Query string: 50
Query string name: โNumber2โ, Data Type: โintegerโ, Example of Query string: 100
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:
Select Action as โUpdate Variableโ.
Select the Target variable as โN1โ.
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:
Select Action as โUpdate Variableโ.
Select the Target variable as โN2โ.
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:
Select Action as โCondition actionโ.
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:
Select Source as variables>Operation. It is displayed as โOperationโ.
Select Operator as โIs Equalโ.
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.โ
Select Action as โCalculateโ.
Select Target variable as โresultโ. This action adds the values of the variables N1 and N2 and stores them in the variable โresultโ.
Select Left Value as Variables>N1. It displays as โN1โ.
By default, the Operator is selected as โAddโ. Keep it as is.
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:
Select Action as โUpdate Variableโ.
Select Target Value as Response>defaultResponseJsonBody>result. It displays as โResponse.defaultResponseJsonBody.resultโ.
Select Value as Variables>result>toString(). It displays as โresult.toString()โ.

To send the response, add the last action and define it as follows:
Select Action as โAPI Respondโ.
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
