# Knowledge documents

Write your agent's knowledge base from your own backend: add, change and delete documents, upload files, crawl a website, read how indexing went, export what changed, and get an event when a document is ready.

Source: https://docs.omazy.ai/reference/core-api/knowledge-documents/

Your agent answers from its knowledge base. These routes let your backend keep
that knowledge base current without anyone opening the console: write a
document when a policy changes, upload the new handbook, and know the moment
the agent can use it.

All of them run against the app's agent: its published agent, or its newest
draft when nothing is published yet. An app with no agent answers `404`.

| You want | Call | Scope |
|---|---|---|
| The documents, newest first | `GET /knowledge/documents` | `kb:read` |
| One document and how its indexing stands | `GET /knowledge/documents/{id}` | `kb:read` |
| A new document | `POST /knowledge/documents` | `kb:write` |
| A change to a document | `PATCH /knowledge/documents/{id}` | `kb:write` |
| A document gone | `DELETE /knowledge/documents/{id}` | `kb:write` |
| A file turned into a document | `POST /knowledge/import` | `kb:write` |
| The websites being crawled | `GET /knowledge/sources` | `kb:read` |
| A website crawled into documents | `POST /knowledge/sources` | `kb:write` |
| A fresh crawl of a source | `POST /knowledge/sources/{id}/crawl` | `kb:write` |
| What changed since you last looked | `GET /knowledge/export` | `kb:read` |

`kb:read` and `kb:write` are separate from each other and from `kb:search`. A
token that may ask the knowledge base a question cannot list what is in it, and
one that may read it cannot change it. A token that writes and then reads back
needs both.

## Indexing

A document you write is not searchable at once. It is split into passages and
indexed in the background, usually within a few seconds. Every document carries
an `index` object that says where that stands.

| `index.status` | Means |
|---|---|
| `queued` | Written, waiting to be indexed |
| `indexing` | Being indexed now |
| `ready` | Indexed as it stands. The agent can use it |
| `failed` | The last attempt failed. `index.error` says why. It is tried again every few minutes without a call from you |

An edit puts a `ready` document back to `queued` until the new text is indexed.
The old text stays searchable until then.

Search catches up a moment after `ready`, usually within a second or two. A
test that writes a document and searches for it in the same breath should
allow for that.

You can find out that a document is ready in two ways. Read it again with
`GET /knowledge/documents/{id}`. Or let the platform tell you: see
[Events](#events).

## Add a document

```
POST /knowledge/documents
```

Scope: `kb:write`.

```sh
curl https://mw.omazy.ai/api/v1/core/knowledge/documents \
  -H "Authorization: Bearer $OMAZY_CORE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "title": "Refund policy", "content": "Refunds are returned to the original payment method within 7 working days." }'
```

| Field | Type | Notes |
|---|---|---|
| `title` | string | Required. At most `300` characters. Shown as the source of an answer. |
| `content` | string | The text. At most `2` MB. Required unless `data` is given. |
| `kind` | string | `text` (the default), `faq`, `structured`, `pricing` or `catalog`. |
| `data` | object | Structured fields, for the `structured`, `pricing` and `catalog` kinds. At most `256` KB. |
| `source_url` | string | Where the text came from, when it has an address. |
| `language` | string | Language code of the text. Default `en`. |
| `is_active` | boolean | `false` stores the document without making it searchable. Default `true`. |
| `app` | string | The app's handle. Needed only when the workspace has more than one app. |

It answers `201` with the document:

```json
{
  "success": true,
  "data": {
    "id": "01M7TEFJ0YYZVHCYVDE5JR3M9K",
    "kind": "text",
    "title": "Refund policy",
    "source_url": null,
    "language": "en",
    "is_active": true,
    "content": "Refunds are returned to the original payment method within 7 working days.",
    "index": { "status": "queued", "indexed_at": null, "chunks": 0, "tokens": 0 },
    "version": 0,
    "created_at": "2026-11-20T04:10:13Z",
    "updated_at": "2026-11-20T04:10:13Z"
  }
}
```

| Field | Notes |
|---|---|
| `id` | Keep it. Every other route takes it. |
| `index.status` | `queued`, `indexing`, `ready` or `failed`. |
| `index.error` | Present only while the status is `failed`. |
| `index.chunks`, `index.tokens` | How many passages the document became, and their size. `0` until it is indexed. |
| `version` | The [knowledge version](#keep-your-own-copy) of the document's last index. `0` until it is indexed. |

Sending a document that already exists in every field returns the existing
document. It does not make a second copy, so a request you had to send twice is
safe. A document that differs in any field is a new document.

The routes here that add something also take an
[`Idempotency-Key`](/reference/core-api/errors-and-limits/#sending-a-request-again):
adding a document, uploading a file, adding a website and crawling one again.
With a key, a repeat returns the first answer and starts nothing.

## Read documents

```
GET /knowledge/documents
GET /knowledge/documents/{id}
```

Scope: `kb:read`.

The list is newest first and leaves `content` and `data` out. Read one document
to get them.

| Query | Notes |
|---|---|
| `limit` | Documents per page. Default `50`, at most `200`. |
| `cursor` | The `next_cursor` of the page before. |
| `app` | The app's handle, when the workspace has more than one app. |

```json
{
  "success": true,
  "data": {
    "documents": [
      {
        "id": "01M7TEFJ0YYZVHCYVDE5JR3M9K",
        "kind": "text",
        "title": "Refund policy",
        "source_url": null,
        "language": "en",
        "is_active": true,
        "index": { "status": "ready", "indexed_at": "2026-11-20T04:10:15Z", "chunks": 1, "tokens": 19 },
        "version": 4127,
        "created_at": "2026-11-20T04:10:13Z",
        "updated_at": "2026-11-20T04:10:15Z"
      }
    ],
    "next_cursor": "",
    "version": 4127
  }
}
```

`next_cursor` is empty on the last page. The top-level `version` is the
knowledge base's version now.

## Change or delete a document

```
PATCH  /knowledge/documents/{id}
DELETE /knowledge/documents/{id}
```

Scope: `kb:write`.

`PATCH` takes any of the fields of a new document and changes only the ones you
send. It answers with the document, back at `queued`, and indexes it again.

```sh
curl -X PATCH https://mw.omazy.ai/api/v1/core/knowledge/documents/01M7TEFJ0YYZVHCYVDE5JR3M9K \
  -H "Authorization: Bearer $OMAZY_CORE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "content": "Refunds are returned within 5 working days." }'
```

To take a document out of search without losing it, send `{ "is_active": false }`.
Send `true` to bring it back.

`DELETE` removes the document for good and answers
`{ "id": "...", "deleted": true }`. A second delete of the same id answers `404`.

## Upload a file

```
POST /knowledge/import?filename=handbook.pdf
```

Scope: `kb:write`.

Send the file as the request body, with its type in `Content-Type` and its name
in the `filename` query parameter. PDF, spreadsheets, images and plain text are
read. The text is extracted and becomes one document titled with the file name.

```sh
curl "https://mw.omazy.ai/api/v1/core/knowledge/import?filename=handbook.pdf" \
  -H "Authorization: Bearer $OMAZY_CORE_TOKEN" \
  -H "Content-Type: application/pdf" \
  --data-binary @handbook.pdf
```

A multipart form works too: put the file in a part named `file`.

It answers `201` with the document, as a new document does. A file with no text
in it answers `400`. The file is at most `20` MB and the name at most `255`
characters.

## Crawl a website

```
GET  /knowledge/sources
POST /knowledge/sources
POST /knowledge/sources/{id}/crawl
```

Scopes: `kb:read` to list, `kb:write` for the other two.

A source is a website. Adding one starts a crawl, and each page it finds
becomes a document.

| Field | Type | Notes |
|---|---|---|
| `root_url` | string | Required. Where to start, with `https://`. |
| `max_pages` | integer | How many pages to fetch at most. Default `25`, at most `200`. |
| `max_depth` | integer | How many links deep to follow. Default `2`, at most `4`. |
| `app` | string | The app's handle, when the workspace has more than one app. |

`POST /knowledge/sources` answers `201` with the source: its `id`, its `status`
and its counts. `POST /knowledge/sources/{id}/crawl` queues a fresh crawl of
that source and answers `202`. Use it after the website changes.

## Keep your own copy

Some products keep a copy of the knowledge base close to where they answer, so
a lookup does not cross the network. `GET /knowledge/export` is for that.

```
GET /knowledge/export
GET /knowledge/export?since=4127
```

Scope: `kb:read`.

Every time a document is indexed or deleted, the knowledge base's version goes
up. An export gives you the documents that changed after a version, each with
the passages search uses, and a new version to ask from next time.

| Query | Notes |
|---|---|
| `since` | The `version` an earlier export returned. Leave it out to get everything. |
| `cursor` | The `next_cursor` of the page before. |
| `limit` | Documents per page. Default `50`, at most `200`. |
| `app` | The app's handle, when the workspace has more than one app. |

```json
{
  "success": true,
  "data": {
    "version": 4131,
    "documents": [
      {
        "id": "01M7TEFJ0YYZVHCYVDE5JR3M9K",
        "kind": "text",
        "title": "Refund policy",
        "source_url": null,
        "language": "en",
        "is_active": true,
        "version": 4130,
        "updated_at": "2026-11-20T04:22:40Z",
        "chunks": [
          { "ordinal": 0, "text": "Refunds are returned within 5 working days.", "tokens": 9, "checksum": "9f2c" }
        ]
      }
    ],
    "deleted": [{ "id": "01M7TEG4E5E4KV4KGKQSHKFPZ5", "version": 4131 }],
    "resync": false,
    "next_cursor": ""
  }
}
```

How to use it:

1. The first time, call it without `since`. Follow `next_cursor` until it is
   empty, storing every document. Keep the `version`. It is the same on every
   page.
2. After that, call it with `since` set to the version you kept. Replace each
   document in `documents`, remove each id in `deleted`, and keep the new
   `version`.
3. A document with `is_active` false was switched off. It has no passages.
   Remove it from your copy.
4. If `resync` is true, more was deleted than one export lists. Drop your copy
   and start again at step 1.

Three things worth knowing. A change shows in an export about two seconds after
it is indexed, not at once. A document can come back in an export unchanged,
because it was indexed again, so write your copy so that storing the same
document twice is harmless. And a document that has never been indexed is not
in an export at all.

## Events

Polling works. An event is quicker. A token can have an events address: an
`https` URL of yours that the platform posts to when a document of the
workspace finishes indexing, fails or is deleted.

A workspace owner sets it on the token, from the console's API:

```
PATCH https://mw.omazy.ai/api/v1/workspace/{handle}/installs/{id}
```

```json
{ "events_url": "https://api.example.com/omazy/events" }
```

An empty string turns events off. The address must be public and `https`.
Only a token that holds `kb:read` or `kb:write` is sent knowledge events.

The event is `kb.updated`:

```json
{
  "id": "evt_01M7TEGS4RQKQ1P15FFZTPY4JG",
  "event": "kb.updated",
  "workspace_id": "7d6b032b-83b1-4e92-ab19-c2bd902faf2c",
  "data": {
    "document_id": "01M7TEFJ0YYZVHCYVDE5JR3M9K",
    "app": "support",
    "op": "indexed",
    "status": "ready",
    "version": 4130,
    "is_active": true
  },
  "sent_at": "2026-11-20T04:22:41Z"
}
```

| `data.op` | `data.status` | Means |
|---|---|---|
| `indexed` | `ready` | The document is indexed as it stands. `is_active` false means it was switched off and is out of search |
| `failed` | `failed` | An attempt failed. `data.error` says why. Sent once per reason, not once per retry |
| `deleted` | `deleted` | The document no longer exists |

The body is small on purpose. It tells you which document changed. Read the
document, or export from your last version, to get what it now says.

The request carries three headers.

| Header | Value |
|---|---|
| `X-Omazy-Signature` | `sha256=` and the hex HMAC-SHA256 of the raw body, keyed with your app's callback signing key. Verify it as you would [a call outcome](/reference/core-api/speech-and-calls/#getting-the-outcome-pushed-to-you) |
| `X-Omazy-Event` | `kb.updated` |
| `X-Omazy-Delivery` | The event's `id`. The same on every attempt |

Answer with any `2xx` within `15` seconds. Anything else is tried again, up to
`8` times over about an hour and a half, with a longer wait each time. Answer `410` to
stop the retries for good. Because of retries the same event can arrive twice:
use `X-Omazy-Delivery` to drop a repeat.

`kb.updated` also goes to the workspace's own webhooks and automations that
subscribe to it, with the same `data`.

## Limits

Writes are counted in their own rate bucket, apart from reads and search, so a
large import cannot use up the searches a live agent's backend depends on. See
[Rate limits](/reference/core-api/errors-and-limits/#rate-limits).

None of these routes is sent again by a [client library](/reference/core-api/client-libraries/)
after a failure, except the reads.
