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

# Create a chat completion



## OpenAPI

````yaml openapi.json post /v1/chat/completions
openapi: 3.1.0
info:
  title: Alter Public API (OpenAI Compatible)
  version: 1.0.0
  description: Public-facing OpenAI-compatible endpoints.
servers:
  - url: https://alterhq.com/api
security: []
paths:
  /v1/chat/completions:
    post:
      summary: Create a chat completion
      operationId: createChatCompletion
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
      responses:
        '200':
          description: The chat completion response or stream.
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ChatCompletionResponse'
                  - $ref: '#/components/schemas/ChatCompletionChunk'
            text/event-stream:
              schema:
                $ref: '#/components/schemas/ChatCompletionChunk'
        '400':
          $ref: '#/components/responses/BadRequestError'
        '401':
          $ref: '#/components/responses/UnauthorizedError'
      security:
        - BearerAuth: []
components:
  schemas:
    ChatCompletionRequest:
      type: object
      required:
        - model
        - messages
      properties:
        model:
          type: string
        messages:
          type: array
          items:
            $ref: '#/components/schemas/ChatMessage'
        temperature:
          type: number
          minimum: 0
          maximum: 2
        top_p:
          type: number
          minimum: 0
          maximum: 1
        'n':
          type: integer
          minimum: 1
        stream:
          type: boolean
        stop:
          oneOf:
            - type: string
            - type: array
              items:
                type: string
        max_tokens:
          type: integer
          minimum: 1
        presence_penalty:
          type: number
          minimum: -2
          maximum: 2
        frequency_penalty:
          type: number
          minimum: -2
          maximum: 2
        logit_bias:
          type: object
        user:
          type: string
        tools:
          type: array
          items:
            type: object
            properties:
              type:
                type: string
                const: function
              function:
                type: object
                properties:
                  name:
                    type: string
                  description:
                    type: string
                  parameters:
                    type: object
                required:
                  - name
                  - parameters
            required:
              - type
              - function
    ChatCompletionResponse:
      type: object
      required:
        - id
        - object
        - created
        - model
        - choices
      properties:
        id:
          type: string
        object:
          type: string
          const: chat.completion
        created:
          type: integer
        model:
          type: string
        choices:
          type: array
          items:
            $ref: '#/components/schemas/ChatCompletionChoice'
        usage:
          $ref: '#/components/schemas/Usage'
    ChatCompletionChunk:
      type: object
      required:
        - id
        - object
        - created
        - model
        - choices
      properties:
        id:
          type: string
        object:
          type: string
          const: chat.completion.chunk
        created:
          type: integer
        model:
          type: string
        choices:
          type: array
          items:
            $ref: '#/components/schemas/ChatCompletionChunkChoice'
    ChatMessage:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum:
            - system
            - user
            - assistant
            - tool
        content:
          oneOf:
            - type: string
            - type: array
              items:
                type: object
                properties:
                  type:
                    type: string
                required:
                  - type
        name:
          type: string
        tool_call_id:
          type: string
    ChatCompletionChoice:
      type: object
      required:
        - index
        - message
        - finish_reason
      properties:
        index:
          type: integer
        message:
          $ref: '#/components/schemas/ChatMessage'
        finish_reason:
          type:
            - string
            - 'null'
    Usage:
      type: object
      required:
        - prompt_tokens
        - completion_tokens
        - total_tokens
      properties:
        prompt_tokens:
          type: integer
        completion_tokens:
          type: integer
        total_tokens:
          type: integer
    ChatCompletionChunkChoice:
      type: object
      required:
        - index
        - delta
        - finish_reason
      properties:
        index:
          type: integer
        delta:
          $ref: '#/components/schemas/ChatCompletionDelta'
        finish_reason:
          type:
            - string
            - 'null'
    ErrorResponse:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
            type:
              type: string
            param:
              type:
                - string
                - 'null'
            code:
              oneOf:
                - type: string
                - type: integer
                - type: 'null'
          required:
            - message
            - type
      required:
        - error
    ChatCompletionDelta:
      type: object
      properties:
        role:
          type: string
          enum:
            - assistant
            - tool
            - system
            - user
        content:
          type: string
        tool_calls:
          type: array
          items:
            type: object
  responses:
    BadRequestError:
      description: Bad request.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    UnauthorizedError:
      description: Unauthorized.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: Alter token
      description: Bearer token issued by the Alter macOS app.

````