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

# Create Chat Completion



## OpenAPI

````yaml https://api.aiql.io/openapi.json post /v1/chat/completions
openapi: 3.1.0
info:
  title: aiql-api
  version: 0.1.0
servers:
  - url: https://api.aiql.io
security: []
tags:
  - name: Health
    description: Service health checks.
  - name: Workspaces
    description: Workspace management.
  - name: Ingestions
    description: Document ingestion pipeline.
  - name: Queries
    description: Natural-language queries.
  - name: Sources
    description: Ingested sources and their contents.
  - name: Requests
    description: Source addition/removal requests awaiting review.
  - name: Request Changes
    description: Individual source add/remove changes on a request.
  - name: Request Reviews
    description: Reviews of a request, including draft and submit.
  - name: Request Comments
    description: Comments on a request review.
  - name: Canvases
    description: Brainstorm canvases.
  - name: Dashboards
    description: Dashboards.
  - name: Presentations
    description: Slide presentations.
  - name: Patterns
    description: User-defined ontology patterns and matched instances.
  - name: Written Documents
    description: Authored documents.
  - name: Prompts
    description: Reusable prompt templates.
  - name: Apps
    description: Workspace automations.
  - name: Tools
    description: Standalone convert/split/shape/extract tools.
  - name: Realtime
    description: Voice session mint and tool execution.
  - name: Connectors
    description: Connector catalog and connect flows.
  - name: Connections
    description: Workspace connector connections.
  - name: Chat Completions
    description: OpenAI-compatible chat completions over workspace queries.
paths:
  /v1/chat/completions:
    post:
      tags:
        - Chat Completions
      summary: Create Chat Completion
      operationId: create_chat_completion_v1_chat_completions_post
      parameters:
        - name: AiQL-Workspace
          in: header
          required: true
          schema:
            type: string
            description: ID of the workspace the request operates on.
            title: Aiql-Workspace
          description: ID of the workspace the request operates on.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletion'
        '401':
          description: 'Authentication failed: missing or invalid API key.'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - HTTPBearer: []
      x-codeSamples:
        - lang: bash
          label: cURL
          source: |-
            curl -X POST https://api.aiql.io/v1/chat/completions \
              -H "Authorization: Bearer $AIQL_API_KEY" \
              -H "AiQL-Workspace: $AIQL_WORKSPACE_ID" \
              -H "Content-Type: application/json" \
              -d '{"model": "ask", "messages": [{"role": "user", "content": "list every invoice over $1,000 from last quarter"}]}'
        - lang: python
          label: Python
          source: |
            import os
            from openai import OpenAI

            client = OpenAI(
                base_url="https://api.aiql.io/v1",
                api_key=os.environ["AIQL_API_KEY"],
                default_headers={"AiQL-Workspace": "ws_3a1b"},
            )

            completion = client.chat.completions.create(
                model="ask",
                messages=[
                    {
                        "role": "user",
                        "content": "list every invoice over $1,000 from last quarter",
                    }
                ],
            )
            print(completion.choices[0].message.content)
        - lang: typescript
          label: TypeScript
          source: |
            import OpenAI from "openai";

            const client = new OpenAI({
              baseURL: "https://api.aiql.io/v1",
              apiKey: process.env.AIQL_API_KEY,
              defaultHeaders: { "AiQL-Workspace": "ws_3a1b" },
            });

            const completion = await client.chat.completions.create({
              model: "ask",
              messages: [
                {
                  role: "user",
                  content: "list every invoice over $1,000 from last quarter",
                },
              ],
            });
            console.log(completion.choices[0].message.content);
components:
  schemas:
    ChatCompletionRequest:
      properties:
        model:
          type: string
          title: Model
          description: 'Interaction mode: ask, reason, or build.'
        messages:
          items:
            $ref: '#/components/schemas/ChatMessage'
          type: array
          title: Messages
          description: OpenAI-style chat messages. The last user message is the prompt.
        stream:
          type: boolean
          title: Stream
          default: false
        'n':
          anyOf:
            - type: integer
            - type: 'null'
          title: 'N'
          default: 1
      additionalProperties: true
      type: object
      required:
        - model
        - messages
      title: ChatCompletionRequest
    ChatCompletion:
      properties:
        id:
          type: string
          title: Id
        object:
          type: string
          const: chat.completion
          title: Object
          default: chat.completion
        created:
          type: integer
          title: Created
        model:
          type: string
          title: Model
        choices:
          items:
            $ref: '#/components/schemas/ChatCompletionChoice'
          type: array
          title: Choices
        usage:
          $ref: '#/components/schemas/ChatCompletionUsage'
      type: object
      required:
        - id
        - created
        - model
        - choices
        - usage
      title: ChatCompletion
    ErrorResponse:
      type: object
      title: ErrorResponse
      properties:
        detail:
          type: string
          title: Detail
      required:
        - detail
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ChatMessage:
      properties:
        role:
          type: string
          title: Role
        content:
          anyOf:
            - type: string
            - items:
                additionalProperties: true
                type: object
              type: array
            - type: 'null'
          title: Content
      additionalProperties: true
      type: object
      required:
        - role
      title: ChatMessage
    ChatCompletionChoice:
      properties:
        index:
          type: integer
          title: Index
        message:
          $ref: '#/components/schemas/ChatCompletionMessage'
        finish_reason:
          type: string
          title: Finish Reason
      type: object
      required:
        - index
        - message
        - finish_reason
      title: ChatCompletionChoice
    ChatCompletionUsage:
      properties:
        prompt_tokens:
          type: integer
          title: Prompt Tokens
        completion_tokens:
          type: integer
          title: Completion Tokens
        total_tokens:
          type: integer
          title: Total Tokens
      type: object
      required:
        - prompt_tokens
        - completion_tokens
        - total_tokens
      title: ChatCompletionUsage
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
        input:
          title: Input
        ctx:
          type: object
          title: Context
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
    ChatCompletionMessage:
      properties:
        role:
          type: string
          const: assistant
          title: Role
        content:
          type: string
          title: Content
        annotations:
          anyOf:
            - items:
                $ref: '#/components/schemas/FileCitation'
              type: array
            - type: 'null'
          title: Annotations
        sources:
          anyOf:
            - items:
                $ref: '#/components/schemas/CompletionSource'
              type: array
            - type: 'null'
          title: Sources
      additionalProperties: true
      type: object
      required:
        - role
        - content
      title: ChatCompletionMessage
    FileCitation:
      properties:
        type:
          type: string
          const: file_citation
          title: Type
          default: file_citation
        file_citation:
          $ref: '#/components/schemas/FileCitationBody'
        start_index:
          anyOf:
            - type: integer
            - type: 'null'
          title: Start Index
        end_index:
          anyOf:
            - type: integer
            - type: 'null'
          title: End Index
      type: object
      required:
        - file_citation
      title: FileCitation
    CompletionSource:
      properties:
        filename:
          anyOf:
            - type: string
            - type: 'null'
          title: Filename
        title:
          anyOf:
            - type: string
            - type: 'null'
          title: Title
        page:
          anyOf:
            - type: integer
            - type: 'null'
          title: Page
        snippet:
          anyOf:
            - type: string
            - type: 'null'
          title: Snippet
        cited:
          type: boolean
          title: Cited
          default: false
      type: object
      title: CompletionSource
    FileCitationBody:
      properties:
        filename:
          type: string
          title: Filename
      type: object
      required:
        - filename
      title: FileCitationBody
  securitySchemes:
    HTTPBearer:
      type: http
      description: API key or UI session JWT passed as a Bearer token.
      scheme: bearer

````

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