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

# Create a new message in a chat via API key

> Create a message with explicit chatId using API key authentication. MessageIndex is auto-calculated if not provided. Requires CHAT scope.



## OpenAPI

````yaml post /api/chat-messages
openapi: 3.0.0
info:
  title: LeanMCP SDK API
  description: >-
    API endpoints for LeanMCP SDK integration. These endpoints use API key
    authentication and are designed for programmatic access from the CLI and
    SDK.
  version: 1.0.0
servers:
  - url: https://api.leanmcp.com
    description: Production
  - url: http://localhost:3001
    description: Local Development
security: []
tags:
  - name: builds-sdk
  - name: chat-messages-api-key
  - name: chats-api-key
  - name: deployments-api-key
  - name: deployments-sdk
  - name: mapping-sdk
  - name: projects-api-key
paths:
  /api/chat-messages:
    post:
      tags:
        - chat-messages-api-key
      summary: Create a new message in a chat via API key
      description: >-
        Create a message with explicit chatId using API key authentication.
        MessageIndex is auto-calculated if not provided. Requires CHAT scope.
      operationId: ChatMessagesApiKeyController_createMessage
      parameters: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateChatMessageDto'
      responses:
        '201':
          description: Message created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatMessage'
        '404':
          description: Chat not found or access denied
        '409':
          description: Message with same chatId and messageIndex already exists
      security:
        - ApiKeyAuth: []
components:
  schemas:
    CreateChatMessageDto:
      type: object
      properties:
        id:
          type: string
          description: Pre-generated message ID (UUID). Auto-generated if not provided
          example: abc123-def456-789-ghi-012345678901
        chatId:
          type: string
          description: Chat ID this message belongs to (UUID)
          example: c1a83d84-7154-464c-bd19-a9f10e0a067f
        role:
          type: string
          enum:
            - system
            - assistant
            - user
          description: Role of the message sender
        content:
          type: string
          description: Message content/text
        messageIndex:
          type: number
          description: Order of message in chat (0-based). Auto-calculated if not provided
          minimum: 0
          example: 0
        metadata:
          type: object
          description: Additional metadata for the message
          additionalProperties: true
        fileIds:
          description: Array of file IDs to associate with this message
          example:
            - f1a83d84-7154-464c-bd19-a9f10e0a067f
            - f2b94e95-8265-575d-ce2a-b0f21f1b178g
          type: array
          items:
            type: string
      required:
        - chatId
        - role
        - content
    ChatMessage:
      type: object
      properties:
        id:
          type: string
          description: Unique message identifier (UUID)
        chatId:
          type: string
          description: Chat ID this message belongs to (UUID)
          example: c1a83d84-7154-464c-bd19-a9f10e0a067f
        userId:
          type: string
          description: User ID who owns this message
        role:
          type: string
          enum:
            - system
            - assistant
            - user
          description: Role of the message sender
        content:
          type: string
          description: Message content/text
        messageIndex:
          type: number
          description: Order of message in the chat (0-based)
          minimum: 0
        createdAt:
          type: string
          description: Timestamp when message was created
        updatedAt:
          type: string
          description: Timestamp when message was last updated
        metadata:
          type: object
          description: Additional metadata for the message
          additionalProperties: true
      required:
        - id
        - chatId
        - userId
        - role
        - content
        - messageIndex
        - createdAt
        - updatedAt
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API Key
      description: Enter your LeanMCP API key

````