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

# OpenAI format

> Create chat completions compatible with the OpenAI Chat Completions protocol, supporting streaming and non-streaming responses

This endpoint is compatible with the OpenAI Chat Completions protocol for creating chat completion requests.

## Features

* Supports both streaming (SSE) and non-streaming response modes
* Supports multimodal input (text, images, etc.)
* Supports function calling and tool calling
* Supports structured output (JSON Schema)
* Supports reasoning effort configuration
* Supports web search options

## Use cases

Suitable for scenarios requiring interaction with OpenAI-compatible models, including:

* Single-turn or multi-turn conversations
* Function calling and tool orchestration
* Structured JSON output
* Real-time streaming responses

## Authentication

Include the `Authorization` header in the format `Bearer YOUR_API_KEY`.

<Warning>
  Never expose API keys in client-side code. Use a server-side proxy to forward requests.
</Warning>

## Quick example

<CodeGroup>
  ```python Python theme={null}
  import requests

  url = "https://api.haitoken.ai/v1/chat/completions"
  headers = {
      "Authorization": "Bearer YOUR_API_KEY",
      "Content-Type": "application/json"
  }
  data = {
      "model": "gpt-4o",
      "messages": [
          {"role": "user", "content": "Hello, please introduce yourself"}
      ]
  }

  response = requests.post(url, json=data, headers=headers)
  print(response.json())
  ```

  ```javascript Node.js theme={null}
  const response = await fetch('https://api.haitoken.ai/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'gpt-4o',
      messages: [
        { role: 'user', content: 'Hello, please introduce yourself' }
      ]
    })
  });

  const data = await response.json();
  console.log(data);
  ```

  ```curl cURL theme={null}
  curl -X POST 'https://api.haitoken.ai/v1/chat/completions' \
    -H 'Authorization: Bearer YOUR_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gpt-4o",
      "messages": [
        {"role": "user", "content": "Hello, please introduce yourself"}
      ]
    }'
  ```
</CodeGroup>


## OpenAPI

````yaml en/api-reference/chat/openai/openapi.json POST /v1/chat/completions
openapi: 3.0.1
info:
  title: Default Module
  description: ''
  version: 1.0.0
servers: []
security: []
tags: []
paths:
  /v1/chat/completions:
    post:
      tags: []
      summary: Create chat completion
      description: Supports both streaming (SSE) and non-streaming response modes
      parameters:
        - name: Authorization
          in: header
          description: ''
          required: true
          schema:
            type: string
            example: Bearer YOUR_API_KEY
        - name: Content-Type
          in: header
          description: ''
          schema:
            type: string
            example: application/json
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
              description: ''
            examples: {}
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletionResponse'
                description: OpenAI Chat Completions API response object
          headers: {}
      deprecated: false
      security: []
components:
  schemas:
    ChatCompletionRequest:
      type: object
      properties:
        stream_options:
          $ref: '#/components/schemas/StreamOptions'
          description: Streaming response options (only used when stream=true)
        top_p:
          type: number
          description: Nucleus sampling, between 0 and 1
        max_tokens:
          type: integer
          description: >-
            Maximum number of tokens to generate (deprecated, use
            max_completion_tokens instead)
        max_completion_tokens:
          type: integer
          description: >-
            Maximum number of tokens to generate (including visible output
            tokens and reasoning tokens)
        frequency_penalty:
          type: number
          description: Frequency penalty, -2.0 to 2.0
        presence_penalty:
          type: number
          description: Presence penalty, -2.0 to 2.0
        logit_bias:
          $ref: '#/components/schemas/MapInteger'
          description: Map to modify the likelihood of specified tokens appearing
        top_logprobs:
          type: integer
          description: >-
            Maximum number of most likely tokens to return at each token
            position (0-20)
        response_format:
          $ref: '#/components/schemas/ResponseFormat'
          description: Response format, supports "text", "json_object", "json_schema"
        prompt_cache_key:
          type: string
          description: Stable identifier for cache optimization
        prompt_cache_retention:
          type: string
          description: 'Cache retention policy: "in_memory" or "24h"'
        safety_identifier:
          type: string
          description: Safety identifier for detecting violating users
        tool_choice:
          type: object
          properties: {}
          description: >-
            Controls which tool the model calls, can be a string
            ("none"/"auto"/"required") or an object
        parallel_tool_calls:
          type: boolean
          description: Whether to enable parallel tool calls
        service_tier:
          type: string
          description: 'Service tier: "auto", "default", "flex", "scale", "priority"'
        reasoning_effort:
          type: string
          description: >-
            Reasoning effort level: "none", "minimal", "low", "medium", "high",
            "xhigh"
        web_search_options:
          type: object
          properties: {}
          description: Web search options
        function_call:
          type: object
          properties: {}
          description: 'Deprecated: function call control'
        model:
          type: string
          description: Model ID, e.g. gpt-4o, deepseek-chat, etc.
        messages:
          type: array
          items:
            $ref: '#/components/schemas/ChatMessage'
            description: Chat message object, compatible with OpenAI various role messages
          description: List of messages
        stream:
          type: boolean
          description: Whether to enable streaming output
        temperature:
          type: number
          description: >-
            Sampling temperature, between 0 and 2, higher values produce more
            random output
        stop:
          type: object
          properties: {}
          description: Stop sequences
        'n':
          type: integer
          description: Number of chat completion choices to generate for each input message
        logprobs:
          type: boolean
          description: Whether to return log probabilities of output tokens
        seed:
          type: integer
          description: Seed value for deterministic sampling
          format: int64
        user:
          type: string
          description: User identifier (deprecated, use prompt_cache_key instead)
        tools:
          type: array
          items:
            $ref: '#/components/schemas/ToolDefinition'
            description: Tool definition (used in request parameters)
          description: List of tools the model can call
        modalities:
          type: array
          items:
            type: string
          description: 'Output type list: ["text"] or ["text", "audio"]'
        audio:
          type: object
          properties: {}
          description: Audio output parameters
        metadata:
          $ref: '#/components/schemas/MapString'
          description: Metadata, up to 16 key-value pairs
        prediction:
          type: object
          properties: {}
          description: Predicted output content
        verbosity:
          type: string
          description: 'Response verbosity: "low", "medium", "high"'
        functions:
          type: array
          items:
            type: object
            properties: {}
          description: 'Deprecated: list of function definitions'
      required:
        - model
        - messages
    ChatCompletionResponse:
      type: object
      properties:
        system_fingerprint:
          type: string
          description: System fingerprint, representing backend configuration
        service_tier:
          type: string
          description: Actual service tier used
        id:
          type: string
          description: Unique identifier for the chat completion
        object:
          type: string
          description: Object type, always "chat.completion"
        created:
          type: integer
          description: Creation time (Unix timestamp)
          format: int64
        model:
          type: string
          description: Model ID used
        choices:
          type: array
          items:
            $ref: '#/components/schemas/ChatChoice'
            description: Chat completion choice
          description: List of chat completion choices
        usage:
          $ref: '#/components/schemas/Usage'
          description: Usage statistics
    StreamOptions:
      type: object
      properties:
        include_usage:
          type: boolean
          description: Whether to include usage statistics in the final chunk
    MapInteger:
      type: object
      properties:
        key:
          type: integer
    ResponseFormat:
      type: object
      properties:
        json_schema:
          $ref: '#/components/schemas/JsonSchema'
          description: JSON Schema configuration (only used when type is "json_schema")
        type:
          type: string
          description: 'Response format type: "text" | "json_object" | "json_schema"'
    ChatMessage:
      type: object
      properties:
        tool_calls:
          type: array
          items:
            $ref: '#/components/schemas/ToolCall'
            description: Tool calls generated by the model (e.g. function calls)
          description: List of tool calls (generated by the model)
        tool_call_id:
          type: string
          description: Tool call ID (only used for tool role messages)
        reasoning_content:
          type: string
          description: >-
            Chain-of-thought content (extended field for DeepSeek and other
            reasoning models)
        role:
          type: string
          description: 'Message role: system/developer/user/assistant/tool/function'
          enum:
            - SYSTEM
            - DEVELOPER
            - USER
            - ASSISTANT
            - TOOL
            - FUNCTION
        content:
          type: object
          properties: {}
          description: >-
            Message content, supports string or array of content parts
            (multimodal)
        name:
          type: string
          description: Message sender name (optional)
        refusal:
          type: string
          description: Refusal message (refusal reply generated by the model)
        annotations:
          type: array
          items:
            type: object
            properties: {}
          description: List of annotations (e.g. URL references during web search)
      required:
        - role
    ToolDefinition:
      type: object
      properties:
        type:
          type: string
          description: Tool type, currently only supports "function"
        function:
          $ref: '#/components/schemas/FunctionDefinition'
          description: Function definition details
    MapString:
      type: object
      properties:
        key:
          type: string
    ChatChoice:
      type: object
      properties:
        finish_reason:
          type: string
          description: >-
            Reason the model stopped generating: "stop", "length", "tool_calls",
            "content_filter", "function_call"
        index:
          type: integer
          description: Index of the choice in the list
        message:
          $ref: '#/components/schemas/ChatMessage'
          description: Message generated by the model
        logprobs:
          $ref: '#/components/schemas/LogProbs'
          description: Log probability information
    Usage:
      type: object
      properties:
        prompt_tokens:
          type: integer
          description: Number of prompt tokens
        completion_tokens:
          type: integer
          description: Number of completion tokens
        total_tokens:
          type: integer
          description: Total number of tokens
        prompt_tokens_details:
          $ref: '#/components/schemas/PromptTokensDetails'
          description: Prompt token details
        completion_tokens_details:
          $ref: '#/components/schemas/CompletionTokensDetails'
          description: Completion token details
        ephemeral_5m_input_tokens:
          type: integer
          description: Number of input tokens used to create 5-minute cache entries
        ephemeral_1h_input_tokens:
          type: integer
          description: Number of input tokens used to create 1-hour cache entries
        web_search_requests:
          type: integer
          description: Number of web search tool requests
    JsonSchema:
      type: object
      properties:
        name:
          type: string
          description: Response format name
        description:
          type: string
          description: Description
        schema:
          type: object
          properties: {}
          description: JSON Schema object
        strict:
          type: boolean
          description: Whether to enable strict schema adherence
    ToolCall:
      type: object
      properties:
        id:
          type: string
          description: Unique identifier for the tool call
        index:
          type: integer
          description: Index of the tool call
        type:
          type: string
          description: Tool type, currently only supports "function"
        function:
          $ref: '#/components/schemas/FunctionCall'
          description: Function call details
    FunctionDefinition:
      type: object
      properties:
        name:
          type: string
          description: >-
            Function name, must contain only a-z, A-Z, 0-9, underscores and
            hyphens, max length 64
        description:
          type: string
          description: >-
            Function description, the model uses this to decide when and how to
            call the function
        parameters:
          type: object
          properties: {}
          description: Function parameters, a JSON Schema object
        strict:
          type: boolean
          description: Whether to enable strict schema adherence
    LogProbs:
      type: object
      properties:
        content:
          type: array
          items:
            $ref: '#/components/schemas/TokenLogprob'
            description: Log probability information for a single token
          description: List of log probability information for message content tokens
        refusal:
          type: array
          items:
            $ref: '#/components/schemas/TokenLogprob'
            description: Log probability information for a single token
          description: List of log probability information for refusal message tokens
    PromptTokensDetails:
      type: object
      properties:
        cached_tokens:
          type: integer
          description: Number of cached tokens
        audio_tokens:
          type: integer
          description: Number of audio tokens
    CompletionTokensDetails:
      type: object
      properties:
        reasoning_tokens:
          type: integer
          description: Number of reasoning tokens
        audio_tokens:
          type: integer
          description: Number of audio tokens
        accepted_prediction_tokens:
          type: integer
          description: >-
            Number of tokens from predicted output that appeared in the
            completion
        rejected_prediction_tokens:
          type: integer
          description: >-
            Number of tokens from predicted output that did not appear in the
            completion
    FunctionCall:
      type: object
      properties:
        name:
          type: string
          description: Function name
        arguments:
          type: string
          description: Function arguments (JSON format string)
    TokenLogprob:
      type: object
      properties:
        top_logprobs:
          type: array
          items:
            $ref: '#/components/schemas/TopLogprob'
            description: Most likely tokens and their log probabilities
          description: List of most likely tokens and their log probabilities
        token:
          type: string
          description: Token text
        logprob:
          type: number
          description: Log probability of the token
        bytes:
          type: array
          items:
            type: integer
          description: UTF-8 byte representation of the token
    TopLogprob:
      type: object
      properties:
        token:
          type: string
          description: Token text
        logprob:
          type: number
          description: Log probability of the token
        bytes:
          type: array
          items:
            type: integer
          description: UTF-8 byte representation of the token

````