# Create chat session (/docs/api-reference/chat-sessions/createChatSession)

> Open a text chat with an agent and get its greeting. Then send messages with Send chat message, and call End chat session when you are done. A session with no message for 30 minutes ends on its own.

**POST** `/chat/sessions/create`

Open a text chat with an agent and get its greeting. Then send messages
with Send chat message, and call End chat session when you are done.
A session with no message for 30 minutes ends on its own.

Call this from your server: your API key must never reach a browser
or app. [Add a chat agent to any website or app](/docs/chat-api)
walks through the whole flow.

A `live` chat is for your customers:

- talks to the agent as you last published it;
- runs the agent's tools for real (`tool_results` is refused). Its end
  call and transfer tools end the chat; a transfer reports the number
  in `transferred_to` and connects nobody, so route the customer
  yourself;
- is listed under the `API-Chat` channel in Call Logs;
- runs post-call analysis and the agent's post-call actions
  (webhooks, email, connected apps), as a call does.

A `test` chat (the default) is for checking an agent:

- talks to the agent as you last edited it, so you can fix the agent
  and test again before you publish;
- never runs the agent's tools. Each tool returns what you set in
  `tool_results`, or `{"status": "ok", "data": {}}` when you set
  nothing. The end call and transfer tools still run, so you see the
  agent end or transfer the chat;
- is listed under the `MCP-Test` channel, apart from your real chats;
- runs post-call analysis to extract variables, and sends nothing to
  your webhooks, email or connected apps;

Each agent reply is billed as a chat message, paid from your balance
before the agent answers (see [Chat API](/docs/chat-api#cost)). A
balance too low for one more reply is refused with
`insufficient_balance`. A session ends after 100 agent replies.

A test chat's response also has a `briefing`: the agent's tools with
their exact names and parameters (key `tool_results` by these names),
and the dynamic variables its prompt expects, with the ones you did not
pass in `custom_variables`.

To test an agent well, play one caller goal per chat. Judge the agent
on the tools it called and their arguments (`tool_calls`) and on the
final record, not only on what it said.

```yaml
operationId: createChatSession
requestBody:
  required: true
  content:
    application/json:
      schema:
        type: object
        required:
          - agent_id
        properties:
          agent_id:
            type: integer
            description: ID of the agent to chat with.
            example: 158910
          mode:
            type: string
            default: test
            enum:
              - live
              - test
            description: >-
              `live` for your customers (real tools and post-call actions), `test` to check the
              agent (scripted tools, nothing delivered).
          custom_variables:
            type: object
            additionalProperties: true
            description: Variables that personalize the conversation, as on a call.
            example:
              name: Ada
          tool_results:
            type: object
            additionalProperties: true
            description: |
              Test chats only. What each of the agent's tools returns, keyed by
              tool name. The agent sees the value as the tool's result. Script a
              failure as `{"status": "error", "error": "<short_code>"}`. A tool you
              leave out returns `{"status": "ok", "data": {}}`.
            example:
              book_appointment:
                status: error
                error: calendar_unavailable
              lookup_order:
                status: ok
                data:
                  state: shipped
responses:
  '201':
    description: Session created. The reply is the agent's greeting.
    content:
      application/json:
        schema:
          type: object
          properties:
            session_id:
              type: integer
              example: 4521
            reply:
              type: string
              description: What the agent said.
              example: Tomorrow at 10 is open. Can I have your name?
            tool_calls:
              type: array
              description: Tools the agent called on this turn, with their arguments and the result it got.
              items:
                type: object
                properties:
                  function_name:
                    type: string
                  args:
                    description: The arguments the agent passed.
                  result:
                    description: What the tool returned to the agent.
                  success:
                    type: boolean
                  error:
                    type: string
                  source:
                    type: string
                    enum:
                      - scripted
                      - default
                      - real
                    description: |
                      Test chats only. Where the result came from: your
                      `tool_results` (`scripted`), an empty success because you
                      scripted nothing for this tool (`default`), or the tool itself
                      (`real`: the end call and transfer tools).
            should_end:
              type: boolean
              description: True when the agent ended the chat on this turn (same as `status` being `ended`).
            status:
              type: string
              enum:
                - active
                - ended
              description: |
                `ended` when the agent closed the chat on this turn, with its end
                call or transfer tool. The chat is already saved: show the reply,
                and do not call End chat session. The fields below are then set.
            ended_by:
              type: string
              nullable: true
              enum:
                - agent
                - transfer
                - client
                - idle
                - turn_limit
              description: |
                How the chat ended: the agent ran its end call tool (`agent`) or
                transfer tool (`transfer`), you called End chat session (`client`),
                nobody wrote for 30 minutes (`idle`), or the agent sent 100 replies
                (`turn_limit`). Null while the chat is active.
            transferred_to:
              type: string
              nullable: true
              description: When the agent transferred the chat, the number it sent the customer to.
              example: '+15551234567'
            transcript:
              type: array
              description: The whole chat, oldest first, once it has ended. The greeting has no customer line.
              items:
                type: object
                properties:
                  role:
                    type: string
                    enum:
                      - agent
                      - customer
                  text:
                    type: string
              example:
                - role: agent
                  text: Hi, this is Sam from Demo Dental. How can I help?
                - role: customer
                  text: I need to talk to a person.
                - role: agent
                  text: Sure, I am transferring you to +15551234567 now.
            briefing:
              type: object
              description: Test chats only, on Create chat session. What you need to plan scenarios.
              properties:
                agent_version:
                  type: string
                  enum:
                    - draft
                  description: Test chats talk to your latest edits, before you publish.
                tools:
                  type: array
                  items:
                    type: object
                    properties:
                      name:
                        type: string
                        description: The exact name to key `tool_results` by.
                      description:
                        type: string
                      parameters:
                        type: object
                        additionalProperties: true
                      runs_in_test:
                        type: boolean
                        description: >-
                          True for the end call and transfer tools, which run for real and end the
                          chat.
                dynamic_variables:
                  type: array
                  items:
                    type: string
                  description: The dynamic variables the agent's prompt uses.
                missing_variables:
                  type: array
                  items:
                    type: string
                  description: Of those, the ones this session did not get in `custom_variables`.
  '400':
    description: >-
      Unsupported mode (`unsupported_mode`), a malformed body, or `tool_results` on a live chat
      (`invalid_request`).
    content:
      application/json:
        schema:
          type: object
          properties:
            error:
              type: string
              description: Machine-readable error code.
            error_description:
              type: string
              description: Human-readable explanation.
            code:
              type: string
              description: |
                Stable, specific reason. Present on the newer endpoints (Voice
                cloning, WhatsApp); older endpoints return only `error`.
            request_id:
              type: string
              description: |
                Correlates this response with the server log. Present alongside
                `code`. Also sent as the `X-Request-Id` header.
        example:
          error: unsupported_mode
          error_description: mode must be 'test' or 'live'.
  '401':
    description: Missing or invalid API key.
  '402':
    description: Your balance is too low for another chat message.
    content:
      application/json:
        schema:
          type: object
          properties:
            error:
              type: string
              description: Machine-readable error code.
            error_description:
              type: string
              description: Human-readable explanation.
            code:
              type: string
              description: |
                Stable, specific reason. Present on the newer endpoints (Voice
                cloning, WhatsApp); older endpoints return only `error`.
            request_id:
              type: string
              description: |
                Correlates this response with the server log. Present alongside
                `code`. Also sent as the `X-Request-Id` header.
        example:
          error: insufficient_balance
          error_description: Your balance is too low for another chat message. Please top up.
  '404':
    description: Agent not found in your organization.
    content:
      application/json:
        schema:
          type: object
          properties:
            error:
              type: string
              description: Machine-readable error code.
            error_description:
              type: string
              description: Human-readable explanation.
            code:
              type: string
              description: |
                Stable, specific reason. Present on the newer endpoints (Voice
                cloning, WhatsApp); older endpoints return only `error`.
            request_id:
              type: string
              description: |
                Correlates this response with the server log. Present alongside
                `code`. Also sent as the `X-Request-Id` header.
        example:
          error: agent_not_found
          error_description: Agent not found or access denied
  '502':
    description: The agent did not answer. Retry the request.
    content:
      application/json:
        schema:
          type: object
          properties:
            error:
              type: string
              description: Machine-readable error code.
            error_description:
              type: string
              description: Human-readable explanation.
            code:
              type: string
              description: |
                Stable, specific reason. Present on the newer endpoints (Voice
                cloning, WhatsApp); older endpoints return only `error`.
            request_id:
              type: string
              description: |
                Correlates this response with the server log. Present alongside
                `code`. Also sent as the `X-Request-Id` header.
        example:
          error: agent_unavailable
          error_description: The agent did not answer. Try the message again.
```

**curl**

```bash
curl -X POST "https://omnidim.io/api/v1/chat/sessions/create" \
  -H "Authorization: Bearer $OMNIDIM_API_KEY"
```