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

# 通过角色创建会话

> 使用已保存的 `character_id` 启动会话。返回字段与创建会话接口相同，包括会话和连接信息。

> [accordion] 错误
>
> 会话端点可能返回的错误码请参阅[会话错误](https://docs.vivix.ai/cn/streaming-avatar/api-references/sessions#errors)。


## OpenAPI

````yaml cn/streaming-avatar/api-references/openapi.json POST /v1/realtime-avatar/character-sessions
openapi: 3.1.0
info:
  title: Vivix Streaming Avatar API 中文参考
  version: 1.0.0
  description: Streaming Avatar 会话、音色和角色的 REST 接口。
servers:
  - url: https://api.vivix.ai
security:
  - bearerAuth: []
paths:
  /v1/realtime-avatar/character-sessions:
    post:
      summary: 通过角色创建会话
      description: 使用已保存的 `character_id` 启动会话。返回字段与创建会话接口相同，包括会话和连接信息。
      operationId: createSessionFromCharacter
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - character_id
              properties:
                character_id:
                  type: string
                  description: 作为会话起点的已保存角色 ID，格式类似 `chr_…`。请在 JSON 请求体中传入，而不是放在 URL 路径中。
                idempotency_key:
                  type: string
                  description: 用于重试会话创建的可选键。重试同一请求时复用同一个键；创建新会话时使用新键。
                model:
                  type: string
                  description: 覆盖已保存的模型。
                max_duration_seconds:
                  type: integer
                  description: >-
                    会话时长上限，单位为秒，最小值为 `4`。标准默认值为 20 分钟（`1200`
                    秒），但受工作区会话时长上限约束。请查看响应中的 `expires_at` 了解实际过期时间。
                  minimum: 4
                recording_mode:
                  type: string
                  description: '`on` 或 `off`。传入 `off` 可禁用录制。'
                output:
                  type: object
                  description: 覆盖已保存的 `output`；与已保存对象进行浅合并。
                  properties:
                    aspect_ratio:
                      type: string
                      description: >-
                        请求的角色视频宽高比。支持 `9:16` 和
                        `16:9`。请使用相同宽高比的起始图像；宽高比不匹配会明显降低视频质量。
                    resolution:
                      type: string
                      description: 请求的角色视频分辨率。支持 `480p` 和 `720p`。
                delivery:
                  type: object
                  description: 覆盖已保存的 `delivery`；与已保存对象进行浅合并。
                  properties:
                    media:
                      type: object
                      description: 选择客户端发送和接收实时媒体的方式。
                      properties:
                        transport:
                          type: string
                          description: >-
                            会话请求使用的媒体传输方式。TRTC 使用 `trtc`，Agora 使用 `agora`。省略时默认为
                            TRTC。响应会在 `delivery.media.trtc` 或
                            `delivery.media.agora` 中返回对应服务商的加入信息。
            example:
              character_id: chr_a1b2c3d4e5f6789012345678abcdef01
              idempotency_key: live-20260901-001
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                  - data
                properties:
                  code:
                    type: integer
                    description: 成功时为 `0`；其他值均为错误码。
                  message:
                    type: string
                    description: '`success`，或英文错误说明。'
                  data:
                    type: object
                    properties:
                      session_id:
                        type: string
                        description: 已创建的角色会话 ID。
                      status:
                        type: string
                        description: 当前会话状态，例如 `active`、`closing`、`closed` 或 `failed`。
                      auto_close:
                        type: object
                        description: 首次创建会话时保存的实际生效策略快照。
                        properties:
                          disconnected_timeout_seconds:
                            type: integer
                            description: 实际生效的无控制 WSS 连接超时秒数。0 表示禁用。
                          interaction_idle_timeout_seconds:
                            type: integer
                            description: 实际生效的交互空闲超时秒数。0 表示禁用。
                        required:
                          - disconnected_timeout_seconds
                          - interaction_idle_timeout_seconds
                      session:
                        type: object
                        description: 应用默认值并通过校验后实际生效的可变会话状态。
                        properties:
                          mode:
                            type: string
                            description: 会话模式：`video_avatar`。
                            enum:
                              - video_avatar
                          active_avatar_id:
                            type: string
                            description: 视频中渲染并用于后续响应的角色。
                          source_image_id:
                            type: string
                            description: 当前用于生成角色视频的源图像 ID。
                        required:
                          - mode
                          - active_avatar_id
                      control:
                        type: object
                        description: 会话控制通道的连接信息。
                        properties:
                          url:
                            type: string
                            description: >-
                              控制通道的绝对 WSS URL，用于传输客户端控制事件和服务端状态事件。路径为
                              `/v1/realtime-avatar/control`，会话通过 `session_id`
                              查询参数标识；请始终连接此字段返回的完整 URL。
                          client_secret:
                            type: string
                            description: >-
                              本次会话的控制通道凭证。浏览器通过 `token` 查询参数发送；服务端客户端也可使用 Bearer
                              请求头。凭证在会话结束前始终有效，包括重连场景。请参阅[连接示例](/cn/streaming-avatar/api-references/sessions#connect-and-control)。
                        required:
                          - url
                          - client_secret
                      output:
                        type: object
                        description: >-
                          应用默认值和限制后实际生效的角色视频输出设置。当 `session.mode` 为
                          `video_avatar` 时提供。
                        properties:
                          aspect_ratio:
                            type: string
                            description: 实际生效的角色视频宽高比，为 `9:16` 或 `16:9`。
                          resolution:
                            type: string
                            description: 实际生效的角色视频分辨率，为 `480p` 或 `720p`。
                        required:
                          - aspect_ratio
                          - resolution
                      assets:
                        type: object
                        description: >-
                          可复用的图像引用、音频、语音文本和动作提示。物体互动中的道具或产品请放在 `images`
                          中（请参阅[物体互动](/cn/streaming-avatar/interaction/object-interaction)）；角色的起始图像请放在
                          `avatars[].visual.source_images` 中。所有资产类型的资产 ID
                          必须互不重复。
                        properties:
                          images:
                            type: array
                            description: 道具或产品的参考图像。它们用于保持对象外观，不会替代角色的起始图像，也不会自行把对象放入场景。
                            items:
                              type: object
                              properties:
                                asset_id:
                                  type: string
                                  description: 对象引用 ID，在会话资产中必须唯一。请依次使用 `OBJ_0`、`OBJ_1` 等。
                                url:
                                  type: string
                                  description: 可公开访问的 HTTPS 图像 URL。
                                mime_type:
                                  type: string
                                  description: 图像 MIME 类型，例如 `image/png` 或 `image/jpeg`。
                                description:
                                  type: string
                                  description: >-
                                    可选的视觉说明。如提供，请包含匹配的引用标签，例如对应 `asset_id:
                                    "OBJ_0"` 可写 `a dark gray cylindrical
                                    speaker<OBJ_0>`。请参阅[物体互动](/cn/streaming-avatar/interaction/object-interaction)。
                              required:
                                - asset_id
                                - url
                          audio:
                            type: array
                            description: >-
                              供 `response.script.vocal.type: "audio_asset"`
                              使用的可复用音频资产。
                            items:
                              type: object
                              description: 一个可复用的音频资产。
                              properties:
                                asset_id:
                                  type: string
                                  description: 由调用方定义的音频资产 ID。
                                url:
                                  type: string
                                  description: >-
                                    音频文件的 HTTPS URL。Vivix
                                    必须能够在不添加自定义请求头的情况下直接访问该 URL。
                                mime_type:
                                  type: string
                                  description: 音频 MIME 类型。支持 `audio/wav` 和 `audio/mpeg`。
                              required:
                                - asset_id
                                - url
                          speech_text:
                            type: array
                            description: >-
                              供 `response.script.vocal.type: "speech_asset"`
                              使用的可复用准确语音文本资产。
                            items:
                              type: object
                              description: 一个可复用的语音文本资产。
                              properties:
                                asset_id:
                                  type: string
                                  description: 由调用方定义的语音文本资产 ID。
                                text:
                                  type: string
                                  description: >-
                                    在 `response.script`
                                    中使用时，要合成并通过输出文本事件流式返回的准确文本。
                              required:
                                - asset_id
                                - text
                          visual_prompts:
                            type: array
                            description: >-
                              供 `response.script.visual.visual_prompt_asset_id`
                              使用的可复用视觉提示资产。
                            items:
                              type: object
                              description: 一个可复用的视觉提示资产。
                              properties:
                                asset_id:
                                  type: string
                                  description: 由调用方定义的视觉提示资产 ID。
                                prompt:
                                  type: string
                                  description: 对角色动作、姿势、视线、手势或呈现方式的指令。
                              required:
                                - asset_id
                                - prompt
                      conversation:
                        type: object
                        description: >-
                          应用服务端限制和默认值后实际生效的对话默认设置。结构和含义与请求中的 `conversation`
                          字段相同。
                        properties:
                          tools:
                            type: array
                            description: 会话中的响应可使用的工具。
                            items:
                              type: object
                              description: 一个工具定义。v1 目前仅记录函数工具。
                              properties:
                                type:
                                  type: string
                                  description: 请使用 `function`。
                                name:
                                  type: string
                                  description: 可供响应规划器调用的函数名称。
                                description:
                                  type: string
                                  description: 便于用户理解的工具使用场景说明。
                                parameters:
                                  type: object
                                  description: 描述函数参数的 JSON Schema 对象。
                              required:
                                - type
                                - name
                          response_defaults:
                            type: object
                            description: 后续轮次使用的默认响应生成设置。
                            properties:
                              temperature:
                                type: number
                                description: 生成响应文本时使用的采样温度。支持范围为 `0` 到 `2`。
                              max_output_tokens:
                                type: integer
                                description: 单次响应最多生成的文本 token 数。
                          turn_detection:
                            type: object
                            description: 实时用户输入使用的默认轮次处理方式。
                            properties:
                              type:
                                type: string
                                description: >-
                                  `manual` 表示由客户端显式启动响应；`server_vad`
                                  表示由服务端根据传入的用户音频检测轮次边界。
                              silence_duration_ms:
                                type: integer
                                description: >-
                                  兼容字段。不要使用它调整实际的语句结束时机。语音识别和轮次检测配置请参阅[语音识别配置](/cn/streaming-avatar/integrate/speech-recognition)。
                            required:
                              - type
                          input_audio_transcription:
                            type: object
                            description: 用户音频输入使用的默认转录设置。
                            properties:
                              enabled:
                                type: boolean
                                description: 是否转录用户音频。
                              language:
                                type: string
                                description: BCP-47 `language` 代码；也可设为 `auto`，以自动检测语言。
                      avatars:
                        type: array
                        description: 会话中实际生效的角色定义。结构和含义与请求中的 `avatars` 字段相同。
                        items:
                          type: object
                          properties:
                            avatar_id:
                              type: string
                              description: 由调用方定义的角色 ID，在会话内必须唯一。
                            persona:
                              type: string
                              description: >-
                                使用自然语言或 Markdown
                                描述角色的身份、性格、说话方式、回答习惯、动作偏好和聆听行为。Vivix 会根据这段
                                Persona 自动准备角色行为。请参阅[编写
                                Persona](/cn/streaming-avatar/character/persona)。
                            voice:
                              type: object
                              description: >-
                                多角色会话需要使用不同音色时，可为各角色提供 TTS 配置。请提供
                                `provider`、`provider_voice_id` 和
                                `model`。常规的会话级音色配置无需设置此对象，请改用
                                `pipeline_config.tts_config`。会话级 TTS
                                配置的优先级高于所有角色级音色配置。
                              properties:
                                provider:
                                  type: string
                                  description: >-
                                    TTS 后端，例如
                                    `qwen-audio-3.0-tts-flash_ws`、`elevenlabs` 或
                                    `qwen3tts`。与 `provider_voice_id` 和 `model`
                                    一起使用时必填。
                                provider_voice_id:
                                  type: string
                                  description: >-
                                    服务商侧的音色或风格 ID。与 `provider` 和 `model`
                                    一起使用时必填。
                                model:
                                  type: string
                                  description: >-
                                    服务商的 TTS 模型 ID。与 `provider` 和
                                    `provider_voice_id` 一起使用时必填。
                                speed:
                                  type: number
                                  description: 语速倍数。有效范围为 `0.7` 到 `1.2`。
                              required:
                                - provider
                                - provider_voice_id
                                - model
                            visual:
                              type: object
                              description: 视觉渲染设置。该角色用于视频渲染时必填。
                              properties:
                                source_images:
                                  type: array
                                  description: 该角色可用的已注册源图像。使用基于图像的角色视频渲染时必填。
                                  items:
                                    type: object
                                    description: 一张可选择用于角色视频渲染的源图像。
                                    properties:
                                      source_image_id:
                                        type: string
                                        description: 由调用方定义的源图像 ID，在角色内必须唯一。
                                      url:
                                        type: string
                                        description: >-
                                          可公开访问的源图像 HTTPS URL。其宽高比应与
                                          `output.aspect_ratio`（`9:16` 或
                                          `16:9`）一致；宽高比不匹配会明显降低视频质量。
                                      description:
                                        type: string
                                        description: 对人物、姿势和场景的可选说明。
                                      media_type:
                                        type: string
                                        description: 图像媒体类型，例如 `image/png` 或 `image/jpeg`。
                                      opening:
                                        type: object
                                        description: >-
                                          可选的开场语音或动作。可提供 `dialogue`、`action`
                                          或同时提供两者。选择此图像并开始渲染时执行。
                                        properties:
                                          dialogue:
                                            type: string
                                            description: 要合成的准确开场台词。
                                          action:
                                            type: object
                                            properties:
                                              prompt:
                                                type: string
                                                description: 开场动作的视觉运动提示词。
                                              duration_ms:
                                                type: integer
                                                minimum: 1
                                                description: 开场动作时长，单位为毫秒。
                                            required:
                                              - prompt
                                              - duration_ms
                                            description: 以提示词和持续时间描述的开场动作。
                                          starting_at:
                                            type: integer
                                            minimum: 0
                                            default: 0
                                            description: 从首帧可用起到开场内容开始前的延迟，单位为毫秒。
                                    required:
                                      - source_image_id
                                      - media_type
                                      - url
                                default_source_image_id:
                                  type: string
                                  description: >-
                                    默认图像 ID。角色只有一张源图像时可省略；否则请设置此字段或
                                    `session.source_image_id`。
                              required:
                                - source_images
                          required:
                            - avatar_id
                        minItems: 1
                      delivery:
                        type: object
                        description: 服务端返回的媒体连接信息。当 `session.mode` 为 `video_avatar` 时提供。
                        properties:
                          media:
                            type: object
                            description: >-
                              临时媒体连接信息。在角色视频媒体可用时提供。根据 `transport`
                              的选择，只返回一种媒体类型。这些信息仅适用于本次会话，并非服务商账户凭证。
                            properties:
                              transport:
                                type: string
                                description: 用于区分返回哪种媒体类型的判别字段。支持 `trtc` 和 `agora`。
                              trtc:
                                type: object
                                description: '`trtc` 媒体类型对应的 TRTC 房间加入信息。'
                                properties:
                                  sdk_app_id:
                                    type: string
                                    description: >-
                                      客户端 SDK 使用的 TRTC 应用 ID。以字符串形式返回，符合 64
                                      位整数可能以字符串返回的约定。
                                  room_id:
                                    type: string
                                    description: 本次会话使用的 TRTC 房间 ID。
                                  user_id:
                                    type: string
                                    description: 分配给客户端的 TRTC 用户 ID。
                                  user_sig:
                                    type: string
                                    description: 用于加入 TRTC 房间的会话级签名。
                                  publisher_user_id:
                                    type: string
                                    description: TRTC 房间中发布角色视频的用户 ID。只订阅该用户 ID 即可接收角色视频流。
                                required:
                                  - sdk_app_id
                                  - room_id
                                  - user_id
                                  - user_sig
                                  - publisher_user_id
                              agora:
                                type: object
                                description: '`agora` 媒体类型对应的 Agora 频道加入信息。'
                                properties:
                                  app_id:
                                    type: string
                                    description: 客户端 SDK 使用的 Agora 应用 ID。
                                  channel_name:
                                    type: string
                                    description: 本次会话使用的 Agora 频道名称。
                                  token:
                                    type: string
                                    description: 用于加入 Agora 频道的会话级 token。
                                  user_id:
                                    type: string
                                    description: 分配给客户端的 Agora 用户 ID。
                                  publisher_user_id:
                                    type: string
                                    description: Agora 频道中发布角色视频的用户 ID。只订阅该用户 ID 即可接收角色视频流。
                                required:
                                  - app_id
                                  - channel_name
                                  - token
                                  - user_id
                                  - publisher_user_id
                            required:
                              - transport
                      model:
                        type: string
                        description: 请求中指定的模型。
                      expires_at:
                        type: string
                        format: date-time
                        description: 会话及其控制凭证的过期时间。服务端返回固定过期时间时提供，否则省略。
                      recording_mode:
                        type: string
                        description: 实际生效的录制模式：`on` 或 `off`。
                    required:
                      - session_id
                      - status
                      - auto_close
                      - session
                      - control
                      - conversation
                      - avatars
                      - model
                      - output
              example:
                code: 0
                message: success
                data:
                  session_id: avatar-session-123
                  status: active
                  auto_close:
                    disconnected_timeout_seconds: 60
                    interaction_idle_timeout_seconds: 300
                  session:
                    mode: video_avatar
                    active_avatar_id: host_a
                    source_image_id: front
                  control:
                    url: >-
                      wss://api.vivix.ai/v1/realtime-avatar/control?session_id=avatar-session-123
                    client_secret: ctl_...
                  output:
                    aspect_ratio: '9:16'
                    resolution: 480p
                  assets:
                    audio:
                      - asset_id: welcome_jingle
                        url: https://cdn.example.com/audio/welcome-jingle.wav
                        mime_type: audio/wav
                    speech_text:
                      - asset_id: welcome_line
                        text: Welcome back. I saved your place.
                    visual_prompts:
                      - asset_id: wave_small
                        prompt: smile and give a small wave
                  conversation:
                    tools:
                      - type: function
                        name: lookup_product
                        description: Look up product details by product id.
                        parameters:
                          type: object
                          properties:
                            product_id:
                              type: string
                          required:
                            - product_id
                    response_defaults:
                      temperature: 0.7
                      max_output_tokens: 512
                    turn_detection:
                      type: server_vad
                      silence_duration_ms: 500
                    input_audio_transcription:
                      enabled: true
                      language: auto
                  avatars:
                    - avatar_id: host_a
                      persona: >-
                        You are Host A, a warm livestream host who gives concise
                        product-focused replies.
                      visual:
                        source_images:
                          - source_image_id: front
                            url: https://cdn.example.com/avatars/host-a-front.png
                            description: >-
                              A woman facing the camera in a brightly lit
                              studio, front view.
                            media_type: image/png
                          - source_image_id: side
                            url: https://cdn.example.com/avatars/host-a-side.png
                            description: The same woman in the same studio, side view.
                            media_type: image/png
                        default_source_image_id: front
                    - avatar_id: host_b
                      persona: >-
                        You are Host B, a calm expert who gives brief technical
                        explanations.
                      visual:
                        source_images:
                          - source_image_id: front
                            url: https://cdn.example.com/avatars/host-b-front.png
                            description: >-
                              A man facing the camera in a brightly lit studio,
                              front view.
                            media_type: image/png
                        default_source_image_id: front
                  delivery:
                    media:
                      transport: trtc
                      trtc:
                        sdk_app_id: '1400000000'
                        room_id: avatar-session-123
                        user_id: viewer
                        user_sig: ...
                        publisher_user_id: publisher_xxx
                  model: vivix-a1-stream
                  expires_at: '2026-07-16T10:05:00Z'
        '422':
          $ref: '#/components/responses/OpeningImageRejected'
components:
  responses:
    OpeningImageRejected:
      description: 所选开场图像不符合视频会话的图像要求。
      content:
        application/json:
          schema:
            type: object
            required:
              - code
              - message
              - data
            properties:
              code:
                type: integer
                enum:
                  - 20021
                  - 20022
              message:
                type: string
              data:
                type: 'null'
          examples:
            faceTooSmall:
              summary: 使用主体更近的开场图像
              value:
                code: 20021
                message: >-
                  The avatar's face is too small in the opening image. Use a
                  closer shot with the full face clearly visible. See the
                  first-image guide:
                  https://docs.vivix.ai/streaming-avatar/character/first-image
                data: null
            solidBackground:
              summary: 选择包含背景细节的图像
              value:
                code: 20022
                message: >-
                  For better results, use an opening image with a background
                  that has some detail, such as a room or outdoor scene. See the
                  first-image guide:
                  https://docs.vivix.ai/streaming-avatar/character/first-image
                data: null
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: 你的 Vivix API 密钥。请仅保存在服务端。

````

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