Skip to main content
Build and maintain knowledge bases from your own code, without going through the Dify console: create a knowledge base, load documents and chunks into it, organize them with metadata and tags, and query it directly for search or RAG.
A single Knowledge Base API key has access to every knowledge base visible to the account that created the key. Handle your keys carefully to avoid unintended data exposure.

Get Your API Endpoint and Key

In Knowledge, click Service API in the top-right corner to open the API configuration panel. From here:
  • Copy the Service API endpoint, the base URL for every Knowledge API request.
  • Click API Key to create and manage keys.
Store your API key securely on the server side. Never expose it in client-side code or public repositories.

Manage API Access for a Knowledge Base

Every knowledge base is reachable through the API by default. To restrict a specific knowledge base, open it, click API Access in the bottom-left corner, and turn the toggle off.

Create and Manage Knowledge Bases

Add and Update Documents

Document creation is asynchronous. Create the document, then poll until indexing finishes:
1

Create a knowledge base

Call Create an Empty Knowledge Base, or use an existing knowledge base.
2

Add a document

Call Create Document by Text or Create Document by File; both return a batch ID.If you didn’t set indexing_technique (how content is indexed for search) when creating the knowledge base, set it on this first document; later documents inherit it automatically.
3

Poll the indexing status

Poll Get Document Indexing Status with the batch ID until indexing_status reaches completed or error; it progresses through waiting, parsing, cleaning, splitting, and indexing along the way.
  • List Documents: a paginated list, filterable by keyword or indexing status.
  • Get Document: one document’s indexing status, metadata, and processing statistics; the metadata query parameter includes, omits, or returns only the metadata fields.
  • Update Document by Text: update a document’s text content, name, or processing configuration inline; re-triggers indexing when the content changes.
  • Update Document by File: replace a document’s content by uploading a new file, re-triggering indexing.
  • Update Document Status in Batch: enable, disable, archive, or unarchive multiple documents at once.
  • Delete Document: permanently remove a document and every chunk inside it.

Manage Chunks and Child Chunks

  • Create Chunks: add chunks to a document by hand—indexing already chunks uploaded content automatically. Each chunk requires content; documents in Q&A mode also require answer.
  • List Chunks: a paginated list, filterable by keyword or status.
  • Get Chunk: one chunk’s content, keywords, and indexing status.
  • Update Chunk: change a chunk’s content, keywords, or answer; re-triggers indexing for that chunk.
  • Delete Chunk: permanently remove a chunk.
For documents in Parent-child mode (hierarchical_model), child chunks nest under a parent chunk. Child chunks you create or update through the API are always typed customized, unlike the automatic ones the indexing pipeline generates.

Manage Metadata Fields

Metadata fields annotate documents with structured information that retrieval can filter on: Metadata also works as a stable external key: store your source system’s ID on each document, then filter on it in later sync runs to find and update the same documents.

Organize Knowledge Bases with Tags

Tags live at the workspace level, independent of any single knowledge base:

Look Up Available Models

  • Get Available Models: the models available for a given model_type; query text-embedding for embedding models or rerank for reranking models when configuring a knowledge base.