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