> ## Documentation Index
> Fetch the complete documentation index at: https://enterprise-docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# チャットメッセージを送信

> チャットアプリケーションにリクエストを送信します。



## OpenAPI

````yaml /ja/3.10.x/develop/api/openapi_chat.json post /chat-messages
openapi: 3.0.1
info:
  title: チャットアプリAPI
  description: >-
    チャットアプリケーションはセッションの永続化をサポートしており、以前のチャット履歴をレスポンスのコンテキストとして使用できます。チャットボットやカスタマーサービス
    AI などに適用できます。
  version: 1.0.0
servers:
  - url: https://{api_base_url}
    description: チャットアプリ API のベース URL です。
    variables:
      api_base_url:
        default: api.dify.ai/v1
        description: API ベース URL のホストとパス（`https://` を除く）。
security:
  - ApiKeyAuth: []
tags:
  - name: チャットメッセージ
    description: チャットメッセージとインタラクションに関連する操作です。
  - name: ファイル操作
    description: ファイルのアップロードとプレビューの操作です。
  - name: エンドユーザー
    description: エンドユーザー情報に関連する操作です。
  - name: メッセージフィードバック
    description: ユーザーフィードバックの操作です。
  - name: 会話管理
    description: 会話管理に関連する操作です。
  - name: 音声・テキスト変換
    description: テキスト読み上げと音声認識の操作です。
  - name: アプリケーション設定
    description: アプリケーション設定と情報を取得する操作です。
  - name: アノテーション管理
    description: ダイレクト返信用のアノテーション管理に関連する操作です。
paths:
  /chat-messages:
    post:
      tags:
        - チャットメッセージ
      summary: チャットメッセージを送信
      description: チャットアプリケーションにリクエストを送信します。
      operationId: sendBasicChatMessageJa
      requestBody:
        description: チャットメッセージを送信するためのリクエストボディ。
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatRequest'
            examples:
              streaming_example:
                summary: リクエスト例 - ストリーミングモード
                value:
                  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://cloud.dify.ai/logo/logo-site.png
              blocking_example:
                summary: リクエスト例 - ブロッキングモード
                value:
                  inputs: {}
                  query: What are the specs of the iPhone 13 Pro Max?
                  response_mode: blocking
                  conversation_id: 45701982-8118-4bc5-8e9b-64562b4555f2
                  user: abc-123
      responses:
        '200':
          description: >-
            リクエスト成功。コンテンツタイプと構造はリクエストの `response_mode` パラメータに依存します。


            - `response_mode` が `blocking` の場合、 `application/json` で
            `ChatCompletionResponse` オブジェクトを返します。

            - `response_mode` が `streaming` の場合、`text/event-stream`
            でサーバー送信イベントのストリームを返します。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletionResponse'
              examples:
                blockingResponse:
                  summary: レスポンス例 - ブロッキングモード
                  value:
                    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
            text/event-stream:
              schema:
                type: string
                description: >-
                  サーバー送信イベント (SSE) のストリームです。


                  **パース**：各イベントは `data: ` プレフィックス付きの JSON オブジェクトの行で、`\n\n`
                  で終了します。JSON をパースする前に `data: ` プレフィックスを除去し、`event`
                  フィールドを読み取ってイベントタイプを判定してください。`ping` イベントは `event: ping`
                  行（`data:` ペイロードなし）として 10 秒ごとに接続維持のために送信されるため、無視してください。


                  **ストリームライフサイクル**：回答は `message` イベント（チャットボットアプリ）、または
                  `agent_thought` と `agent_message` イベント（Agent
                  アプリ）としてストリーミングされ、`message_end`
                  で終了します。テキスト読み上げ（TTS）の自動再生が有効な場合、`tts_message`
                  イベントが間に挟まり、`tts_message_end` が最後のイベントになります。


                  **イベント**：`ping` を除き、すべてのイベントには
                  `conversation_id`、`message_id`、`created_at`（Unix
                  エポック秒）が含まれます。`error` を除くイベントには `task_id` も含まれます。


                  **応答イベント**


                  | イベント | アプリ | 発生タイミング | 主なフィールド |

                  |:---|:---|:---|:---|

                  | `message` | チャットボット | 回答の各チャンク（順番に連結） | `answer` |

                  | `agent_message` | Agent | 回答の各チャンク（順番に連結） | `answer` |

                  | `agent_thought` | Agent | 推論またはツール呼び出しの各ステップ |
                  `position`、`thought`、`tool`、`tool_input`（JSON）、`observation`、`message_files`
                  |

                  | `message_replace` | チャットボット、Agent | 出力モデレーションがそれまでの回答を置き換える
                  | `answer` |

                  | `message_file` | チャットボット、Agent | アシスタントがファイルを返す |
                  `type`、`belongs_to`、`url` |

                  | `message_end` | チャットボット、Agent | 回答が完了 |
                  `metadata`（`usage`、`retriever_resources`） |

                  | `tts_message`、`tts_message_end` | チャットボット、Agent |
                  音声チャンク/終了（TTS 自動再生が有効な場合） | `audio` |


                  **トランスポートイベント**


                  | イベント | アプリ | 発生タイミング | 主なフィールド |

                  |:---|:---|:---|:---|

                  | `error` | チャットボット、Agent | 失敗によりストリームが終了。HTTP は `200` のまま |
                  `status`（例：`400`）、`code`（例：`invalid_param`）、`message` |

                  | `ping` | チャットボット、Agent | 10 秒ごとのキープアライブ | なし |
              examples:
                streamingResponseBasic:
                  summary: Response Example - Streaming (Basic)
                  value: >-
                    data: {"event": "message", "task_id":"mock_task_id",
                    "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290",
                    "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
                    "answer": " I", "created_at": 1679586595} data: {"event":
                    "message_end", "task_id":"mock_task_id", "message_id":
                    "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id":
                    "45701982-8118-4bc5-8e9b-64562b4555f2", "metadata":
                    {"usage": {"total_tokens": 10, "latency": 1.0}}}
                streamingResponseAgent:
                  summary: Response Example - Streaming (Agent)
                  value: >-
                    data: {"event": "agent_thought", "id": "agent_thought_id_1",
                    "task_id": "task123", "message_id": "msg123",
                    "conversation_id": "conv123", "position": 1, "thought":
                    "Thinking about calling a tool...", "tool": "dalle3",
                    "tool_input": "{\"dalle3\": {\"prompt\": \"a cute cat\"}}",
                    "created_at": 1705395332} data: {"event": "message_file",
                    "task_id": "task123", "message_id": "msg123",
                    "conversation_id": "conv123", "id": "file_id_1", "type":
                    "image", "belongs_to": "assistant", "url":
                    "https://example.com/cat.png", "created_at": 1705395332}
                    data: {"event": "agent_message", "task_id": "task123",
                    "message_id": "msg123", "conversation_id": "conv123",
                    "answer": "Here is the image: ", "created_at": 1705395333}
                    data: {"event": "message_end", "task_id":"task123",
                    "message_id": "msg123", "conversation_id": "conv123",
                    "metadata": {"usage": {"total_tokens": 50, "latency": 2.5}}}
        '400':
          description: |-
            - `app_unavailable` : アプリケーションが利用できないか、設定が正しくありません。
            - `not_chat_app` : App mode does not match the API route.
            - `conversation_completed` : The conversation has ended.
            - `provider_not_initialize` : 有効なモデルプロバイダーの認証情報が見つかりません。
            - `provider_quota_exceeded` : モデルプロバイダーのクォータが使い切られました。
            - `model_currently_not_support` : 現在のモデルは利用できません。
            - `completion_request_error` : テキスト生成に失敗しました。
          content:
            application/json:
              examples:
                app_unavailable:
                  summary: app_unavailable
                  value:
                    status: 400
                    code: app_unavailable
                    message: App unavailable, please check your app configurations.
                not_chat_app:
                  summary: not_chat_app
                  value:
                    status: 400
                    code: not_chat_app
                    message: Please check if your app mode matches the right API route.
                conversation_completed:
                  summary: conversation_completed
                  value:
                    status: 400
                    code: conversation_completed
                    message: >-
                      The conversation has ended. Please start a new
                      conversation.
                provider_not_initialize:
                  summary: provider_not_initialize
                  value:
                    status: 400
                    code: provider_not_initialize
                    message: >-
                      No valid model provider credentials found. Please go to
                      Settings -> Model Provider to complete your provider
                      credentials.
                provider_quota_exceeded:
                  summary: provider_quota_exceeded
                  value:
                    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.
                model_currently_not_support:
                  summary: model_currently_not_support
                  value:
                    status: 400
                    code: model_currently_not_support
                    message: >-
                      Dify Hosted OpenAI trial currently not support the GPT-4
                      model.
                completion_request_error:
                  summary: completion_request_error
                  value:
                    status: 400
                    code: completion_request_error
                    message: Completion request failed.
        '404':
          description: '`not_found` : 会話が存在しません。'
          content:
            application/json:
              examples:
                conversation_not_exists:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Conversation Not Exists.
        '429':
          description: |-
            - `too_many_requests` : このアプリケーションへの同時リクエストが多すぎます。
            - `rate_limit_error` : アップストリームのモデルプロバイダーのレート制限を超えました。
          content:
            application/json:
              examples:
                too_many_requests:
                  summary: too_many_requests
                  value:
                    status: 429
                    code: too_many_requests
                    message: Too many requests. Please try again later.
                rate_limit_error:
                  summary: rate_limit_error
                  value:
                    status: 429
                    code: rate_limit_error
                    message: Rate Limit Error
        '500':
          description: '`internal_server_error` : 内部サーバーエラー。'
          content:
            application/json:
              examples:
                internal_server_error:
                  summary: internal_server_error
                  value:
                    status: 500
                    code: internal_server_error
                    message: Internal server error.
components:
  schemas:
    ChatRequest:
      type: object
      required:
        - inputs
        - query
        - user
      properties:
        query:
          type: string
          description: ユーザーの入力/質問内容です。
        inputs:
          type: object
          description: >-
            アプリで定義されたさまざまな変数の値を入力できます。キーと値のペアを含みます。アプリが期待する変数名と型については、[アプリケーションのパラメータ情報を取得](/api-reference/アプリケーション設定/アプリケーションのパラメータ情報を取得)
            レスポンスの `user_input_form` フィールドを参照してください。
          additionalProperties: true
        response_mode:
          type: string
          enum:
            - streaming
            - blocking
          description: >-
            レスポンスの返却モードです。`streaming`（推奨）は SSE を使用します。`blocking`
            は完了後に返します（長時間の処理では中断される場合があります。エージェントアシスタントモードではサポートされていません）。Cloudflare
            のタイムアウトは `100 s` です。省略した場合、デフォルトはブロッキング動作になります。
        user:
          type: string
          description: >-
            アプリケーション内で一意のユーザー識別子です。この識別子はデータアクセスの範囲を限定します——会話、メッセージ、ファイルは同じ
            `user` 値でクエリした場合にのみ表示されます。
        conversation_id:
          type: string
          description: >-
            会話を継続するための会話 ID です。前のメッセージの `conversation_id`
            を渡します。新しい会話を開始するには、このフィールドを省略するか空文字列を渡します。レスポンスで `conversation_id`
            が返されます——以降のメッセージでこの値を渡すとその会話を継続できます。
        files:
          type: array
          description: >-
            マルチモーダル理解用のファイルリストです。画像、ドキュメント、音声、動画を含みます。ローカルファイルを添付するには、まず
            [ファイルをアップロード](/api-reference/ファイル操作/ファイルをアップロード) でアップロードし、返された `id`
            を `upload_file_id` として `transfer_method: local_file` で使用します。
          items:
            type: object
            required:
              - type
              - transfer_method
            properties:
              type:
                type: string
                enum:
                  - image
                  - document
                  - audio
                  - video
                  - custom
                description: ファイルタイプ。
              transfer_method:
                type: string
                enum:
                  - remote_url
                  - local_file
                description: >-
                  転送方法です。ファイル URL の場合は `remote_url`、アップロードファイルの場合は `local_file`
                  です。
              url:
                type: string
                format: url
                description: ファイル URL（`transfer_method` が `remote_url` の場合は必須）です。
              upload_file_id:
                type: string
                description: >-
                  [ファイルをアップロード](/api-reference/ファイル操作/ファイルをアップロード) API
                  から取得したアップロード済みファイル ID です（`transfer_method` が `local_file`
                  の場合は必須）。
        auto_generate_name:
          type: boolean
          description: >-
            会話タイトルを自動生成します。`false` の場合、[会話の名前を変更](/api-reference/会話管理/会話の名前を変更)
            API で `auto_generate: true` を設定して非同期でタイトルを生成します。
          default: true
    ChatCompletionResponse:
      type: object
      properties:
        event:
          type: string
          description: イベントタイプ。`message` に固定されています。
        task_id:
          type: string
          format: uuid
          description: リクエスト追跡およびレスポンス停止 API 用のタスク ID です。
        id:
          type: string
          format: uuid
          description: このレスポンスイベントの一意 ID です。
        message_id:
          type: string
          format: uuid
          description: >-
            一意のメッセージ ID です。フィードバックや推奨質問のエンドポイントを呼び出す際に `message_id`
            パラメータとして使用します。
        conversation_id:
          type: string
          format: uuid
          description: 会話 ID。
        mode:
          type: string
          description: アプリモード。`chat` はチャットボットアプリ、`agent-chat` は Agent アプリです。
        answer:
          type: string
          description: 完全なレスポンスコンテンツ。
        metadata:
          type: object
          description: 使用量と検索リソースを含むメタデータ。
          properties:
            usage:
              $ref: '#/components/schemas/Usage'
            retriever_resources:
              type: array
              description: 使用された検索リソースのリスト。
              items:
                $ref: '#/components/schemas/RetrieverResource'
        created_at:
          type: integer
          format: int64
          description: メッセージ作成タイムスタンプ（Unix エポック秒）。
    Usage:
      type: object
      description: モデルの使用情報です。
      properties:
        prompt_tokens:
          type: integer
          description: プロンプト内のトークン数。
        prompt_unit_price:
          type: string
          format: decimal
          description: プロンプトトークンあたりの単価。
        prompt_price_unit:
          type: string
          format: decimal
          description: プロンプトトークンの価格単位。
        prompt_price:
          type: string
          format: decimal
          description: プロンプトトークンの合計価格。
        completion_tokens:
          type: integer
          description: 補完のトークン数。
        completion_unit_price:
          type: string
          format: decimal
          description: 補完トークンあたりの単価。
        completion_price_unit:
          type: string
          format: decimal
          description: 補完トークンの価格単位。
        completion_price:
          type: string
          format: decimal
          description: 補完トークンの合計価格。
        total_tokens:
          type: integer
          description: 使用されたトークンの合計数。
        total_price:
          type: string
          format: decimal
          description: すべてのトークンの合計価格。
        currency:
          type: string
          description: 課金通貨。
        latency:
          type: number
          format: double
          description: レイテンシ（秒）。
    RetrieverResource:
      type: object
      description: 検索リソースの引用および帰属情報です。
      properties:
        id:
          type: string
          format: uuid
          description: 検索リソースの一意の ID。
        message_id:
          type: string
          format: uuid
          description: このリソースが属するメッセージの ID。
        position:
          type: integer
          description: リスト内のリソースの位置。
        dataset_id:
          type: string
          format: uuid
          description: ナレッジベース ID。
        dataset_name:
          type: string
          description: ナレッジベース名。
        document_id:
          type: string
          format: uuid
          description: ドキュメント ID。
        document_name:
          type: string
          description: ドキュメント名。
        data_source_type:
          type: string
          description: データソースのタイプ。
        segment_id:
          type: string
          format: uuid
          description: ドキュメント内の特定のチャンクの ID。
        score:
          type: number
          format: float
          description: リソースの関連性スコア。
        hit_count:
          type: integer
          description: このチャンクがヒットした回数。
        word_count:
          type: integer
          description: チャンクの単語数。
        segment_position:
          type: integer
          description: ドキュメント内のチャンクの位置。
        index_node_hash:
          type: string
          description: インデックスノードのハッシュ。
        content:
          type: string
          description: リソースからのコンテンツスニペット。
        summary:
          type: string
          nullable: true
          description: チャンクコンテンツの要約。
        created_at:
          type: integer
          format: int64
          description: 作成タイムスタンプ（Unix エポック秒）。
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        API Key 認証です。すべての API リクエストにおいて、`Authorization` HTTP ヘッダーに `Bearer `
        プレフィックスを付けた API Key を含めてください。例：`Authorization: Bearer {API_KEY}`。**API
        Key はサーバーサイドに保存し、クライアントサイドで共有・保存しないことを強く推奨します。API Key
        の漏洩は深刻な結果につながる可能性があります。**

````