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:

    1. Enter the API name as โ€œUserName_Acc_AddCustomerโ€. The name indicates that a customer has been added.

    2. Select the API method as โ€œPOSTโ€. This defines that the API will be posting or sending data.

    3. 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:

    1. Label the Request body as โ€œrequestJsonCreateCustomerโ€.

    2. Content type from the list of values is selected as โ€œapplication/jsonโ€. This defines the type of content in the request.

    3. Character set is selected as โ€œUniversal (UTF-8)โ€.

    4. Optionally enter a description for the โ€œRequest bodyโ€. (This is used in the API documentation.)

    5. 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).

    6. 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:

    1. By default, the name for the response body is โ€œdefaultResponseJsonBodyโ€. For this exercise, keep the response body name as is.

    2. The Content type from the list of values is selected as โ€œapplication/jsonโ€. This defines the type of content in the response.

    3. The Response Code is selected as โ€œ200 OKโ€.

    4. Character set is selected as โ€œUniversal (UTF-8)โ€.

    5. Optionally add a description for the โ€œResponse bodyโ€ (This is used in the API documentation).

    6. 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:

    1. Select Target entity as โ€œUserName_Acc_customersโ€ (created in Step 1).

    2. 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.

    3. โ€œ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:

    1. Variable Name: โ€œRequestโ€, Data Type: โ€œWebRequestโ€ This variable contains the Web Request received.

    2. 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:

    1. Enter Variable Name as โ€œAcc_customersโ€.

    2. 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:

    1. Select Action as โ€œUpdate Entity fields from jsonโ€.

    2. Select the Target variable as โ€œAcc_customersโ€.

    3. Select Source variable as Variables>Request>requestJsonCreateCustomer. It displays as โ€œRequest.requestJsonCreateCustomerโ€.

    4. 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:

    1. Select Action as โ€œUpdate Variableโ€.

    2. Select Target Variable as Acc_customers>Customer_Value. It displays as โ€œAcc_customers.Customer_Valueโ€.

    3. 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:

    1. Select Action as โ€œUpdate Variableโ€.

    2. Select Target variable as โ€œAcc_customers.Ratingโ€.

    3. 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.

    1. Select Action as โ€œCondition actionโ€.

    2. 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

    1. Select Source as Variables>Acc_customers>Join_Date. It displays as โ€œAcc_customers.Join_Dateโ€.

    2. 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โ€™.

    1. Select Source as Variables>Acc_customers>Join_Date>toInteger(). It displays as โ€œAcc_customers.Join_Date.toInteger()โ€.

    2. Select the Operator as โ€œIs Not Equalโ€.

    3. 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:

    1. Select Action as โ€œUpdate Variableโ€.

    2. Select Source as Acc_customers>Join_Date. It is displayed as โ€œAcc_customer.Join_Dateโ€.

    3. 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:

    1. Select Action as โ€œCreate Entity recordโ€.

    2. Select Source as โ€œEntityCreationResponseโ€. The entity is created in this response variable.

    3. 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:

    1. Select Action as โ€œUpdade Variableโ€.

    2. Select Target variable as Response>defaultResponseJsonBody. It displays as โ€œResponse.defaultResponseJsonBodyโ€.

    3. 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:

    1. Select Action as โ€œAPI Respond".

    2. 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