Knowledge Base API#
Dify exposes two parallel sets of REST endpoints for managing knowledge bases (datasets): the Console API (browser/dashboard, under /console/api/datasets/) and the Service API (external integrations, under /v1/datasets/). Both surface the same core capabilities β document upload, metadata field management, and document metadata value assignment β but differ in auth (session-based vs. API-key) and minor response shapes.
Key source files:
- Service API document endpoints:
api/controllers/service_api/dataset/document.py - Service API metadata endpoints:
api/controllers/service_api/dataset/metadata.py - Console metadata endpoints:
api/controllers/console/datasets/metadata.py - Business logic:
api/services/metadata_service.py - Request/response entities:
api/services/entities/knowledge_entities/knowledge_entities.py
Document Upload and Configuration#
All document CRUD operations are available on the Service API:
| Method | Route | Description |
|---|---|---|
POST | /datasets/{dataset_id}/document/create-by-text | Create from raw text |
POST | /datasets/{dataset_id}/document/create-by-file | Upload a file (multipart/form-data) |
PATCH | /datasets/{dataset_id}/documents/{document_id} | Update document by file |
POST | /datasets/{dataset_id}/documents/{document_id}/update-by-text | Update from text |
GET | /datasets/{dataset_id}/documents | List documents (paginated, filterable by keyword and status) |
DELETE | /datasets/{dataset_id}/documents/{document_id} | Delete a document and all its chunks |
Deprecation note: Legacy underscore routes (
create_by_text,create_by_file) remain registered for backward compatibility but are marked deprecated .
Key configuration fields (DocumentTextCreatePayload)#
| Field | Values | Notes |
|---|---|---|
indexing_technique | high_quality | economy |
doc_form | text_model | hierarchical_model |
doc_language | string (e.g., "English") | Language hint for processing optimization. Defaults to "English". |
process_rule | ProcessRule object | Chunking/cleaning rules. Modes: automatic, custom, hierarchical. If omitted, the KB's latest process rule is reused. |
retrieval_model | RetrievalModel object | Controls search method, reranking, top_k, score threshold, and hybrid weights. |
embedding_model / embedding_model_provider | strings | Override embedding model per document. |
For file uploads the same fields are passed as a JSON string in the data form field .
Indexing status tracking#
Document creation is asynchronous. The response includes a batch ID; poll GET /datasets/{dataset_id}/documents/{batch}/indexing-status to track progress through stages: waiting β parsing β cleaning β splitting β indexing β completed .
Getting a document#
GET /datasets/{dataset_id}/documents/{document_id} accepts a metadata query param :
all(default) β full document fields + metadataonlyβ returns onlyid,doc_type,doc_metadatawithoutβ omitsdoc_typeanddoc_metadata
Metadata Field Management#
Metadata fields are schema-level definitions attached to a knowledge base. They allow documents to be annotated with typed structured values that can later be used for filtering during retrieval (see Knowledge Base Metadata Filtering).
Endpoints#
Both the Console and Service APIs expose the same operations :
| Method | Path | Action |
|---|---|---|
POST | /datasets/{dataset_id}/metadata | Create a custom metadata field |
GET | /datasets/{dataset_id}/metadata | List all metadata fields (custom + built-in) with per-field document counts |
PATCH | /datasets/{dataset_id}/metadata/{metadata_id} | Rename a custom field |
DELETE | /datasets/{dataset_id}/metadata/{metadata_id} | Delete a custom field (removes values from all documents) |
GET | /datasets/{dataset_id}/metadata/built-in | List built-in field definitions |
POST | /datasets/{dataset_id}/metadata/built-in/{action} | enable or disable built-in fields for the KB |
The Console metadata endpoints are gated by
@enterprise_license_required.
Enterprise license requirement#
In self-hosted enterprise deployments (DEPLOYMENT_EDITION=ENTERPRISE), all Service API endpoints β including the Knowledge Base API under /v1/datasets/ β require a valid enterprise license. When the enterprise license is invalid (INACTIVE, EXPIRED, or LOST status) or unavailable, requests to /v1 endpoints return 403 Forbidden with the error marker license_required. This global check is enforced by the request gate in app_factory.py before controller-level auth or validation runs.
Note:
ENTERPRISE_ENABLEDhas been deprecated and replaced withDEPLOYMENT_EDITIONas the single source of truth for edition checking (set inapi/configs/deploy/__init__.py).
Custom field schema (MetadataArgs)#
{
"type": "string" | "number" | "time",
"name": "<field_name_max_255_chars>"
}
MetadataService.create_metadata validates that the name is β€ 255 characters, is not a duplicate, and does not conflict with built-in field names .
Built-in fields#
Five system-defined fields are always available via MetadataService.get_built_in_fields() :
document_nameuploaderupload_datelast_update_datesource
Enabling built-in fields for a KB auto-populates them on all existing documents. Disabling removes the values from all documents .
Setting Document Metadata Values#
To assign values to metadata fields on specific documents, use the batch update endpoint:
| Method | Path | Action |
|---|---|---|
POST | /datasets/{dataset_id}/documents/metadata | Set metadata values for one or more documents |
Service API returns {"result": "success"} with HTTP 200 ; Console API returns 204 No Content .
Request body (MetadataOperationData)#
{
"operation_data": [
{
"document_id": "<doc_uuid>",
"metadata_list": [
{ "id": "<metadata_field_id>", "name": "<field_name>", "value": "<value>" }
],
"partial_update": false
}
]
}
operation_dataβ array of per-document operations (DocumentMetadataOperation)metadata_listβ each entry is aMetadataDetailwithid,name, andvalue(string, number, or null)partial_update(defaultfalse) β whentrue, only the listed fields are updated; whenfalse, the document's metadata is replaced entirely
MetadataService.update_documents_metadata acquires per-document Redis locks to prevent concurrent modifications .
Metadata in retrieval#
Document metadata values are stored in the doc_metadata JSON column on DatasetDocument. During retrieval, metadata filtering conditions are translated into SQLAlchemy predicates against this column β see Knowledge Base Metadata Filtering for supported operators and filter structure.