Step 2: Create the API
Once the Entity is created, configure an API to create customer account records and store them in the โUserName_Acc_customersโ Entity created in Step 1.
To go to APIs, click โAPIโ under the โAPIs and integrationโ menu on the left side panel.
To create a new API, click the [+ API] button.
The New API page displays with the Details tab preselected. To define the API, add the following information:
Enter the API name as โUserName_Acc_AddCustomerโ. The name indicates that a customer has been added.
Select the API method as โPOSTโ. This defines that the API will be posting or sending data.
Optionally add a description for the API.
Create a new Group Name, "UserName", by clicking [+].
Add constant text for API Paths as โaccโ and โaddcustomerโ. Each of both parameters displays in a blue bubble.

The URL for the API is generated.
Enable โpublic documentationโ. 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 the Request body, go to the Inputs & outputs tab>Request body section. Click it to expand the already created Request body. It is defined with the following information:
Label the Request body as โrequestJsonCreateCustomerโ.
Content type from the list of values is selected as โapplication/jsonโ. This defines the type of content in the request.
Character set is selected as โUniversal (UTF-8)โ.
Optionally enter a description for the โRequest bodyโ. (This is used in the API documentation.)
To add a sample JSON from an entity, select the entity โUserName_Acc_customersโ from the โCopy sample json fromโ drop-down menu (this is the Entity created in Step 1).
A json object is populated representing all the created fields in the entity โUserName_Acc_customersโ.
To define the Response body, go to the Inputs & outputs tab>Response body section and click it to expand it. A Response body is already created and defined with the following information:
By default, the name for the response body is โdefaultResponseJsonBodyโ. For this exercise, keep the response body name as is.
The Content type from the list of values is selected as โapplication/jsonโ. This defines the type of content in the response.
The Response Code is selected as โ200 OKโ.
Character set is selected as โUniversal (UTF-8)โ.
Optionally add a description for the โResponse bodyโ (This is used in the API documentation).
To define a sample Json for the response, type the following json object:
{ "status": "success or failed","errorcode": "0","message": "The response of creating the entity record.","details": [] }
This object indicates that: - The status is โsuccessโ or โfailedโ. - The error code is zero (0). - The message displayed is โThe response of creating the entity record.โ - The details will be a list of values as identified by the two square brackets โ[].โ
To define the Target Entity for this API, go to the โTarget Entitiesโ tab. Target entities are the entities that the Actions in this API can process data into i.e., perform add, update or delete upon. To create a new target entity, click [+ Target entity].
To define the Target entity for storing the created record:
Select Target entity as โUserName_Acc_customersโ (created in Step 1).
Keep โSelect a default Validation pipelineโ as is. If a Validation pipeline needs to be applied to the data before storing in an entity, the relevant Validation pipeline can be selected in this field. In this exercise, no Validation pipeline is applied.
โShow in API gatewayโ toggle button is enabled by default. This must be enabled so that the API is visible to the API gateway through which it can process the information into the entity.

To see the default variables created for an API, go to the โVariablesโ tab. The following two (2) Read Only variables are predefined:
Variable Name: โRequestโ, Data Type: โWebRequestโ This variable contains the Web Request received.
Variable Name: โResponseโ, Data Type: โWebResponseโ This variable contains the Web Response sent.
To create Actions for this API, go to the โCodeโ tab. By default, an โAction boxโ is already created with the description of the โAction boxโ as โOnExitโ. To change the label of the โAction boxโ, double click the description and label the โAction boxโ as โAdd request to Entity Acc_customerโ. (In this Action box, the records will be updated in the Target entity based on the request received.)
Expand the "Action box", an Action called โAPI Respondโ is already added. Notice the variable as โdefaultResponseJsonBodyโ.
Remove the existing Action โAPI Respondโ. (It will be added later after all the Actions are defined as no Action is executed after โAPI Respondโ.)
To define the variables, go to the โVariablesโ tab of the โAction boxโ. The variables created here will cover the scope of the โAction boxโ. To create a new variable, click the [+ Variable] button.
To contain the values for the fields of the desired entity in a record, define the variable as follows:
Enter Variable Name as โAcc_customersโ.
Select Data Type as the Entity โUserName_Acc_customersโ.
To define the Actions, click on the โCodeโ tab in the โAction boxโ and add an Action.
To update the variable โAcc_customersโ with details (from the json object) for the customer fields defined in the Entity โUserName_Acc_customersโ , define the first Action as follows:
Select Action as โUpdate Entity fields from jsonโ.
Select the Target variable as โAcc_customersโ.
Select Source variable as Variables>Request>requestJsonCreateCustomer. It displays as โRequest.requestJsonCreateCustomerโ.
Select the checkbox โUnset missing fieldsโ. By this setting, if the field is not found in the json request, the Action will set the field in the target variable (โAcc_customerโ in this case) as unset.
To define all the Actions, add six (6) more Actions by clicking (+) the button.
To update the value of โCustomer_Valueโ received via โRequest Bodyโ to โzero (0)โ, define the second Action as follows:
Select Action as โUpdate Variableโ.
Select Target Variable as Acc_customers>Customer_Value. It displays as โAcc_customers.Customer_Valueโ.
Add Value as โ0โ (without quotes) and press Enter.

To update the value of โRatingโ received via โRequest Bodyโ to โone (1)โ, define the third Action:
Select Action as โUpdate Variableโ.
Select Target variable as โAcc_customers.Ratingโ.
Add Value as โ1โ (without quotes) and press Enter. It displays in a blue bubble.

To check if the โJoin_Dateโ value is received in the โRequest Bodyโ, define the fourth Action such that if the Join Date is received, it will display the date as per the given value in Join Date, else it will update the date to current date and time.
Select Action as โCondition actionโ.
Add Condition Name as โcheck join dateโ.

Under the Criteria tab, add 2 Criteria bars by clicking the (+) button.
To check if a value for the โJoin_Dateโ has been received, define the first Criteria bar as follows
Select Source as Variables>Acc_customers>Join_Date. It displays as โAcc_customers.Join_Dateโ.
Select the Operator as โIs Setโ.

To check if a value for the โJoin_Dateโ is not equal to zero (0), define the second Criteria bar to convert the value of โJoin_Dateโ from string data type to integer and check if the value is not equal to zero โ0โ.
Select Source as Variables>Acc_customers>Join_Date>toInteger(). It displays as โAcc_customers.Join_Date.toInteger()โ.
Select the Operator as โIs Not Equalโ.
Enter the Value as โzero (0)โ.

The AND logical operator displays indicating that both of these criteria must be validated before execution of the โCodeโ tab in the Condition action.
To define the Actions in case the return as โFalseโ, go to the Code tab>On False section of the โCondition actionโ.
Add an Action to update the value of the โJoin_Dateโ to current Date and Time if the โJoin_Dateโ is not received. Define the Action:
Select Action as โUpdate Variableโ.
Select Source as Acc_customers>Join_Date. It is displayed as โAcc_customer.Join_Dateโ.
Select Value as Functions>DateTime>Now(). It is displayed as โDateTime.Now()โ. The value will be of the current date and time.

To update the information received in the EntityCreationResponse (Json object) to a record in the Entity โUserName_Acc_customersโ, after the Condition action define the fifth Action as follows:
Select Action as โCreate Entity recordโ.
Select Source as โEntityCreationResponseโ. The entity is created in this response variable.
Select the Entity variable as โAcc_customersโ. This entity variable is the source from which the values are taken.

To convert the information received in โEntityCreationResponseโ to Json and update it in the default Response, define the sixth Action as follows:
Select Action as โUpdade Variableโ.
Select Target variable as Response>defaultResponseJsonBody. It displays as โResponse.defaultResponseJsonBodyโ.
Select the Entity variable as Variables>EntityCreationResponse>toJson(). It displays as โEntityCreationResponse.toJson()โ.

To send response through the predefined response variable, define the last Action as follows:
Select Action as โAPI Respond".
Set Response as โdefaultResponseJsonBodyโ.

After all the Actions are created, the โCodeโ tab in the Action box should look as follows:

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

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

To define the API gateway, go to the Details tab and add the API gateway for this API as โUserName_Acc_AddCustomerโ. Save the API again.

To save the API again, click the [Save] button.
In the โDetailsโ tab and on the right-hand side, ensure the API is enabled.
Last updated
