DifyナレッジのAPIキーを取得
ページ上部の「ナレッジ」をクリック

「API」をクリック

ページ右の「APIキー」をクリック

「新しいシークレットキーを作成」をクリック

コピーして「OK」をクリック

操作したいデータセットid(ナレッジid)を取得
対象のナレッジを選択

ナレッジを選択後のURLからデータセットidを抽出

サンプルソース
ナレッジリストの取得
use Illuminate\Support\Facades\Http;
$dify_knowledge_api_key = "DifyナレッジAPIキー";
$dify_dataset_id = "データセットid";
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $dify_knowledge_api_key,
])->get('https://api.dify.ai/v1/datasets/' . $dify_dataset_id . '/documents');
// レスポンスの取得
$responseBody = $response->json();
return $responseBody['data'];
ナレッジの登録
use Illuminate\Support\Facades\Http;
$dify_knowledge_api_key = "DifyナレッジAPIキー";
$dify_dataset_id = "データセットid";
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $dify_knowledge_api_key,
'Content-Type' => 'application/json',
])->post('https://api.dify.ai/v1/datasets/' . $dify_dataset_id . '/document/create_by_text', [
'name' => "ファイル名(タイトル)",
'text' => "ナレッジ",
'indexing_technique' => 'high_quality',
'process_rule' => [
'mode' => 'automatic',
],
]);
// レスポンスの取得
$responseBody = $response->json();
// document_id取得
$dify_document_id = $responseBody['document']['id'];
返ってくるdocument_idを使って更新や削除を行う
ナレッジの更新
use Illuminate\Support\Facades\Http;
$dify_knowledge_api_key = "DifyナレッジAPIキー";
$dify_dataset_id = "データセットid";
$document_id = "ドキュメントid";
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $dify_knowledge_api_key,
'Content-Type' => 'application/json',
])->post('https://api.dify.ai/v1/datasets/' . $dify_dataset_id . '/documents/' . $document_id . '/update_by_text', [
'name' => "ファイル名(タイトル)",
'text' => "ナレッジ",
]);
// レスポンスの取得
$responseBody = $response->json();
ナレッジの削除
use Illuminate\Support\Facades\Http;
$dify_knowledge_api_key = "DifyナレッジAPIキー";
$dify_dataset_id = "データセットid";
$document_id = "ドキュメントid";
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $dify_knowledge_api_key,
])->delete('https://api.dify.ai/v1/datasets/' . $dify_dataset_id . '/documents/' . $document_id);
// レスポンスの取得
$responseBody = $response->json();