ワークフローを実行
対象アプリ:Workflow。
アプリの公開済みワークフローを実行し、その出力を返します。単一の blocking レスポンス、または streaming の Server-Sent Events フィードのいずれかで返します。公開済みのワークフローが必要です。
1 min read
post/workflows/run
Request example
curl --request POST \
--url https://api.dify.ai/v1/workflows/run \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"inputs": {
"query": "Translate this to French: Hello world"
},
"response_mode": "blocking",
"user": "user_workflow_456"
}'import requests
url = "https://api.dify.ai/v1/workflows/run"
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json",
}
payload = {
"inputs": {
"query": "Translate this to French: Hello world"
},
"response_mode": "blocking",
"user": "user_workflow_456"
}
response = requests.request("POST", url, headers=headers, json=payload)
print(response.text)const response = await fetch('https://api.dify.ai/v1/workflows/run', {
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"inputs": {
"query": "Translate this to French: Hello world"
},
"response_mode": "blocking",
"user": "user_workflow_456"
})
});
const data = await response.json();
console.log(data);<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.dify.ai/v1/workflows/run',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"inputs": {
"query": "Translate this to French: Hello world"
},
"response_mode": "blocking",
"user": "user_workflow_456"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer <token>',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.dify.ai/v1/workflows/run"
payload := strings.NewReader("{\n \"inputs\": {\n \"query\": \"Translate this to French: Hello world\"\n },\n \"response_mode\": \"blocking\",\n \"user\": \"user_workflow_456\"\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))
}HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.dify.ai/v1/workflows/run"))
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\n \"inputs\": {\n \"query\": \"Translate this to French: Hello world\"\n },\n \"response_mode\": \"blocking\",\n \"user\": \"user_workflow_456\"\n}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());require 'uri'
require 'net/http'
url = URI('https://api.dify.ai/v1/workflows/run')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = url.scheme == 'https'
request = Net::HTTP::Post.new(url)
request['Authorization'] = 'Bearer <token>'
request['Content-Type'] = 'application/json'
request.body = '{
"inputs": {
"query": "Translate this to French: Hello world"
},
"response_mode": "blocking",
"user": "user_workflow_456"
}'
response = http.request(request)
puts response.read_bodyResponse
{
"data": {
"created_at": 1705407629,
"elapsed_time": 1.23,
"error": null,
"finished_at": 1705407630,
"id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156",
"outputs": {
"result": "Bonjour le monde"
},
"status": "succeeded",
"total_steps": 3,
"total_tokens": 150,
"workflow_id": "7c3e33d4-2a8b-4e5f-9b1a-d3c6e8f12345"
},
"task_id": "c3800678-a077-43df-a102-53f23ed20b88",
"workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156"
}event: ping
data: {"event": "workflow_started", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "workflow_id": "7c3e33d4-2a8b-4e5f-9b1a-d3c6e8f12345", "inputs": {"draft": "Hello"}, "created_at": 1705407629, "reason": "initial"}}
data: {"event": "human_input_required", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"form_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890", "form_token": "tok_abc123", "node_id": "approval_node", "node_title": "Approval", "form_content": "ドラフトを確認してください。", "inputs": [{"type": "paragraph", "output_variable_name": "comment", "default": null}], "actions": [{"id": "approve", "title": "承認", "button_style": "primary"}], "display_in_ui": false, "resolved_default_values": {"comment": ""}, "expiration_time": 1705494029}}
data: {"event": "workflow_paused", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "status": "paused", "created_at": 1705407629, "elapsed_time": 0.5}}event: ping
data: {"event": "workflow_started", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "workflow_id": "7c3e33d4-2a8b-4e5f-9b1a-d3c6e8f12345", "inputs": {"query": "Translate this"}, "created_at": 1705407629, "reason": "initial"}}
data: {"event": "node_started", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"id": "node_exec_1", "node_id": "node_1", "node_type": "llm", "title": "LLM Node", "index": 1, "created_at": 1705407629}}
data: {"event": "reasoning_chunk", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"reasoning": "Let me translate that.", "node_id": "node_1", "is_final": false}}
data: {"event": "reasoning_chunk", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"reasoning": "", "node_id": "node_1", "is_final": true}}
data: {"event": "text_chunk", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"text": "Bonjour", "from_variable_selector": ["node_1", "text"]}}
data: {"event": "workflow_finished", "task_id": "c3800678-a077-43df-a102-53f23ed20b88", "workflow_run_id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "data": {"id": "fb47b2e6-5e43-4f90-be01-d5c5a088d156", "workflow_id": "7c3e33d4-2a8b-4e5f-9b1a-d3c6e8f12345", "status": "succeeded", "outputs": {"result": "Bonjour le monde"}, "elapsed_time": 1.23, "total_tokens": 150, "total_steps": 3, "created_at": 1705407629, "finished_at": 1705407630}}{
"code": "completion_request_error",
"message": "Completion request failed.",
"status": 400
}{
"code": "invalid_param",
"message": "Arg user must be provided.",
"status": 400
}{
"code": "model_currently_not_support",
"message": "Dify Hosted OpenAI trial currently not support the GPT-4 model.",
"status": 400
}{
"code": "not_workflow_app",
"message": "Please check if your app mode matches the right API route.",
"status": 400
}{
"code": "provider_not_initialize",
"message": "No valid model provider credentials found. Please go to Settings -> Model Provider to complete your provider credentials.",
"status": 400
}{
"code": "provider_quota_exceeded",
"message": "Your quota for Dify Hosted OpenAI has been exhausted. Please go to Settings -> Model Provider to complete your own provider credentials.",
"status": 400
}{
"code": "trigger_workflow_service_mode_unavailable",
"message": "This workflow uses a trigger entry and cannot be invoked through Web App, Service API, OpenAPI, or MCP.",
"status": 403
}{
"code": "rate_limit_error",
"message": "Workflow execution quota limit reached for tenant 8f1e2a90-3b7c-4d55-9a01-6c2f4e8b1d33",
"status": 429
}{
"code": "too_many_requests",
"message": "Too many requests. Please try again later.",
"status": 429
}{
"code": "internal_server_error",
"message": "Internal Server Error.",
"status": 500
}post/workflows/run
Authorization
すべてのリクエストは API キーで認証します:Authorization: Bearer {API_KEY}。アプリのエンドポイントにはアプリの API キーを、ナレッジのエンドポイントにはナレッジベースの API キーを使用します(Dify API クイックスタート)。
キーはサーバーサイドで保管し、クライアントコードには決して埋め込まないでください。キーが欠落または無効なリクエストは HTTP 401(unauthorized)で失敗します。
Body
application/jsonファイルリストです。ファイルとテキストを組み合わせて入力する場合に適しており、モデルが Vision 機能をサポートしている場合にのみ使用できます。ローカルファイルを添付するには、まず ファイルをアップロード でアップロードし、返された
id を upload_file_id として transfer_method: local_file で使用します。Show child attributesHide child attributes
ワークフロー入力変数のキーと値のペアです。アプリが期待する変数名と型は、アプリケーションのパラメータ情報を取得 の user_input_form フィールドで確認できます。
ファイル型の変数には、type、transfer_method、および url または upload_file_id を持つファイルオブジェクトの配列を渡します。
Show child attributesHide child attributes
Show child attributesHide child attributes
レスポンスモードです。同期レスポンスには
blocking(Cloudflare タイムアウトは 100 s)、サーバー送信イベントには streaming を使用します。省略した場合、デフォルトはブロッキング動作になります。Options:
streamingblockingエンドユーザーの識別子。アプリ側で定義し、アプリ内で一意にします。データアクセスをスコープします。ワークフロー実行とそのファイルは、同じ
user を持つ後続のリクエストにのみ表示されます。エンドユーザーの識別 を参照してください。Responses
リクエスト成功。コンテンツタイプと構造はリクエストの response_mode パラメータに依存します。
response_modeがblockingの場合、application/jsonでWorkflowBlockingResponseオブジェクトを返します。response_modeがstreamingの場合、text/event-streamでChunkWorkflowEventオブジェクトのストリームを返します。
Show child attributesHide child attributes
ワークフロー実行が作成された Unix タイムスタンプです。
作成者情報です。ストリーミングの
workflow_finished イベントにのみ存在します。合計経過時間(秒)です。
ワークフローが失敗した場合のエラーメッセージです。
実行中に発生した例外の数です。ストリーミングの
workflow_finished イベントにのみ含まれます。ワークフロー実行中に生成されたファイルです。ストリーミングの
workflow_finished イベントにのみ存在します。ワークフロー実行が完了した Unix タイムスタンプです。
ワークフロー実行 ID です。
ワークフローからの出力データです。
ワークフローの実行ステータスです。
running は実行中、succeeded は正常完了、failed は実行エラー、stopped は手動停止、partial-succeeded は一部のノードが成功し他が失敗、paused は人間の入力待ちを示します。実行されたワークフローの合計ステップ数です。
全ノードで消費された合計トークン数です。
Workflow ID.
進行中の実行のタスク ID です。ワークフロータスクを停止 と組み合わせて、実行中のワークフローをキャンセルします。実行中のみ有効です。
このワークフロー実行記録の永続的な識別子です。ワークフロー実行詳細を取得 と組み合わせて、実行後に結果を取得します。