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