# 3. Reading / Writing Data

### HAT Data

**Response type:**

```typescript
interface HatRecord<T> {
  endpoint: string; // eg. locations
  recordId: string; // A unique record ID of the record.
  data: T; // The Data
}
```

#### Write Data

| Parameters | Type   |
| ---------- | ------ |
| namespace  | string |
| endpoint   | string |
| body       | object |

Response

* HatRecord\\

**Example:**

```typescript
const namespace = "testhatapp";
const endpoint = "locations";
const newLocation = {
    latitude: "51.01",
    longitude: "52.12"
};

try {
    const result = await hat.hatData().create(
        namespace, 
        endpoint, 
        newLocation
    );

    if (result.parsedBody) {
        // result.parsedBody contains a HatRecord with the new data.
    }
} catch (error) {
  // Failed to write data...
}
```

#### Update Data

| Parameters | Type   |
| ---------- | ------ |
| body       | Array> |

Response:

* Array>

**Example:**

```typescript
const location = {
    endpoint: "testhatapp/locations",
    recordId: "c14b8f73-157f-40f1-9c77-985d9dea50d2",
    data: {
        latitude: "51.01",
        longitude: "52.12"
    }
};

location.data.latitude = "49.01";

const records = [location]; // Array of records to update. In this case we have only one.

try {
    const result = await hat.hatData().update(records);

    if (result.parsedBody) {
        // result.parsedBody contains an array of HatRecords<T> with the updated location data.
    }
} catch (error) {
  // Failed to update the records...
}
```

#### Delete Data

| Parameters | Type  |
| ---------- | ----- |
| recordIds  | Array |

Response:

* A successful response message

**Example:**

```typescript
const recordIds = ["c14b8f73-157f-40f1-9c77-985d9dea50d2", "d52b8f73-151f-40f1-9c77-932d9dea5daa"];

try {
    const result = await hat.hatData().delete(recordIds);

    if (result.parsedBody) {
        // result.parsedBody contains a successful response message.
        // In this case the result will be {message: "All records deleted"}.
    }
} catch (error) {
  // Failed to delete records...
}
```

#### Read Data

The are 2 ways to fetch data:

1. With default options.

| Parameters | Type   |
| ---------- | ------ |
| namespace  | string |
| endpoint   | string |

Response

* Array>

**Example:**

```typescript
const namespace = "testhatapp";
const endpoint = "locations";

try{
    const result = await hat.hatData().getAllDefault(namespace, endpoint);

    if (result.parsedBody) {
        // result.parsedBody contains an Array of HatRecord<Location>.
    }
} catch (error) {
    // Failed to fetch the data
}
```

2\. With custom options.

**Example:**

```typescript
const options = {
    ordering: "descending",
    orderBy: "lastUpdated"
};
const namespace = "testhatapp";
const endpoint = "locations";

try{
   const result = await hat.hatData().getAll(namespace, endpoint, options);

   if (result.parsedBody) {
       // result.parsedBody contains an Array of HatRecords<Location>, ordered by the last updated date.
   }
} catch (error) {
    // Failed to fetch the data
}
```


---

# 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.dataswyft.com/build/advanced-topics/pda-authentication/password-management/tutorials/data-api/3.-reading-writing-data.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.
