curl --request POST \
--url 'https://{api_base_url}/chat-messages' \
--header 'Authorization: Bearer {api_key}' \
--header 'Content-Type: application/json' \
--no-buffer \
--data '{"query": "What are the specs of the iPhone 13 Pro Max?", "inputs": {}, "response_mode": "streaming", "user": "{user}"}'import requests
url = "https://{api_base_url}/chat-messages"
payload = {
"inputs": { "city": "San Francisco" },
"query": "What are the specs of the iPhone 13 Pro Max?",
"response_mode": "streaming",
"conversation_id": "",
"user": "abc-123",
"files": [
{
"type": "image",
"transfer_method": "remote_url",
"url": "https://example.com/logo.png"
}
]
}
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({
inputs: {city: 'San Francisco'},
query: 'What are the specs of the iPhone 13 Pro Max?',
response_mode: 'streaming',
conversation_id: '',
user: 'abc-123',
files: [
{
type: 'image',
transfer_method: 'remote_url',
url: 'https://example.com/logo.png'
}
]
})
};
fetch('https://{api_base_url}/chat-messages', 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}/chat-messages",
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([
'inputs' => [
'city' => 'San Francisco'
],
'query' => 'What are the specs of the iPhone 13 Pro Max?',
'response_mode' => 'streaming',
'conversation_id' => '',
'user' => 'abc-123',
'files' => [
[
'type' => 'image',
'transfer_method' => 'remote_url',
'url' => 'https://example.com/logo.png'
]
]
]),
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}/chat-messages"
payload := strings.NewReader("{\n \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\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}/chat-messages")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{api_base_url}/chat-messages")
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 \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"event": "message",
"task_id": "c3800678-a077-43df-a102-53f23ed20b88",
"id": "b01a39de-3480-4f3e-9f1e-4841a80f8e5e",
"message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
"conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
"mode": "chat",
"answer": "iPhone 13 Pro Max specs are listed here:...",
"metadata": {
"usage": {
"prompt_tokens": 1033,
"prompt_unit_price": "0.001",
"prompt_price_unit": "0.001",
"prompt_price": "0.0010330",
"completion_tokens": 128,
"completion_unit_price": "0.002",
"completion_price_unit": "0.001",
"completion_price": "0.0002560",
"total_tokens": 1161,
"total_price": "0.0012890",
"currency": "USD",
"latency": 0.7682376249867957
},
"retriever_resources": [
{
"position": 1,
"dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
"dataset_name": "iPhone",
"document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
"document_name": "iPhone List",
"segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
"score": 0.98457545,
"content": "\"Model\",\"Release Date\",\"Display Size\",\"Resolution\",\"Processor\",\"RAM\",\"Storage\",\"Camera\",\"Battery\",\"Operating System\" \"iPhone 13 Pro Max\",\"September 24, 2021\",\"6.7 inch\",\"1284 x 2778\",\"Hexa-core (2x3.23 GHz Avalanche + 4x1.82 GHz Blizzard)\",\"6 GB\",\"128, 256, 512 GB, 1TB\",\"12 MP\",\"4352 mAh\",\"iOS 15\""
}
]
},
"created_at": 1705407629
}チャットメッセージを送信
対象アプリ:Chatflow、チャットボット、Agent。
チャットアプリにメッセージを送信し、アシスタントの応答を返します。ストリーミングレスポンスのイベントはアプリタイプによって異なります。
curl --request POST \
--url 'https://{api_base_url}/chat-messages' \
--header 'Authorization: Bearer {api_key}' \
--header 'Content-Type: application/json' \
--no-buffer \
--data '{"query": "What are the specs of the iPhone 13 Pro Max?", "inputs": {}, "response_mode": "streaming", "user": "{user}"}'import requests
url = "https://{api_base_url}/chat-messages"
payload = {
"inputs": { "city": "San Francisco" },
"query": "What are the specs of the iPhone 13 Pro Max?",
"response_mode": "streaming",
"conversation_id": "",
"user": "abc-123",
"files": [
{
"type": "image",
"transfer_method": "remote_url",
"url": "https://example.com/logo.png"
}
]
}
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({
inputs: {city: 'San Francisco'},
query: 'What are the specs of the iPhone 13 Pro Max?',
response_mode: 'streaming',
conversation_id: '',
user: 'abc-123',
files: [
{
type: 'image',
transfer_method: 'remote_url',
url: 'https://example.com/logo.png'
}
]
})
};
fetch('https://{api_base_url}/chat-messages', 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}/chat-messages",
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([
'inputs' => [
'city' => 'San Francisco'
],
'query' => 'What are the specs of the iPhone 13 Pro Max?',
'response_mode' => 'streaming',
'conversation_id' => '',
'user' => 'abc-123',
'files' => [
[
'type' => 'image',
'transfer_method' => 'remote_url',
'url' => 'https://example.com/logo.png'
]
]
]),
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}/chat-messages"
payload := strings.NewReader("{\n \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\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}/chat-messages")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{api_base_url}/chat-messages")
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 \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"event": "message",
"task_id": "c3800678-a077-43df-a102-53f23ed20b88",
"id": "b01a39de-3480-4f3e-9f1e-4841a80f8e5e",
"message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
"conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
"mode": "chat",
"answer": "iPhone 13 Pro Max specs are listed here:...",
"metadata": {
"usage": {
"prompt_tokens": 1033,
"prompt_unit_price": "0.001",
"prompt_price_unit": "0.001",
"prompt_price": "0.0010330",
"completion_tokens": 128,
"completion_unit_price": "0.002",
"completion_price_unit": "0.001",
"completion_price": "0.0002560",
"total_tokens": 1161,
"total_price": "0.0012890",
"currency": "USD",
"latency": 0.7682376249867957
},
"retriever_resources": [
{
"position": 1,
"dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
"dataset_name": "iPhone",
"document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
"document_name": "iPhone List",
"segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
"score": 0.98457545,
"content": "\"Model\",\"Release Date\",\"Display Size\",\"Resolution\",\"Processor\",\"RAM\",\"Storage\",\"Camera\",\"Battery\",\"Operating System\" \"iPhone 13 Pro Max\",\"September 24, 2021\",\"6.7 inch\",\"1284 x 2778\",\"Hexa-core (2x3.23 GHz Avalanche + 4x1.82 GHz Blizzard)\",\"6 GB\",\"128, 256, 512 GB, 1TB\",\"12 MP\",\"4352 mAh\",\"iOS 15\""
}
]
},
"created_at": 1705407629
}承認
すべてのリクエストは API キーで認証します:Authorization: Bearer {API_KEY}。アプリのエンドポイントにはアプリの API キーを、ナレッジのエンドポイントにはナレッジベースの API キーを使用します(Dify API クイックスタート)。
キーはサーバーサイドで保管し、クライアントコードには決して埋め込まないでください。キーが欠落または無効なリクエストは HTTP 401(unauthorized)で失敗します。
ボディ
チャットメッセージを送信するためのリクエストボディ。
ユーザーのメッセージです。
アプリの入力変数の値を、変数名をキーとして指定します。期待される名前と型は、アプリケーションのパラメータ情報を取得 の user_input_form フィールドで確認できます。
エンドユーザーの識別子。アプリ側で定義し、アプリ内で一意にします。会話、メッセージ、ファイルは、同じ user を持つリクエストにのみ表示されます。エンドユーザーの識別 を参照してください。
レスポンスの返却モードです。省略した場合はブロッキングになります。
streaming(推奨):応答は Server-Sent Events として届きます。blocking:生成の完了後に返します。長時間の生成は中断されるおそれがあります。API の前段にあるリバースプロキシは、応答がタイムアウト内に届かないと接続を終了する場合があります。- Agent アプリは
streamingのみに対応します。blockingのリクエストは 400 で失敗します。
streaming, blocking メッセージに添付するファイルです。ローカルファイルの場合は、まず ファイルをアップロード でアップロードし、返された id を upload_file_id として transfer_method: local_file で参照します。
Show child attributes
Show child attributes
レスポンス
リクエスト成功。コンテンツタイプと構造はリクエストの response_mode パラメータに依存します。
response_modeがblockingの場合、application/jsonでChatCompletionResponseオブジェクトを返します。response_modeがstreamingの場合、text/event-streamでサーバー送信イベントのストリームを返します。
イベントタイプ。message に固定されています。
リクエスト追跡およびレスポンス停止 API 用のタスク ID です。
このレスポンスイベントの一意 ID です。
一意のメッセージ ID です。フィードバックや推奨質問のエンドポイントを呼び出す際に message_id パラメータとして使用します。
会話 ID。
アプリモード。chat はチャットボットアプリ、agent-chat は Agent アプリ、advanced-chat は Chatflow アプリです。
完全なレスポンスコンテンツ。
使用量と検索リソースを含むメタデータ。
Show child attributes
Show child attributes
メッセージ作成タイムスタンプ(Unix エポック秒)。