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

# 创建线程

> 发起运行前先创建线程。调用智能体时应传入 `assistant_id`，后续运行使用同一个智能体 ID。接口允许用 `{}` 创建未绑定的线程，但创建成功不代表具备运行权限。保存 `thread_id` 供后续请求使用，需要对话 ID 时读取 `metadata.conversation_id`。创建线程不会启动智能体。若运行返回 403，请查看 [SDK 接入指南](/zh-hans/ai/agent/development-api)中的权限排查说明。



## OpenAPI

````yaml /api/openapi.sdk.zh-Hans.json post /api/ai/threads
openapi: 3.0.3
info:
  title: XpertAI SDK HTTP 接口
  version: 1.0.0
  description: >-
    SDK 指南使用的当前 HTTP 接口。使用服务端 API Key，通过 Authorization: Bearer <API_KEY> 或
    x-api-key 鉴权，不依赖 ChatKit。本参考覆盖指南中的案例，不代表 SDK 导出的所有方法都已实现；使用其他字段前请核对部署支持情况。
servers:
  - url: https://api.xpertai.cn
    description: XpertAI API，私有部署请替换为自己的 API 服务地址。
security:
  - ApiKeyBearer: []
  - ApiKeyHeader: []
tags:
  - name: Assistants
  - name: Threads
  - name: Runs
  - name: Contexts
  - name: Store
paths:
  /api/ai/threads:
    post:
      tags:
        - Threads
      summary: 创建线程
      description: >-
        发起运行前先创建线程。调用智能体时应传入 `assistant_id`，后续运行使用同一个智能体 ID。接口允许用 `{}`
        创建未绑定的线程，但创建成功不代表具备运行权限。保存 `thread_id` 供后续请求使用，需要对话 ID 时读取
        `metadata.conversation_id`。创建线程不会启动智能体。若运行返回 403，请查看 [SDK
        接入指南](/zh-hans/ai/agent/development-api)中的权限排查说明。
      operationId: create_thread
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                thread_id:
                  type: string
                  description: 可选 UUID，省略时自动生成。
                  format: uuid
                assistant_id:
                  type: string
                  description: >-
                    接口协议允许省略。调用智能体时，应在创建阶段传入目标已发布智能体的 ID，后续运行使用同一个 ID。TypeScript
                    SDK 对应参数为
                    `assistantId`。省略时按当前请求身份创建未绑定智能体的线程；后续运行若使用不同的执行身份，可能返回
                    403。绑定不会授予额外的对话访问权限。
                if_exists:
                  type: string
                  enum:
                    - raise
                    - do_nothing
                metadata:
                  type: object
                  properties:
                    title:
                      type: string
                    fromEndUserId:
                      type: string
            example:
              assistant_id: 22222222-2222-4222-8222-222222222222
      responses:
        '201':
          description: 请求成功。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Thread'
              example:
                thread_id: 11111111-1111-4111-8111-111111111111
                status: idle
                metadata:
                  conversation_id: 44444444-4444-4444-8444-444444444444
                  assistant_id: 22222222-2222-4222-8222-222222222222
        '400':
          description: 请求参数、标识关联或操作状态不正确。
        '401':
          description: 缺少 API Key 或 API Key 无效。
        '403':
          description: 调用方无权访问目标资源。
        '404':
          description: 资源不存在或对调用方不可用。
components:
  schemas:
    Thread:
      type: object
      properties:
        thread_id:
          type: string
        status:
          type: string
        created_at:
          type: string
          format: date-time
        updated_at:
          type: string
          format: date-time
        metadata:
          type: object
          properties:
            conversation_id:
              type: string
              description: 对话 ID，与 thread_id 不同。
            assistant_id:
              type: string
            title:
              type: string
          additionalProperties: true
        values:
          type: object
          additionalProperties: true
      required:
        - thread_id
        - status
  securitySchemes:
    ApiKeyBearer:
      type: http
      scheme: bearer
      description: 服务端 API Key，不要暴露到浏览器代码中。
    ApiKeyHeader:
      type: apiKey
      in: header
      name: x-api-key

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.