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