LogoLogo
HomeCommunityLangstack.comCreate free account
  • 🤝Welcome
  • 🙌Support
  • 💡Get started
    • 📕Learn Langstack
      • Introduction to Langstack
        • Application templates
        • Storage frameworks
        • Account administration
      • Basics and essentials
        • What is an API gateway?
        • How to work with an API gateway
        • What is an Entity?
        • What is an Action box?
          • How to create and work with Action boxes
          • How to perform search in Actions
        • What is an Action?
          • How to create and work with Actions
          • Error propagation
          • Actions in Langstack applications
        • What is a Data type?
          • Simple Data types
        • What is a Variable?
          • Naming rules of a Variable
        • What is a Function?
          • Working with Functions
        • Navigating to a Variable or a Function
        • Implicit Type Conversion (ITC)
        • Explicit Type Conversion
        • Implicit and Explicit Type Conversion for Json
        • Next Steps
      • User management
        • What is a Privilege?
        • What are Groups?
        • What are Users?
      • Introduction to REST API
        • API Name
        • API Group name
        • Request and Response body
          • Request body
          • How to create and work with a Request body
          • Response body
          • How to create and work with a Response body
        • Query strings
          • How to create and work with Query strings
        • Headers
          • How to create and work with Headers
        • Path parameter
        • Create your first API: "Hello World!"
          • Working with API options
        • Exercise 1: GET REST API & Return Response Object
          • Step 1: Create API
          • Step 2: Test the API
        • Exercise 2: Delete a published API
        • Next Steps
      • Introduction to Entities
        • Fields
        • Records
        • Relations
        • Options for an Entity
        • Exercise 1: Customer Accounts and Activities
          • Customer accounts
            • Step 1: Create the Customers Entity
            • Step 2: Create the API
            • Step 3: Update entity through API
          • Customer Activities
            • Step 1: Create the Activities entity
            • Step 2: Create a “1 to Many” relation
            • Step 3: Create the API
            • Step 4: Update entity through API
        • Exercise 2: Customer Records
          • Step 1: Create the API
          • Step 2: Test the API
        • Next Steps
      • Programming with Entities
        • Create
        • Read
        • Update
        • Delete
        • JXPResponse
        • Next Steps
      • Using Triggers
        • After Create Trigger
        • After Update Trigger
        • After Delete Trigger
        • Exercise: Entity & Entity triggers
        • Step 1: Define “After Create” Trigger
        • Step 2: Define “After Update” Trigger
      • Connectors
        • Create a connector
        • SFTP Connector
        • MySQL Connector
        • Next Steps
      • Introduction to ETL pipeline
        • Create an ETL pipeline
        • Data Formats in ETL pipeline
          • Reader
          • Writer
          • Field mapping
        • Shared variables for ETL pipeline
        • ETL functions execution sequences overview
        • Displaying Functions
        • How to perform Search in ETL pipeline
        • Exercise 1: Skip Customer records
          • Step 1: Create the ETL pipeline
          • Step 2: Skip records based on Customer_ID
          • Step 3: Store skipped records in a list
          • Step 4: Store the Skipped Customer record
        • Exercise 2: ETL pipeline, CSV source & FTP Connector
        • Exercise 3: ETL pipeline, Database Source & MySQL Connector
        • Exercise 4: ETL pipeline, CSV Source & FTP Connector using Sections
          • Step 1: Create the ETL pipeline
          • Step 2: Reformat the Join_Date Values and define added fields
      • Multiple Executions, Time Duration, Linked App and Execution Status
        • Skip execution while in progress
        • Time duration settings
        • Linked App
        • Options for Delay Types
        • Execution status
      • Validation pipeline
        • Sequence of Validation pipeline
        • Exercise: Customer Information
          • Step 1: Create a Validation pipeline
          • Step 2: Create the API gateway
          • Step 3: Test the Validation pipeline
      • Introduction to Process
        • Create a process
        • Shared variables for a process
        • Exercise: Customer Anniversary Email Activity
          • Step 1: Check if the join date matches the current date
          • Step 2: Test the Process
      • Introduction to Flows & Flow components
        • What is a Flow?
        • What is a Flow component?
          • What is a Flow component Event?
        • Exercise 1: Customer Anniversary Email
          • Step 1: Create the Flow component
          • Step 2: Create the Flow
          • Step 3: Trigger the Flow
          • Step 4: Check records in the target entity
        • Exercise 2: Store list of customers
          • Step 1: Create the Flow component
          • Step 2: Create the Flow
          • Step 3: Create the API
    • 🌍Tour of Langstack (coming soon)
  • 📘In depth learning
    • 📖User Manual (coming soon)
    • 🔗Connectors
      • MS-SQL connector
        • Data type conversions between Langstack and MSSQL
        • Handling data loss in data type conversions
        • Setting up MS-SQL connector
        • MS-SQL connector as Reader and Writer
        • Using MS-SQL connector in ETL pipeline
          • MS-SQL connector as a source
          • MS-SQL connector as a destination
      • Google Drive connector
        • Set up Google Drive Connector
        • Adding Google Drive path in ETL pipeline Streams
        • Setting up GCP Account, GCP Project, API Enablement, and Credentials (Prerequisites)
          • OAuth 2.0 Client Credentials
          • Service Account Key Credentials
          • API Key Credentials
          • Authorization through Redirect URLs
        • Using Google Drive connector in ETL pipeline
          • Specifying file or folder paths
            • Direct file link
            • Folder link and file name
            • Folder path and file name
            • Folder path excluding file name
            • Folder path including file name
          • Google Drive connector as a source
          • Google Drive connector as a destination
      • AWS connector
        • Introduction
        • Set up AWS S3 Connector
          • OAuth 2.0 Client Type
          • Access Key
          • Public
        • Setting up AWS Account
          • Retrieve Client ID and Client Secret
        • Set up Amazon Web Services (Cognito) Console
          • Retrieve Identity Pool Id
          • Manage Permissions for Roles
          • Adding Redirect URL in Amazon Developer Console
          • User Consent for OAuth 2.0 Client through Login with Amazon
          • Retrieve Access Key Credentials
        • Creating a bucket in AWS
        • Using S3 connector in ETL pipeline
          • Specifying paths
          • Examples
            • AWS S3 connector as a source
            • AWS S3 connector as a destination
      • OneDrive connector
        • Set up OneDrive Connector
        • Setting up Microsoft Azure Account
        • Add URL to Authorized Redirect URLs
        • User Consent for Establishing OneDrive Connection
        • Usage of OneDrive Connector in ETL pipeline
          • Examples
          • OneDrive connector as a source
          • OneDrive connector as a destination
      • Dropbox connector
        • Set up Dropbox Connector
        • Setting up Dropbox Account
        • Add URL to Authorized Redirect URLs
        • User Consent for Establishing Dropbox Connection
        • Usage of Dropbox connector in ETL pipeline
          • Dropbox connector path settings
          • Examples
            • Dropbox connector as a source
            • Dropbox connector as destination
  • 🗃️Use Cases (Coming soon)
    • Use Case 1
    • Use Case 2
    • Use Case 3
Powered by GitBook
On this page
  1. Get started
  2. Learn Langstack
  3. Introduction to Entities
  4. Exercise 1: Customer Accounts and Activities
  5. Customer Activities

Step 3: Create the API

PreviousStep 2: Create a “1 to Many” relationNextStep 4: Update entity through API

Last updated 2 years ago

Once the Entity is created, create an API to generate customer activities records and store them in the Activities Entity created in Step 1.

  • The API will receive the customer ID as a single value and a list of customer activities (based on the 1 to Many relation configured in Step 2). The items in the list will be defined by the attributes of the fields created in the Entity “UserName_Acc_Activities”.

  • When all the items in the list are read, the API will create a new record for the Entity “UserName_Acc_Activities”. The API will then update the activity information against the customer ID received.

  • 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_AddActivities”. The name indicates that a customer activity is 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.

  • Select the Group "UserName".

  • Add constant text for API Paths as “acc” and “addactivities”. 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. By default, the name for the request body is “defaultRequestJsonBody”. For this exercise, label it as “ActivityRequestJsonBody”.

    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. Op.”onally enter a description for the “Request Body”. (This is used in the API documentation.)

    5. To define the Json object, enter the following sample json object: { "customer_id" : "string: representing the customer GUID", "activities" : [ { "Activity_Type" : "string: one of the following: Incoming Support call,Email,Product details request,Product Offer email,Outgoing call", "Activity_Description" : "string: the description text of the activity", "Activity_DateTime" : "string representing the date time in the form of YYYY-MM-DD hh:mm:ss", "Activity_Priority" : 0, "Activity_AssignedTo": "string representing the person this activity is assigned to and if missing sets it to default", "Activity_CreatedBy" : "string representing the person created this activity and if missing sets it to default" } ] } This json object indicates the type of request that will be received including the Customer ID and the list of activities for the customer. The key “activities” has the value enclosed in “[ ]”. This means that this key will have a list of values instead of just one value.

  • 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”.

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

  • Double click the description and label the “Action box” as “add customer activities”.

  • Expand the “Action box”. In the “Code” tab, 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” Action.)

  • To define the variables, go to the “Variables” tab of the “Action box”. To create a new variable, click the [+ Variable] button and create 3 new variables.

  • Define the variables as follows:

    1. Variable Name: “customeractivity”, Data Type: “UserName_Acc_Activities”. This variable will contain customer activity details for the customer activities entity.

    2. Variable Name: “customer”, Data Type: “UserName_Acc_customers”. This variable will contain customer information from the customer account entity.

    3. Variable Name: “resultofupdate”, Data Type: “JXPResponse”. This variable contains the response to be shown to the user i.e. success or failure of creation of record.

  • To define the actions for the API, click on the “Code” tab. Add Action by clicking the (+) sign in the “Action box”.

  • To receive the details (from the json object) for the customer field “Customer_ID” in the Entity “UserName_Acc_customers” (via “Request Body”), define the following Action to first convert it to GUID data type from string and update the variable “customer” as follows:

    1. Select Action as “Update Variable”.

    2. Select the Target variable as “customer.Customer_ID”.

    3. Select Value as Variables>Request>ActivityRequestJsonBody>customer_id>toGUID(). It displays as “Request.ActivityRequestJsonBody.customer_id.toGUID()”.

  • Add four (4) more “Actions” by clicking the (+) button.

  • To traverse a list of items received in the Activities list in the Request, update each item to the corresponding field in the record for the Activities entity and update the information for the customer via the “Customer_Activities” relation, define the second Action as follows:

    1. Select Action as “Traverse List”. A list of the activity records will be traversed.

    2. Select “Traverse the List” as Variables>Request>ActivityRequestJsonBody>Activities. It is displayed as “Request.ActivityRequestJsonBody.Activities”.

    3. The “item” is the element in the list to be traversed. It is automatically populated.

  • To store the values in the “Actions”, in the “Variables” tab of the “Traverse List” Action:

    1. Variable Name: “item”, Data Type: “Json”. This variable is automatically created on selecting “Traverse List” Action.

    2. Add a variable with variable Name: “activity”, Data Type: “UserName_Acc_Activities”. This variable will be updated with the value from the item traversed.

  • To define “Actions” in the “Traverse List”, go to the "Code" tab and add two (2) Actions by clicking the (+) button.

  • To take the value of each item received in the Json request (“item”) and update it to a corresponding field for activity record (for “UserName_Acc_Activities entity”), define the first Action in the Traverse List as follows:

    1. Select Action as “Update Entity fields from json”.

    2. Select the Target variable as “activity”.

    3. Select Source variable as “item” (Variables>item).

  • The second Action in the Traverse List updates the value of the activity variable and updates the variable customer information through the 1 to many relation.

    1. Select Action as “Add item to List”.

    2. Select the Target variable as customer>Customer_Activities. It is displayed as “customer.Customer_Activities”.

    3. Select Source variable as Variables>activity. It is displayed as “activity”.

  • To add the detail as a record to the target entity, define the third Action after the “Traverse List” in the “Action box” to update the record in the target entity:

    1. Select Action as “Update Entity record on primary key”. This Action updates the record of the Target Entity or related entities based on the primary key.

    2. Select “resultofupdate”. (This variable “resultofupdate” displays success or error messages.)

    3. Select Source as “customer”.

  • To convert the “resultofupdate” variable to json and store it in the response body, define the fourth Action in the “Action box” as follows:

    1. Select Action as “Update Variable”.

    2. Select the Target variable as Response>defaultResponseJsonBody. It displays as “Response.defaultResponseJsonBody”.

    3. Select Value as Variables>resultofupdate>toJson(). It displays as “resultofupdate.toJson()”.

  • The fifth and last Action in the “Action box” sends the response to the user.

    1. Select Action as “API Respond”.

    2. Select Response Body as “defaultResponseJsonBody”.

  • After all the Actions are defined, the “Code” tab should look as per the image below.

  • 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_AddActivities”.

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

  • In the “Details” tab and on the right-hand side, ensure the API is enabled.

💡
📕