> ## Documentation Index
> Fetch the complete documentation index at: https://docs.moorcheh.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Fetch Text Data

> List text and summary chunks from a text-type namespace with cursor pagination. Use for export, display, or bulk RAG context loading.

## Overview

Get text chunks from a **text-type** namespace via the **public API**. Use this to list stored text/summary chunks for display, export, or RAG.

Results are returned **one page at a time**. Each page returns up to **100** items. When more data exists, the response includes `pagination.has_more` and a `pagination.next_token` — pass that token on the next request to fetch the following page.

<Info>
  Only namespaces with `type === "text"` are supported. Vector-only namespaces are not supported.
</Info>

## Authentication

<ParamField header="x-api-key" type="string" required>
  Your API key. API Gateway requires a valid API key for this route.
</ParamField>

## Path Parameters

<ParamField path="namespace_name" type="string" required>
  Name of the text namespace (e.g. `my-docs`)
</ParamField>

## Query Parameters

<ParamField query="limit" type="integer">
  Maximum number of items to return in this page. Default: **100**. Maximum: **100**.
</ParamField>

<ParamField query="next_token" type="string">
  Opaque cursor from the previous response's `pagination.next_token`. Omit on the first request.
</ParamField>

<Note>
  `next_token` is tied to your API key user and namespace. Do not reuse a token from a different namespace or account.
</Note>

## Pagination

<Steps>
  <Step title="First request">
    `GET .../fetch-text-data?limit=100` (or omit `limit` for the default of 100).
  </Step>

  <Step title="Check pagination">
    If `pagination.has_more` is `true`, more chunks exist in the namespace.
  </Step>

  <Step title="Next page">
    Call again with `next_token` from the previous response:
    `GET .../fetch-text-data?limit=100&next_token=<token>`
  </Step>

  <Step title="Stop">
    Repeat until `pagination.has_more` is `false` or `next_token` is `null`.
  </Step>
</Steps>

Each page is a separate API call and counts toward your API request usage and credits.

<RequestExample>
  ```bash Fetch first page theme={null}
  curl -X GET "https://api.moorcheh.ai/v1/namespaces/my-docs/documents/fetch-text-data?limit=100" \
    -H "x-api-key: YOUR_API_KEY"
  ```

  ```bash Fetch next page theme={null}
  curl -X GET "https://api.moorcheh.ai/v1/namespaces/my-docs/documents/fetch-text-data?limit=100&next_token=PASTE_TOKEN_FROM_PREVIOUS_RESPONSE" \
    -H "x-api-key: YOUR_API_KEY"
  ```
</RequestExample>

<ResponseExample>
  ```json 200 - Success (page with more data) theme={null}
  {
    "status": "success",
    "message": "Fetched 100 text items from namespace 'my-docs'.",
    "namespace": "my-docs",
    "statistics": {
      "total_items": 100,
      "total_text_chunks": 95,
      "total_summary_chunks": 5,
      "created_at_min": "2025-12-19T18:18:57.370Z",
      "created_at_max": "2025-12-19T18:20:01.120Z",
      "source_counts": {
        "my-docs": 100
      }
    },
    "items": [
      {
        "id": "api-reference_ai_generate_chunk_0",
        "text": "Generate AI-powered answers to questions...",
        "metadata": {
          "chunk_index": 0,
          "heading": "Overview",
          "source": "my-docs",
          "title": "Generate AI Answer - Overview"
        },
        "created_at": "2025-12-19T18:18:57.700Z",
        "is_summary": false
      }
    ],
    "pagination": {
      "limit": 100,
      "has_more": true,
      "next_token": "eyJ1c2VySWQiOiI..."
    },
    "execution_time": 1.381
  }
  ```

  ```json 200 - Success (last page) theme={null}
  {
    "status": "success",
    "message": "Fetched 20 text items from namespace 'my-docs'.",
    "namespace": "my-docs",
    "statistics": {
      "total_items": 20,
      "total_text_chunks": 18,
      "total_summary_chunks": 2,
      "created_at_min": "2025-12-19T18:21:00.000Z",
      "created_at_max": "2025-12-19T18:22:30.000Z",
      "source_counts": {
        "my-docs": 20
      }
    },
    "items": [],
    "pagination": {
      "limit": 100,
      "has_more": false,
      "next_token": null
    },
    "execution_time": 0.412
  }
  ```

  ```json 400 - Bad Request (Missing namespace) theme={null}
  {
    "status": "failure",
    "message": "Bad Request: Missing namespace name in URL path."
  }
  ```

  ```json 400 - Bad Request (Invalid limit) theme={null}
  {
    "status": "failure",
    "message": "Invalid limit: must be a positive integer."
  }
  ```

  ```json 400 - Bad Request (Invalid next_token) theme={null}
  {
    "status": "failure",
    "message": "Invalid next_token: token does not match requested namespace."
  }
  ```

  ```json 400 - Bad Request (Not text namespace) theme={null}
  {
    "status": "failure",
    "message": "Bad Request: Namespace 'your-namespace' is not a text-based namespace."
  }
  ```

  ```json 401 - Unauthorized (Missing API key) theme={null}
  {
    "status": "failure",
    "message": "Unauthorized: Missing API key"
  }
  ```

  ```json 401 - Unauthorized (Invalid or disabled API key) theme={null}
  {
    "status": "failure",
    "message": "Unauthorized: Invalid API key ID"
  }
  ```

  ```json 404 - Not Found theme={null}
  {
    "status": "failure",
    "message": "Namespace 'your-namespace' not found."
  }
  ```

  ```json 500 - Internal Server Error theme={null}
  {
    "status": "failure",
    "message": "Internal Server Error during namespace validation."
  }
  ```
</ResponseExample>

## Response Fields

### Success Response (200)

<ResponseField name="status" type="string">
  Always `"success"` for 200 responses
</ResponseField>

<ResponseField name="message" type="string">
  Human-readable summary for **this page** (e.g. "Fetched N text items from namespace '...'")
</ResponseField>

<ResponseField name="namespace" type="string">
  The requested namespace name
</ResponseField>

<ResponseField name="statistics" type="object">
  Aggregated counts and timestamps for **items in this page only** (not the full namespace total)
</ResponseField>

<ResponseField name="statistics.total_items" type="number">
  Number of items in `items` on this page
</ResponseField>

<ResponseField name="statistics.total_text_chunks" type="number">
  Count of non-summary chunks on this page
</ResponseField>

<ResponseField name="statistics.total_summary_chunks" type="number">
  Count of summary chunks on this page
</ResponseField>

<ResponseField name="statistics.created_at_min" type="string | null">
  Earliest item timestamp on this page as **ISO 8601** (e.g. `"2025-12-19T18:18:57.370Z"`). Computed by normalizing each item's stored `createdAt` (ISO string or Unix s/ms) and converting the minimum back to ISO. `null` when no item has a parseable date.
</ResponseField>

<ResponseField name="statistics.created_at_max" type="string | null">
  Latest item timestamp on this page as **ISO 8601**. Same normalization as `created_at_min`. `null` when no item has a parseable date.
</ResponseField>

<ResponseField name="statistics.source_counts" type="object">
  Map of source name → count on this page. Keys may be file names, upload sources, or namespace identifiers.
</ResponseField>

<ResponseField name="items" type="array">
  Text chunks for this page (length ≤ `limit`, max 100)
</ResponseField>

<ResponseField name="items[].id" type="string">
  Item/chunk identifier
</ResponseField>

<ResponseField name="items[].text" type="string">
  Text content of the chunk
</ResponseField>

<ResponseField name="items[].metadata" type="object">
  Arbitrary metadata (e.g. `source`, `heading`, `chunk_index`, `page_title`)
</ResponseField>

<ResponseField name="items[].created_at" type="string | null">
  Creation time as an ISO 8601 string (e.g. `"2025-12-19T18:18:57.700Z"`)
</ResponseField>

<ResponseField name="items[].is_summary" type="boolean">
  `true` for summary chunks, `false` for regular text chunks
</ResponseField>

<ResponseField name="pagination" type="object">
  Cursor pagination metadata for this response
</ResponseField>

<ResponseField name="pagination.limit" type="number">
  Page size used for this request (≤ 100)
</ResponseField>

<ResponseField name="pagination.has_more" type="boolean">
  `true` if additional pages exist after this one
</ResponseField>

<ResponseField name="pagination.next_token" type="string | null">
  Pass as query param `next_token` to fetch the next page. `null` when `has_more` is `false`.
</ResponseField>

<ResponseField name="execution_time" type="number">
  Request processing time in seconds
</ResponseField>

## Limits

<CardGroup cols={2}>
  <Card title="Items per page" icon="list">
    Up to **100** items per request. Use `limit` and `next_token` to walk the full namespace.
  </Card>

  <Card title="Credits & API requests" icon="zap">
    **Each page** is one API call and consumes credits toward your plan's API request limit.
  </Card>
</CardGroup>

## Export all pages (example)

```javascript theme={null}
async function fetchAllTextChunks(apiKey, namespace) {
  const base = "https://api.moorcheh.ai/v1";
  const allItems = [];
  let nextToken;

  do {
    const params = new URLSearchParams({ limit: "100" });
    if (nextToken) params.set("next_token", nextToken);

    const res = await fetch(
      `${base}/namespaces/${encodeURIComponent(namespace)}/documents/fetch-text-data?${params}`,
      { headers: { "x-api-key": apiKey } }
    );
    const body = await res.json();
    if (!res.ok) throw new Error(body.message || res.statusText);

    allItems.push(...(body.items || []));
    nextToken = body.pagination?.has_more ? body.pagination.next_token : null;
  } while (nextToken);

  return allItems;
}
```

## Related Endpoints

* [Get Documents](/api-reference/data/get-documents) - Retrieve specific documents by ID
* [Upload Text Data](/api-reference/data/upload-text) - Add text documents to a namespace
* [Search](/api-reference/search/query) - Semantic search over namespace content
