数据源
创建数据源
POST
/
knowledge-fs
/
spaces
/
{knowledge_base_id}
/
sources
创建数据源
curl --request POST \
--url https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"connectionId": "c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f",
"metadata": {
"client_request_id": "source-20260907-0001",
"providerKind": "website"
},
"name": "Docs site",
"permissionScope": [],
"status": "disabled",
"type": "web",
"uri": "https://docs.example.com"
}
'import requests
url = "https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources"
payload = {
"connectionId": "c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f",
"metadata": {
"client_request_id": "source-20260907-0001",
"providerKind": "website"
},
"name": "Docs site",
"permissionScope": [],
"status": "disabled",
"type": "web",
"uri": "https://docs.example.com"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
connectionId: 'c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f',
metadata: {client_request_id: 'source-20260907-0001', providerKind: 'website'},
name: 'Docs site',
permissionScope: [],
status: 'disabled',
type: 'web',
uri: 'https://docs.example.com'
})
};
fetch('https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'connectionId' => 'c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f',
'metadata' => [
'client_request_id' => 'source-20260907-0001',
'providerKind' => 'website'
],
'name' => 'Docs site',
'permissionScope' => [
],
'status' => 'disabled',
'type' => 'web',
'uri' => 'https://docs.example.com'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources"
payload := strings.NewReader("{\n \"connectionId\": \"c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f\",\n \"metadata\": {\n \"client_request_id\": \"source-20260907-0001\",\n \"providerKind\": \"website\"\n },\n \"name\": \"Docs site\",\n \"permissionScope\": [],\n \"status\": \"disabled\",\n \"type\": \"web\",\n \"uri\": \"https://docs.example.com\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"connectionId\": \"c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f\",\n \"metadata\": {\n \"client_request_id\": \"source-20260907-0001\",\n \"providerKind\": \"website\"\n },\n \"name\": \"Docs site\",\n \"permissionScope\": [],\n \"status\": \"disabled\",\n \"type\": \"web\",\n \"uri\": \"https://docs.example.com\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"connectionId\": \"c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f\",\n \"metadata\": {\n \"client_request_id\": \"source-20260907-0001\",\n \"providerKind\": \"website\"\n },\n \"name\": \"Docs site\",\n \"permissionScope\": [],\n \"status\": \"disabled\",\n \"type\": \"web\",\n \"uri\": \"https://docs.example.com\"\n}"
response = http.request(request)
puts response.read_body{
"connection_id": "c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f",
"created_at": "2026-09-06T01:30:00Z",
"credential_configured": null,
"id": "6f1c2b0e-3d0b-4a5e-9f4c-1c0e6a7b8d90",
"knowledge_space_id": "0a9d3c2e-5b6f-4e1a-8c7d-2f3e4a5b6c7d",
"last_synced_at": null,
"metadata": {
"client_request_id": "source-20260907-0001",
"providerKind": "website",
"tenantId": "2f1e0d9c-8b7a-4695-8473-2a1b0c9d8e7f"
},
"name": "Docs site",
"permission_scope": [],
"status": "disabled",
"sync_policy": null,
"sync_workflow": null,
"type": "web",
"updated_at": "2026-09-06T01:30:00Z",
"uri": "https://docs.example.com",
"version": 1
}Authorizations
每个请求都通过 API 密钥认证:Authorization: Bearer {API_KEY}。应用接口使用应用 API 密钥,知识库接口使用知识库 API 密钥(快速开始)。
API 密钥应保存在服务端,切勿嵌入客户端代码。缺失或无效的密钥会返回 HTTP 401(unauthorized)。
Path Parameters
Body
application/json
标记为 nullable 的可选字段,传 null 等同于省略。
规范化后的请求不得超过 262,144 个 UTF-8 字节。参见 请求大小限制。
为数据源提供读取凭据的连接。必须属于此知识库,且处于 active 状态。取自 获取数据源连接列表 返回的 id。
提供商设置,以及你自己的非敏感元数据。添加唯一的 client_request_id,可在创建响应丢失时识别出此数据源。该值会随数据源一起返回,但 API 不校验其唯一性,也不会对请求去重。
将 providerKind 设为连接的 configuration.providerKind,并选择与之匹配的数据源 type。来自连接的设置可能不会出现在数据源响应中。
如果基于现有数据源的响应构造 metadata,需去掉以 __knowledgeFs 开头的字段、credentials 以及 parameters 对象。credentialId 若存在,指向 Dify 保存的凭据。
Was this page helpful?
⌘I
创建数据源
curl --request POST \
--url https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"connectionId": "c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f",
"metadata": {
"client_request_id": "source-20260907-0001",
"providerKind": "website"
},
"name": "Docs site",
"permissionScope": [],
"status": "disabled",
"type": "web",
"uri": "https://docs.example.com"
}
'import requests
url = "https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources"
payload = {
"connectionId": "c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f",
"metadata": {
"client_request_id": "source-20260907-0001",
"providerKind": "website"
},
"name": "Docs site",
"permissionScope": [],
"status": "disabled",
"type": "web",
"uri": "https://docs.example.com"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
connectionId: 'c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f',
metadata: {client_request_id: 'source-20260907-0001', providerKind: 'website'},
name: 'Docs site',
permissionScope: [],
status: 'disabled',
type: 'web',
uri: 'https://docs.example.com'
})
};
fetch('https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'connectionId' => 'c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f',
'metadata' => [
'client_request_id' => 'source-20260907-0001',
'providerKind' => 'website'
],
'name' => 'Docs site',
'permissionScope' => [
],
'status' => 'disabled',
'type' => 'web',
'uri' => 'https://docs.example.com'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources"
payload := strings.NewReader("{\n \"connectionId\": \"c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f\",\n \"metadata\": {\n \"client_request_id\": \"source-20260907-0001\",\n \"providerKind\": \"website\"\n },\n \"name\": \"Docs site\",\n \"permissionScope\": [],\n \"status\": \"disabled\",\n \"type\": \"web\",\n \"uri\": \"https://docs.example.com\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"connectionId\": \"c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f\",\n \"metadata\": {\n \"client_request_id\": \"source-20260907-0001\",\n \"providerKind\": \"website\"\n },\n \"name\": \"Docs site\",\n \"permissionScope\": [],\n \"status\": \"disabled\",\n \"type\": \"web\",\n \"uri\": \"https://docs.example.com\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{api_base_url}/knowledge-fs/spaces/{knowledge_base_id}/sources")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"connectionId\": \"c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f\",\n \"metadata\": {\n \"client_request_id\": \"source-20260907-0001\",\n \"providerKind\": \"website\"\n },\n \"name\": \"Docs site\",\n \"permissionScope\": [],\n \"status\": \"disabled\",\n \"type\": \"web\",\n \"uri\": \"https://docs.example.com\"\n}"
response = http.request(request)
puts response.read_body{
"connection_id": "c3d4e5f6-7a8b-4c9d-8e0f-1a2b3c4d5e6f",
"created_at": "2026-09-06T01:30:00Z",
"credential_configured": null,
"id": "6f1c2b0e-3d0b-4a5e-9f4c-1c0e6a7b8d90",
"knowledge_space_id": "0a9d3c2e-5b6f-4e1a-8c7d-2f3e4a5b6c7d",
"last_synced_at": null,
"metadata": {
"client_request_id": "source-20260907-0001",
"providerKind": "website",
"tenantId": "2f1e0d9c-8b7a-4695-8473-2a1b0c9d8e7f"
},
"name": "Docs site",
"permission_scope": [],
"status": "disabled",
"sync_policy": null,
"sync_workflow": null,
"type": "web",
"updated_at": "2026-09-06T01:30:00Z",
"uri": "https://docs.example.com",
"version": 1
}