# Step 3: Create the API

Create an API to trigger the Flow to create records from the list of customers 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.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_4/ch4apimenu.png" alt=""><figcaption></figcaption></figure>

* To create a new API, click the \[+ API] button.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_4/ch4addapi.png" alt=""><figcaption></figcaption></figure>

* The New API page displays with the Details tab preselected. To define the API, add the following information:&#x20;
  1. Enter the API name as “UserName\_async\_addlistofcustomers”. The name indicates that a customer has been added.&#x20;
  2. Select the API method as “POST”. This defines that the API will post or send data.&#x20;
  3. Optionally add a description for the API.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13apiaddlistofcust.png" alt=""><figcaption></figcaption></figure>

* Create a new Group Name by clicking \[+].&#x20;
  1. Enter the Group name as “UserName”.&#x20;
  2. Enter the Group base URL as “UserName”.&#x20;
  3. To save the Group name details, click \[Save].

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13apigrpname.png" alt=""><figcaption></figcaption></figure>

* Add API Paths as “async” and “addlistofcustomers”. \
  Each parameter displays in a blue bubble.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2pathparameter.png" alt=""><figcaption></figcaption></figure>

* The URL for the API is generated.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2apiurl.png" alt=""><figcaption></figcaption></figure>

* To define the Request body and Response body details, go to the “Inputs & outputs” tab.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_4/ch4nocalciotab.png" alt=""><figcaption></figcaption></figure>

*   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:&#x20;

    1. By default, the name for the request body is “defaultRequestJsonBody”. For this exercise, label it as “requestJsonCreateCustomer”.&#x20;
    2. Content type from the list of values is selected as “application/json”. This defines the type of content in the request.&#x20;
    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 define the json sample for the request, add the following Json: \


    \[{ "Address\_Line\_1": "first Street", "City": "", "Customer\_Value": 5, "Country": "USA", "Email": "JaneDoe@mail.com", "First\_Name": "Jane", "IsActive": true, "Join\_Date": "2021-09-15", "Last\_Name": "Doe", "Phone": "1237865", "Rating": 1000, "Zip\_Code": "45"

    }]

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2apireqbody.png" alt=""><figcaption></figcaption></figure>

* 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:&#x20;
  1. By default, the name for the response body is “defaultResponseJsonBody”. For this exercise, keep the response body name as is.&#x20;
  2. The Content type from the list of values is selected as “application/json”. This defines the type of content in the response.&#x20;
  3. The Response Code is selected as “200 OK”.&#x20;
  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: \
      \
      `{` \
      &#x20; `"status": "success or failed",`\
      &#x20; `"errorcode": "0",`

      &#x20; `"message": "The response of creating the entity record.",`

      &#x20; `"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 “\[]”

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2apirespbody.png" alt=""><figcaption></figcaption></figure>

* 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].

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13teaddtargent.png" alt=""><figcaption></figcaption></figure>

* To define the Target Entity for storing the created record:&#x20;
  1. Select Target Entity as “UserName\_Acc\_customers”.&#x20;
  2. Keep “select a default validation” 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.&#x20;
  3. “Show in API gateways” 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.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13tecustomers.png" alt=""><figcaption></figcaption></figure>

* To see the default variables created for an API, go to the “Variables” tab. The following two (2) Read Only variables are predefined:&#x20;
  1. Variable Name: “Request”, Data Type: “WebRequest”. This variable contains the Web Request received.&#x20;
  2. Variable Name: “Response”, Data Type: “WebResponse”. This variable contains the Web Response sent.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13apisharedvars.png" alt=""><figcaption></figcaption></figure>

* 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 “Action box” label, double click the description and label the Action box as “create records for list of customers”. \
  In this “Action box”, the records will be updated in the entity “UserName\_Acc\_customers” based on the request received.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13codeabcreaterecords.png" alt=""><figcaption></figcaption></figure>

* Click on the arrow on the top right corner of the “Action box” to expand it. \
  In the “Code” tab, an Action called “API Respond” is already added. Notice the variable as “defaultResponseJsonBody”.\
  Remove this action for now.
* First, the variables need to be defined and initialized. To define the variables, go to the “Variables” tab of the “Action box”. \
  To create a new variable, click the \[+ Variable] button.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ab1addvar.png" alt=""><figcaption></figcaption></figure>

* To contain the values for the fields of the desired entity in a record, define the variable as follows:&#x20;
  1. Enter Variable Name as “triggerCustomerRecord”.&#x20;
  2. Select Data Type as the Entity “UserName\_Acc\_customers”.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13trigcustrecvar.png" alt=""><figcaption></figcaption></figure>

* To contain the values for the Json fields received in the request, define the variable as follows:&#x20;
  1. Enter Variable Name as “detailsJS”.&#x20;
  2. Select Data Type as the Entity “Json”.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13detailsjsvar.png" alt=""><figcaption></figcaption></figure>

* To define the Json sample (layout in which the list of customers will be received in the request), click the \[Sample] button.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13detailsjssample.png" alt=""><figcaption></figcaption></figure>

*   Add the following Json sample in the Sample Schema View: \
    \
    `{ "customerslist": [{` \
    &#x20; `"Address": "first Street",`

    &#x20; `"City": "",`

    &#x20; `"Customer_Value": 5,`

    &#x20; `"Country": "USA",`

    &#x20; `"Email": "JaneDoe@mail.com",`

    &#x20; `"First_Name": "Jane",`

    &#x20; `"IsActive": true,`

    &#x20; `"Join_Date": "2021-09-15",`

    &#x20; `"Last_Name": "Doe",`

    &#x20; `"Phone": "1237865",`

    &#x20; `"Rating": 1000,`

    &#x20; `"Zip_Code": "45" }]`\
    &#x20;`}`\


    To save the Sample Schema, click the \[Save] button.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13detailsjssamplesch.png" alt=""><figcaption></figcaption></figure>

* To define the actions for this API, go to the “Code” tab of the Action box and add three (3) Actions.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13crtreclistcode.png" alt=""><figcaption></figcaption></figure>

* Define the first three actions to acknowledge the request and to send the response that the request is queued. To send the response code, redefine the first, existing first Action as follows:
  1. Select Action as “Update Variable”.&#x20;
  2. Select Target Value as Response>defaultResponseJsonBody>errorcode. It displays as “Response.defaultResponseJsonBody.errorcode”.&#x20;
  3. Enter Value as “200” and press Enter.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2uverrorcode.png" alt=""><figcaption></figcaption></figure>

* To send the response message, add the second Action and define it as follows:&#x20;
  1. Select Action as “Update Variable”.&#x20;
  2. Select Target Value as Response>defaultResponseJsonBody>message. It displays as “Response.defaultResponseJsonBody.message”.&#x20;
  3. Enter Value as “Your request is now queued for processing.” and press Enter.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2uverrormsg.png" alt=""><figcaption></figcaption></figure>

* To send the response status, add the third Action and define it as follows:&#x20;
  1. Select Action as “Update Variable”.&#x20;
  2. Select Target Value as Response>defaultResponseJsonBody>status. It displays as “Response.defaultResponseJsonBody.status”.&#x20;
  3. Enter Value as “Request Queued” and press Enter.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13uvvarstatus.png" alt=""><figcaption></figcaption></figure>

* Once the response has been sent, the created Flow will be triggered to create the customer record. To trigger a Flow, it is mandatory that an entity record is loaded.
* To create the record to load and ensure it does not get deleted, for this exercise, go to the “UserName\_Acc\_customers” entity and create a record as follows:&#x20;
  1. Address: DO NOT DELETE&#x20;
  2. City: DO NOT DELETE&#x20;
  3. Country: DO NOT DELETE&#x20;
  4. Customer\_ID: (autogenerated)&#x20;
  5. Customer\_Value: 1&#x20;
  6. DayMonthJoinDate: (current day and month)&#x20;
  7. First\_Name: DO NOT DELETE&#x20;
  8. IsActive: true&#x20;
  9. Join\_Date: (current day and month)&#x20;
  10. Last\_Name: DO NOT DELETE&#x20;
  11. Phone: DO NOT DELETE

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13donotdelrec.png" alt=""><figcaption></figcaption></figure>

* To load the entity record for triggering the Flow, go to the API, add the fourth Action, and define it as follows:&#x20;
  1. Select Action as “Load Entity record”.&#x20;
  2. Select Target Value as “triggerCustomerRecord”.&#x20;
  3. Click on the Setup Criteria button to define the settings for this Action.

![](https://2351945889-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVoxOh7gdjWdmqooFdYBY%2Fuploads%2FUYs439fKysQcrcE8g4JB%2Fcreaterecordslist_codeTab_LoadEntityRecord.png?alt=media\&token=7e543ea8-ede7-41a1-9063-8ac56936ae4a)

* To define the Setup criteria for the entity record:&#x20;
  1. Select Order by as “First\_Name”.&#x20;
  2. By default, the “Descending” option is selected. Keep it as is.&#x20;
  3. Add a match key by clicking the \[+ Match key] button.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2loadentreccrit1.png" alt=""><figcaption></figcaption></figure>

* To match the First\_Name field of the entity to “DO NOT DELETE” text, define the match key as follows:&#x20;
  1. Select Field as “First\_Name”.&#x20;
  2. Enter Source as “DO NOT DELETE”.&#x20;
  3. To save the settings, click the \[Save] button.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2loadentreccrit2.png" alt=""><figcaption></figcaption></figure>

* To update the request into the Json field to be loaded with the entity while triggering Flow, add and define the fifth Action as follows:&#x20;
  1. Select Action as “Update Variable”.&#x20;
  2. Select Target Value as detailsJS>customerslist. It displays as “detailsJS.customerslist”.&#x20;
  3. Enter Value as Variables>Request>defaultRequestJsonBody. It displays as “Request.defaultRequestJsonBody”.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2uvdetjson.png" alt=""><figcaption></figcaption></figure>

* To trigger the Flow, add and define the sixth Action with the entity variable and Json sample as follows:&#x20;
  1. Select Action as “Trigger Flow”.&#x20;
  2. Select Flow as “UserName\_createlistofcustomers”. Select Entity variable as Variables>triggerCustomerRecord. It displays as “triggerCustomerRecord”.&#x20;
  3. Select Json as Variables>detailsJS. It displays as “detailsJS”.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2trigflow.png" alt=""><figcaption></figcaption></figure>

* To send the API response, add the last Action and define it as follows:&#x20;
  1. Select Action as “API Respond”.&#x20;
  2. Select Response body as “defaultResponseJsonBody”.

![](https://2351945889-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVoxOh7gdjWdmqooFdYBY%2Fuploads%2FLCe9RjH142wGlbd9p6Vn%2FAPIRespondLabelled.png?alt=media\&token=39914f53-f0d0-4192-9a45-5b366d046933)

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

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_4/ch4hwsaveapi.png" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_4/ch4publishapi.png" alt=""><figcaption></figcaption></figure>

* To define the API gateway, go to the Details tab and add the API gateway for this API as “UserName\_async\_addlistofcustomers”. Save the API again.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2apigatwy.png" alt=""><figcaption></figcaption></figure>

* Ensure the API is enabled in the “Details” tab on the right-hand side.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_4/ch4enableapi.png" alt=""><figcaption></figcaption></figure>

* To test the API, click the \[Test] button.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_4/ch4nocalctestapi.png" alt=""><figcaption></figcaption></figure>

* In the Test Console:&#x20;
  1. Add the values of the list of customers.&#x20;
  2. Enable the \[Entity actions] toggle button.
  3. Click \[Test API].

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13ex2testapi.png" alt=""><figcaption></figcaption></figure>



* To see the Response, go to the Body section in the Test Console. The response message displays stating that the request is queued for processing.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_13/ch13testapi2.png" alt=""><figcaption></figcaption></figure>

![](https://2351945889-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVoxOh7gdjWdmqooFdYBY%2Fuploads%2Fql9VaOg4uLbCD1cBskFo%2FTestAPI_AddListofCustomers3.png?alt=media\&token=8df46c58-9cb4-457f-9543-6460d28b6259)
