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

# 获取会话历史消息

> **适用于**：Chatflow、新 Agent、聊天助手、Agent。

以滚动加载的格式返回历史聊天记录，首页返回最新的 `limit` 条消息，即按时间倒序排列。



## OpenAPI

````yaml /zh/api-reference/openapi_service.json get /messages
openapi: 3.0.1
info:
  title: Dify 服务 API
  description: 用于 Dify 应用与知识库的 REST API。应用类接口使用应用 API 密钥认证，知识库类接口使用知识库 API 密钥认证。
  version: 1.0.0
servers:
  - url: https://{api_base_url}
    description: Dify 服务 API 的基础 URL。自部署时，替换为你的 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: 与管理标注直接回复相关的操作。
  - name: 人工介入
    description: 暂停等待人工输入的工作流恢复操作。
  - name: 工作流运行
    description: 用于执行和管理工作流的操作。
  - name: 文本生成消息
    description: 文本生成相关操作。
  - name: 知识库
    description: 用于管理知识库的操作，包括创建、配置和检索。
  - name: 文档
    description: 用于在知识库中创建、更新和管理文档的操作。
  - name: 分段
    description: 用于管理分段和子分段的操作。
  - name: 元数据
    description: 用于管理知识库元数据字段和文档元数据值的操作。
  - name: 标签
    description: 用于管理知识库标签和标签绑定的操作。
  - name: 模型
    description: 用于获取可用模型的操作。
  - name: 知识流水线
    description: 用于管理和运行知识流水线的操作，包括数据源插件和流水线执行。
paths:
  /messages:
    get:
      tags:
        - 会话管理
      summary: 获取会话历史消息
      description: |-
        **适用于**：Chatflow、新 Agent、聊天助手、Agent。

        以滚动加载的格式返回历史聊天记录，首页返回最新的 `limit` 条消息，即按时间倒序排列。
      operationId: getBasicChatConversationHistoryCn
      parameters:
        - name: conversation_id
          in: query
          required: true
          description: 会话 ID。
          schema:
            type: string
        - name: user
          in: query
          required: false
          description: 用户标识符。
          schema:
            type: string
        - name: first_id
          in: query
          required: false
          description: 当前页第一条聊天记录的 ID。默认为 `null`（获取最新消息）。获取后续页面时，使用当前列表中第一条消息的 ID 来获取更早的消息。
          schema:
            type: string
        - name: limit
          in: query
          required: false
          description: 每次请求返回的聊天历史消息数量。
          schema:
            type: integer
            default: 20
            minimum: 1
            maximum: 100
      responses:
        '200':
          description: 成功获取会话历史。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ConversationHistoryResponse'
              examples:
                conversationHistory:
                  summary: 响应示例
                  value:
                    limit: 20
                    has_more: false
                    data:
                      - id: 9da23599-e713-473b-982c-4328d4f5c78a
                        conversation_id: 45701982-8118-4bc5-8e9b-64562b4555f2
                        parent_message_id: null
                        inputs:
                          city: San Francisco
                        query: What are the specs of the iPhone 13 Pro Max?
                        answer: iPhone 13 Pro Max specs are listed here:...
                        status: normal
                        error: null
                        message_files: []
                        feedback:
                          rating: like
                        retriever_resources: []
                        agent_thoughts: []
                        created_at: 1705407629
                        extra_contents: []
        '400':
          description: '`not_chat_app` : 应用模式与 API 路由不匹配。'
          content:
            application/json:
              examples:
                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.
        '404':
          description: |-
            - `not_found` : 会话不存在。
            - `not_found` : 第一条消息不存在。
          content:
            application/json:
              examples:
                conversation_not_exists:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Conversation Not Exists.
                first_message_not_exists:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: First Message Not Exists.
      x-codeSamples:
        - lang: bash
          label: cURL
          source: |-
            curl --request GET \
              --url 'https://{api_base_url}/messages?conversation_id={conversation_id}&user={user}' \
              --header 'Authorization: Bearer {api_key}'
components:
  schemas:
    ConversationHistoryResponse:
      type: object
      properties:
        limit:
          type: integer
          description: 每页条目数。
        has_more:
          type: boolean
          description: 是否有更多消息。
        data:
          type: array
          description: 消息列表。
          items:
            $ref: '#/components/schemas/ConversationMessageItem'
    ConversationMessageItem:
      type: object
      description: 会话中的单条消息。
      properties:
        id:
          type: string
          format: uuid
          description: 消息 ID。
        conversation_id:
          type: string
          format: uuid
          description: 会话 ID。
        parent_message_id:
          type: string
          format: uuid
          nullable: true
          description: 线程会话的父消息 ID。
        inputs:
          type: object
          additionalProperties: true
          description: 该消息的输入变量。
        query:
          type: string
          description: 用户查询文本。
        answer:
          type: string
          description: 助手回答文本。
        status:
          type: string
          description: 消息状态。成功消息为 `normal`，生成失败时为 `error`。
        error:
          type: string
          nullable: true
          description: 当 `status` 为 `error` 时的错误消息。
        message_files:
          type: array
          description: 附加到该消息的文件。
          items:
            $ref: '#/components/schemas/MessageFileItem'
        feedback:
          type: object
          nullable: true
          description: 该消息的用户反馈。
          properties:
            rating:
              type: string
              description: 反馈评分。`like` 为正面，`dislike` 为负面。
        retriever_resources:
          type: array
          description: 该消息使用的检索资源。
          items:
            $ref: '#/components/schemas/RetrieverResource'
        agent_thoughts:
          type: array
          description: 该消息的 Agent 思考。
          items:
            $ref: '#/components/schemas/AgentThoughtItem'
        created_at:
          type: integer
          format: int64
          description: 创建时间戳（Unix 纪元秒）。
        extra_contents:
          type: array
          description: 与此消息关联的附加执行内容，例如 Chatflow 工作流中人工介入节点的表单数据。
          items:
            $ref: '#/components/schemas/HumanInputContent'
    MessageFileItem:
      type: object
      description: 附加到消息的文件。
      properties:
        id:
          type: string
          format: uuid
          description: File ID.
        filename:
          type: string
          description: 原始文件名。
        type:
          type: string
          description: 文件类型，例如 `image`。
        url:
          type: string
          format: url
          nullable: true
          description: 文件的预览 URL。
        mime_type:
          type: string
          nullable: true
          description: 文件的 MIME 类型。
        size:
          type: integer
          nullable: true
          description: 文件大小（字节）。
        transfer_method:
          type: string
          description: >-
            使用的传输方式。`remote_url` 表示基于 URL 的文件，`local_file` 表示已上传的文件，`tool_file`
            表示工具生成的文件。
        belongs_to:
          type: string
          nullable: true
          description: 该文件的所属者。用户上传的文件为 `user`，助手生成的文件为 `assistant`。
        upload_file_id:
          type: string
          format: uuid
          nullable: true
          description: 通过 `local_file` 传输时的上传文件 ID。
    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 纪元秒）。
    AgentThoughtItem:
      type: object
      description: 消息中的 Agent 思考步骤。
      properties:
        id:
          type: string
          format: uuid
          description: Agent 思考 ID。
        chain_id:
          type: string
          nullable: true
          description: 该思考的链 ID。
        message_id:
          type: string
          format: uuid
          description: 该思考所属的唯一消息 ID。
        position:
          type: integer
          description: 该思考的位置。
        thought:
          type: string
          description: LLM 正在思考的内容。
        tool:
          type: string
          description: 调用的工具，以 `;` 分隔。
        tool_labels:
          type: object
          nullable: true
          additionalProperties: true
          description: 已使用工具的标签。
        tool_input:
          type: string
          description: 工具输入（JSON 格式）。
        observation:
          type: string
          description: 工具调用的响应。
        files:
          type: array
          items:
            type: string
          description: 与该思考相关的文件 ID。
        created_at:
          type: integer
          format: int64
          description: 创建时间戳。
    HumanInputContent:
      type: object
      description: 人工介入节点的执行内容，包括表单定义和提交数据。
      properties:
        workflow_run_id:
          type: string
          description: 该内容所属的工作流运行 ID。
        submitted:
          type: boolean
          description: 人工介入表单是否已提交。
        type:
          type: string
          description: '`human_input` 表示人工介入内容。'
        form_definition:
          $ref: '#/components/schemas/HumanInputFormDefinition'
          nullable: true
          description: 人工介入节点的表单定义。当内容表示提交响应时为 `null`。
        form_submission_data:
          $ref: '#/components/schemas/HumanInputFormSubmissionData'
          nullable: true
          description: 已提交的表单数据。表单尚未提交时为 `null`。
    HumanInputFormDefinition:
      type: object
      description: 由人工介入节点渲染的人工介入表单定义。
      properties:
        form_id:
          type: string
          description: 表单唯一标识符。
        node_id:
          type: string
          description: 生成该表单的人工介入节点 ID。
        node_title:
          type: string
          description: 人工介入节点的标题。
        form_content:
          type: string
          description: 与表单一起显示的 Markdown 或文本内容。
        inputs:
          type: array
          description: 表单中的输入字段。
          items:
            $ref: '#/components/schemas/FormInput'
        actions:
          type: array
          description: 表单上可用的操作按钮。
          items:
            $ref: '#/components/schemas/UserAction'
        display_in_ui:
          type: boolean
          description: 表单是否应在 UI 中显示。
        form_token:
          type: string
          nullable: true
          description: 表单提交认证的令牌。
        resolved_default_values:
          type: object
          additionalProperties: true
          description: 表单输入的已解析默认值，按输出变量名称索引。
        expiration_time:
          type: integer
          description: 表单过期的 Unix 时间戳。
    HumanInputFormSubmissionData:
      type: object
      description: 来自已提交的人工介入表单的数据。
      properties:
        node_id:
          type: string
          description: 人工介入节点的 ID。
        node_title:
          type: string
          description: 人工介入节点的标题。
        rendered_content:
          type: string
          description: 表单提交的渲染内容。
        action_id:
          type: string
          description: 被点击的操作按钮的 ID。
        action_text:
          type: string
          description: 被点击的操作按钮的显示文本。
    FormInput:
      type: object
      description: 表单输入字段定义。
      properties:
        type:
          type: string
          description: >-
            表单输入控件类型。可用值：`paragraph`（多行文本输入）、`select`（从列表中单选）、`file`（单文件上传）和
            `file-list`（多文件上传）。
        output_variable_name:
          type: string
          description: 存储输入值的变量名称。
        default:
          $ref: '#/components/schemas/FormInputDefault'
          nullable: true
          description: 该输入的默认值配置。
    UserAction:
      type: object
      description: 人工介入表单上的操作按钮。
      properties:
        id:
          type: string
          description: 操作唯一标识符。
        title:
          type: string
          description: 按钮显示文本。
        button_style:
          type: string
          description: '`primary`, `default`, `accent`, or `ghost`.'
    FormInputDefault:
      type: object
      description: 表单输入的默认值配置。
      properties:
        type:
          type: string
          description: >-
            `variable` for dynamic values from 工作流 variables, `constant` for
            static values.
        selector:
          type: array
          items:
            type: string
          description: 当 `type` 为 `variable` 时的变量选择器路径。
        value:
          type: string
          description: 当 `type` 为 `constant` 时的静态值。
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        API Key 认证。对于所有 API 请求，请在 `Authorization` HTTP 头中包含您的 API Key，并加上
        `Bearer ` 前缀。示例：`Authorization: Bearer {API_KEY}`。**强烈建议将 API Key
        存储在服务端，不要在客户端共享或存储，以避免 API Key 泄漏导致严重后果。**缺少或无效的 API Key 会返回 HTTP
        `401`，错误码为 `unauthorized`。

````