Step 1: Create API
Last updated
Last updated
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β.