> ## 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.

# Create a thread

> Create a thread before starting a run. For agent conversations, include `assistant_id` and use the same agent ID for the run. The protocol accepts `{}` for an unbound thread, but successful creation does not establish run permission. Save `thread_id` for subsequent requests and `metadata.conversation_id` when a conversation ID is required. Creating a thread does not start the agent. If the run returns 403, see the permission troubleshooting section in the [SDK guide](/en/ai/agent/development-api).



## OpenAPI

````yaml /api/openapi.sdk.json post /api/ai/threads
openapi: 3.0.3
info:
  title: XpertAI SDK HTTP API
  version: 1.0.0
  description: >-
    Current HTTP endpoints used by the SDK guide. Authenticate with a
    server-side API Key using Authorization: Bearer <API_KEY> or x-api-key.
    These endpoints do not require ChatKit. The reference covers the guide
    examples, not every method exported by the SDK. Check deployment support
    before using additional fields.
servers:
  - url: https://api.xpertai.cn
    description: XpertAI API. Replace with your own API origin for private deployments.
security:
  - ApiKeyBearer: []
  - ApiKeyHeader: []
tags:
  - name: Assistants
  - name: Threads
  - name: Runs
  - name: Contexts
  - name: Store
paths:
  /api/ai/threads:
    post:
      tags:
        - Threads
      summary: Create a thread
      description: >-
        Create a thread before starting a run. For agent conversations, include
        `assistant_id` and use the same agent ID for the run. The protocol
        accepts `{}` for an unbound thread, but successful creation does not
        establish run permission. Save `thread_id` for subsequent requests and
        `metadata.conversation_id` when a conversation ID is required. Creating
        a thread does not start the agent. If the run returns 403, see the
        permission troubleshooting section in the [SDK
        guide](/en/ai/agent/development-api).
      operationId: create_thread
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                thread_id:
                  type: string
                  description: Optional UUID; generated when omitted.
                  format: uuid
                assistant_id:
                  type: string
                  description: >-
                    Optional at the protocol level. For agent conversations,
                    supply the target published agent ID at creation and use the
                    same ID for subsequent runs. The TypeScript SDK parameter is
                    `assistantId`. If omitted, the thread is created without an
                    agent binding under the current request identity; a later
                    run using a different execution identity can fail with 403.
                    Binding does not grant additional conversation access.
                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: Successful response.
          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: Invalid input, identifier mismatch or invalid operation state.
        '401':
          description: Missing or invalid API Key.
        '403':
          description: The caller cannot access the requested resource.
        '404':
          description: Resource not found or unavailable to the caller.
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: Conversation ID; it is distinct from 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: Server-side API Key; do not expose it in browser code.
    ApiKeyHeader:
      type: apiKey
      in: header
      name: x-api-key

````

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