# Create a Custom App to Call App Runtime REST API

This section demonstrates how a **Custom Application** can interact with ONEWEB through the **App Runtime REST API** to retrieve entity data.\
The example uses a **Work Order entity** and shows a complete flow from authentication to displaying results in a custom UI.

***

### Objective

By the end of this section, you will understand how to:

* Authenticate using App Runtime REST API
* Retrieve entity data using REST endpoints
* Pass key parameters through the request URL
* Use REST responses in a Custom Application
* Display returned JSON data in the UI

***

### Use Case Overview

**Use case:**\
A Custom Application needs to retrieve **Work Order** information from ONEWEB and display related customer contact data using REST APIs.

***

### Step‑by‑Step: Get Entity Data Using REST API

***

#### Step 1: Configure Entity for REST Access

Create an Entity in **App Designer** named **Work Order**.

In this example:

* The entity contains **3 Tabs**
* Includes **4 Modules**
* Is configured to expose data through App Runtime APIs

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2Fd9TnJ94Bf9dyxhpQn1eg%2Fimage.png?alt=media&#x26;token=2451433e-a249-4395-b2b7-672359477ecc" alt=""><figcaption></figcaption></figure>

***

#### Step 2: Identify Key Field for Query

Determine the **key field** used to retrieve entity data via REST.

In this example:

* Key field: `SALE_ORDER_NO`
* Key value: `0000000010`

This value will be passed as a query parameter to the REST API.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2F1jsGqafoKBTJURf2yGxd%2Fimage.png?alt=media&#x26;token=90af6b97-6e5f-4dda-8261-ddc25dda2df2" alt=""><figcaption></figcaption></figure>

***

#### Step 3: Login to Obtain Authentication Token

Before calling entity APIs, authenticate using the **Login REST API**.

**Request**

* **Method:** POST
* **URL:**

  ```
  http://localhost:9084/eaf-rest/login
  ```
* **Headers:**

  ```
  Content-Type: application/json
  ```
* **Body (JSON):**

```
JSON{
    "username": "administrator",
    "password": "P@55w0rd",
    "clientId": "123456789"
}
```

**Response**

* The system returns an **id\_token**
* This token must be used for authorization in subsequent API calls

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2F5tjXfxLBhz5EQkI67MZA%2Fimage.png?alt=media&#x26;token=0f643dc6-0c89-40e2-942d-5a98eb2fa5ce" alt=""><figcaption></figcaption></figure>

***

#### Step 4: Call Entity GET API

Construct the GET request to retrieve data from the entity.

**URL Format**

```
ServerName:Port/ContextRoot/entity/{entityID}/get
```

**Example**

```
http://localhost:9084/eaf-rest/entity/EN_431959963131500/get
  ?handleForm=Y
  &SALE_ORDER_NO=0000000010
  &cbMethod=callback
```

***

#### Step 5: Set Required HTTP Headers

Include the following headers in the request:

```
Content-Type: application/json
Authorization: Bearer <id_token>
clientId: 123456789
```

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FOwJ4g1PqRNCNGN98MDVo%2Fimage.png?alt=media&#x26;token=cba880ef-dae3-4248-84b6-1499a7592172" alt=""><figcaption></figcaption></figure>

***

#### Step 6: Execute Requests and Receive JSON Response

After successful authentication and API call, the App Runtime responds with **JSON data** representing the entity.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FWeSusApw7fYzCTKO9X3n%2Fimage.png?alt=media&#x26;token=f98bc930-add7-4003-a858-defb07e23272" alt=""><figcaption></figcaption></figure>

***

#### Understanding Response Structure

The response JSON typically includes:

* `ENTITY_ID` – Entity identifier
* `MODULE_ID` – Module identifier
* `UPDATE` – Operation mode
* `SALE_ORDER_ITEM` – Table or module name
* Field names such as:
  * `PRODUCT_ID`
  * `PRODUCT_NAME`
* `KEY` – Primary key for records

This structured response allows Custom Apps to navigate and extract relevant information programmatically.

***

### Test API from a Custom Application

Now let’s simulate a **real Custom App flow** that uses JavaScript and AJAX to consume the App Runtime API.

***

#### Step 1: Login Screen

The user enters credentials in the Custom App UI and triggers a login request.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FUHZsFHXF8gJ9NPy6cAXN%2Fimage.png?alt=media&#x26;token=f07ed21f-a5a5-4a25-8016-f74fe2894552" alt=""><figcaption></figcaption></figure>

***

#### Step 2: Store Token in Session

Use **AJAX** to:

* Call Login REST API
* Store `id_token` securely in session or memory for reuse

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2F8gfRy2NjD1cI9zJ7eDuA%2Fimage.png?alt=media&#x26;token=bfa237d9-49f2-433a-aef8-bd80eff35711" alt=""><figcaption></figcaption></figure>

***

#### Step 3: Input Order Number in UI

Prepare a screen where users can input a **Sales Order Number**.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FkU5TKf5FoV1jnM4CzE8W%2Fimage.png?alt=media&#x26;token=9dd9eaaa-a605-4bbb-baec-fa26e98c8892" alt=""><figcaption></figcaption></figure>

***

#### Step 4: Trigger Entity API Call

User enters an Order No and clicks **Find Customer Contact**.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FBfi1OlSiIFvEgoI8tzCg%2Fimage.png?alt=media&#x26;token=787007b7-c63d-47ee-8d2f-d2f0d288b13a" alt=""><figcaption></figcaption></figure>

***

#### Step 5: Call REST API via JavaScript

Use JavaScript / AJAX to:

* Call the entity GET API
* Pass `SALE_ORDER_NO`
* Include headers:
  * Content-Type
  * Authorization (Bearer token)
  * clientId

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FTkAt4b3aefDi2QwgL2Z6%2Fimage.png?alt=media&#x26;token=23ec89c9-b6d7-41a1-93b0-d73a3ae0bf13" alt=""><figcaption></figcaption></figure>

***

#### Step 6: Extract Required Data from JSON

Navigate the JSON response to retrieve:

```
MODULE_ID → UPDATE → SALE_ORDER_DELIVERY → CUST_CONTACT
```

This value represents the **Customer Contact** associated with the work order.

***

#### Step 7: Display Result in UI

Use JavaScript or JQuery to render the retrieved data on the screen.

<figure><img src="https://2015371994-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMpDjHWFRUtZ5nJcSfVXd%2Fuploads%2FRgP1xJGrJAmFrUFhpHuW%2Fimage.png?alt=media&#x26;token=eed15cf8-634f-4453-bff1-a3922dd1851a" alt=""><figcaption></figcaption></figure>

***

### Summary

This example demonstrates how a **Custom Application** can:

* Authenticate via App Runtime REST API
* Retrieve entity data using key‑based queries
* Consume REST responses in JSON format
* Display backend data dynamically in a custom UI

By using **App Runtime REST APIs**, ONEWEB applications can act as powerful backend services for:

* Web apps
* Mobile apps
* Third‑party systems


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.onewebstack.com/building-apps/integrations/app-runtime-rest-api/create-a-custom-app-to-call-app-runtime-rest-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
