> For the complete documentation index, see [llms.txt](https://docs.clickai.vn/clickai-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clickai.vn/clickai-docs/clickai-docs-en/database/external-knowledge-api.md).

# External Knowledge API

## Table of Contents

·       \[Authentication]\(#authentication)

·       \[Request]\(#request)

·       \[Response]\(#response)

·       \[Error Handling]\(#error-handling)

&#x20;

## Authentication

ClickAI authenticates with your external knowledge service using a Bearer token in the Authorization header:

Authorization: Bearer {API\_KEY}

The API Key is the credential you provide when registering the External Knowledge API in ClickAI.

&#x20;

## Request

### Endpoint

POST {your-endpoint}/retrieval\
Content-Type: application/json\
Authorization: Bearer {API\_KEY}

ClickAI automatically appends /retrieval to the registered API Endpoint. For example, if you configure the endpoint as <https://your-service.com>, ClickAI will send requests to <https://your-service.com/retrieval>.

### Body

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Required</td><td valign="top">Description</td></tr><tr><td valign="top">knowledge_id</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">ID of the knowledge source in your external system</td></tr><tr><td valign="top">query</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">Search query from the user</td></tr><tr><td valign="top">retrieval_setting</td><td valign="top">object</td><td valign="top">✅</td><td valign="top">Retrieval configuration (see below)</td></tr><tr><td valign="top">metadata_condition</td><td valign="top">object</td><td valign="top">❌</td><td valign="top">Metadata filter conditions (see below)</td></tr></tbody></table>

&#x20;

### retrieval\_setting

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Description</td></tr><tr><td valign="top">top_k</td><td valign="top">integer</td><td valign="top">Maximum number of chunks to return</td></tr><tr><td valign="top">score_threshold</td><td valign="top">float</td><td valign="top">Minimum similarity score (0.0 - 1.0). Default 0.0 (no filtering)</td></tr></tbody></table>

&#x20;

### metadata\_condition

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Description</td></tr><tr><td valign="top">logical_operator</td><td valign="top">string</td><td valign="top">Logic operator: and or or. Default and</td></tr><tr><td valign="top">conditions</td><td valign="top">array</td><td valign="top">List of filter conditions</td></tr></tbody></table>

&#x20;

Each condition contains:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Description</td></tr><tr><td valign="top">name</td><td valign="top">string</td><td valign="top">Metadata field name</td></tr><tr><td valign="top">comparison_operator</td><td valign="top">string</td><td valign="top">Comparison operator</td></tr><tr><td valign="top">value</td><td valign="top">any</td><td valign="top">Comparison value (not required for empty, not empty)</td></tr></tbody></table>

&#x20;

Supported Comparison Operators:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Data type</td><td valign="top">Operators</td></tr><tr><td valign="top">String</td><td valign="top">contains, not contains, start with, end with, is, is not, in, not in, empty, not empty</td></tr><tr><td valign="top">Number</td><td valign="top">=, ≠, >, &#x3C;, ≥, ≤, empty, not empty</td></tr><tr><td valign="top">Date</td><td valign="top">before, after, is, empty, not empty</td></tr></tbody></table>

&#x20;

### Example Request

{\
&#x20; "knowledge\_id": "your-knowledge-id",\
&#x20; "query": "What is ClickAI?",\
&#x20; "retrieval\_setting": {\
&#x20;   "top\_k": 3,\
&#x20;   "score\_threshold": 0.5\
&#x20; }\
}

&#x20;

## Response

Response must contain a records field as an array. If no results, return an empty array:

{"records": \[]}

### records

Each record in the records array contains:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Required</td><td valign="top">Description</td></tr><tr><td valign="top">content</td><td valign="top">string</td><td valign="top">✅</td><td valign="top">Text content of the chunk</td></tr><tr><td valign="top">score</td><td valign="top">float</td><td valign="top">✅</td><td valign="top">Relevance/similarity score (0.0 - 1.0)</td></tr><tr><td valign="top">title</td><td valign="top">string</td><td valign="top">❌</td><td valign="top">Source document title</td></tr><tr><td valign="top">metadata</td><td valign="top">object</td><td valign="top">✅</td><td valign="top">Additional metadata. Must be an object {}, NOT null</td></tr></tbody></table>

&#x20;

### Example Response

{\
&#x20; "records": \[\
&#x20;   {\
&#x20;     "content": "ClickAI is a comprehensive AI platform for businesses.",\
&#x20;     "score": 0.98,\
&#x20;     "title": "introduction.txt",\
&#x20;     "metadata": {\
&#x20;       "path": "s3://clickai/introduction.txt",\
&#x20;       "description": "ClickAI introduction document"\
&#x20;     }\
&#x20;   },\
&#x20;   {\
&#x20;     "content": "ClickAI allows building AI applications without code.",\
&#x20;     "score": 0.85,\
&#x20;     "title": "features.txt",\
&#x20;     "metadata": {}\
&#x20;   }\
&#x20; ]\
}

&#x20;

## Error Handling

When errors occur, the API should return an object containing error\_code and error\_msg:

<table data-header-hidden><thead><tr><th valign="top"></th><th valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Field</td><td valign="top">Type</td><td valign="top">Description</td></tr><tr><td valign="top">error_code</td><td valign="top">integer</td><td valign="top">Error code</td></tr><tr><td valign="top">error_msg</td><td valign="top">string</td><td valign="top">Detailed error message</td></tr></tbody></table>

&#x20;

### Example Error Response

{\
&#x20; "error\_code": 1002,\
&#x20; "error\_msg": "Authorization failed. Please check your API key."\
}

&#x20;

*📖 Previous: \[External Knowledge]\(./06-external-knowledge.md) · Next: \[Manage Documents & Chunks]\(./08-manage-documents-chunks.md)*
