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

# Chat Completions

> Generate a model response for a given chat conversation. Supports streaming and non-streaming responses.

This API endpoint allows you to send text and images as inputs, and the model will generate the next message in the conversation.

## Create Chat Completion

To create a chat completion, use the following endpoint:

`POST /v1/chat/completions`

### Request Body

<ParamField body="model" type="string" required>
  The model id to use for completion ("gpt-4o", "gpt-4o-mini", "deepseek-r",
  [...](/getting-started/models))
</ParamField>

<ParamField body="messages" type="array" required>
  An array of message objects that form the conversation
</ParamField>

<ParamField body="max_tokens" type="integer">
  The maximum number of tokens to generate
</ParamField>

<ParamField body="temperature" type="string">
  Sampling temperature, a value between 0 and 2.
</ParamField>

<ParamField body="stream" type="boolean">
  Whether to enable streaming responses
</ParamField>

<ParamField body="frequency_penalty" type="integer">
  Tells the model not to repeat a word that has already been used multiple times
  in the conversation.
</ParamField>

<ParamField body="presence_penalty" type="integer">
  Prevents the model from repeating a word, even if it's only been used once
</ParamField>

<ParamField body="stop" type="array">
  Up to 4 sequences where the API will stop generating further tokens. The
  returned text will not contain the stop sequence.
</ParamField>

<RequestExample>
  ```python Python theme={null}
  import os
  from openai import OpenAI

  client = OpenAI(
      base_url="https://api.routeway.ai/v1",
      api_key=os.getenv("Routeway_API_KEY")
  )

  response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[
          {"role": "user", "content": "Write a short story about a robot and a cat."}
      ]
  )

  print(response.choices[0].message.content)
  ```

  ```javascript Node.js theme={null}
  const axios = require('axios')

  const apiKey = 'your_routeway_api_key'
  const url = 'https://api.routeway.ai/v1/chat/completions'

  const data = {
  	model: 'gpt-4o',
  	messages: [
  		{
  			role: 'user',
  			content: 'Say this is a test!',
  		},
  	],
  }

  const headers = {
  	'Content-Type': 'application/json',
  	Authorization: `Bearer ${apiKey}`,
  }

  axios
  	.post(url, data, { headers })
  	.then(response => {
  		console.log(response.data)
  	})
  	.catch(error => {
  		console.error('Error:', error)
  	})
  ```

  ```bash cURL theme={null}
  curl https://api.routeway.ai/v1/chat/completions \
    -H "Authorization: Bearer $Routeway_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4o-mini",
      "messages": [
        {"role": "user", "content": "Write a short story about a robot and a cat"}
      ]
    }'
  ```
</RequestExample>


## OpenAPI

````yaml POST /v1/chat/completions
openapi: 3.1.0
info:
  title: Routeway API
  version: 1.0.0
servers:
  - url: https://api.routeway.ai
security:
  - HTTPBearer: []
paths:
  /v1/chat/completions:
    post:
      tags:
        - Chat
      summary: Create Chat Completion (v1)
      description: >-
        Generate a model response for a given chat conversation. Supports
        streaming and non-streaming responses.
      operationId: chat_completions_v1_chat_completions_post
      parameters:
        - name: x-api-key
          in: header
          required: false
          schema:
            type: string
            title: X-Api-Key
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletions'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletionResponse'
        '400':
          description: Bad Request - Invalid query parameters or body format.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          description: Unauthorized - Missing or invalid API key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '403':
          description: Forbidden - Key does not have permission or is disabled.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '422':
          description: Unprocessable Entity - Request body validation failed.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '429':
          description: Too Many Requests - Rate limit exceeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          description: Internal Server Error - Internal error occurred.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '502':
          description: Bad Gateway - Provider error occurred.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    ChatCompletions:
      properties:
        model:
          type: string
          title: Model
        messages:
          items:
            $ref: '#/components/schemas/ChatMessage'
          type: array
          minItems: 1
          title: Messages
        tools:
          anyOf:
            - items:
                $ref: '#/components/schemas/Tool-Input'
              type: array
            - type: 'null'
          title: Tools
        tool_choice:
          anyOf:
            - type: string
            - additionalProperties: true
              type: object
          title: Tool Choice
        functions:
          items: {}
          type: array
          title: Functions
        function_call:
          anyOf:
            - type: string
            - additionalProperties: true
              type: object
          title: Function Call
        response_format:
          anyOf:
            - $ref: '#/components/schemas/schemas__openai__TextResponseFormat'
            - $ref: '#/components/schemas/JsonObjectResponseFormat-Input'
            - $ref: '#/components/schemas/JsonSchemaResponseFormat-Input'
            - type: 'null'
          title: Response Format
        temperature:
          anyOf:
            - type: number
              maximum: 2
              minimum: 0
            - type: 'null'
          title: Temperature
        top_p:
          anyOf:
            - type: number
              maximum: 1
              minimum: 0
            - type: 'null'
          title: Top P
        stream:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Stream
          default: false
        stream_options:
          anyOf:
            - $ref: '#/components/schemas/StreamOptions'
            - type: 'null'
        stop:
          anyOf:
            - type: string
            - items:
                type: string
              type: array
            - type: 'null'
          title: Stop
        max_tokens:
          anyOf:
            - type: integer
              minimum: 1
            - type: 'null'
          title: Max Tokens
        max_completion_tokens:
          anyOf:
            - type: integer
              minimum: 1
            - type: 'null'
          title: Max Completion Tokens
        presence_penalty:
          anyOf:
            - type: number
              maximum: 2
              minimum: -2
            - type: 'null'
          title: Presence Penalty
        frequency_penalty:
          anyOf:
            - type: number
              maximum: 2
              minimum: -2
            - type: 'null'
          title: Frequency Penalty
        logit_bias:
          additionalProperties: true
          type: object
          title: Logit Bias
        reasoning_effort:
          anyOf:
            - $ref: '#/components/schemas/ReasoningEffort'
            - type: 'null'
      type: object
      required:
        - model
        - messages
      title: ChatCompletions
    ChatCompletionResponse:
      properties:
        id:
          type: string
          title: Id
          description: A unique identifier for the chat completion.
        object:
          type: string
          const: chat.completion
          title: Object
          description: The object type, which is always chat.completion.
          default: chat.completion
        created:
          type: integer
          title: Created
          description: >-
            The Unix timestamp (in seconds) of when the chat completion was
            created.
        model:
          type: string
          title: Model
          description: The model used for the chat completion.
        choices:
          items:
            $ref: '#/components/schemas/ChatCompletionChoice'
          type: array
          title: Choices
          description: A list of chat completion choices.
        usage:
          $ref: '#/components/schemas/ChatCompletionUsage'
          description: Usage statistics for the completion request.
        system_fingerprint:
          anyOf:
            - type: string
            - type: 'null'
          title: System Fingerprint
          description: >-
            This fingerprint represents the backend configuration that the model
            runs with.
      type: object
      required:
        - id
        - created
        - model
        - choices
        - usage
      title: ChatCompletionResponse
    ErrorResponse:
      properties:
        error:
          $ref: '#/components/schemas/ErrorDetail'
      type: object
      required:
        - error
      title: ErrorResponse
    ChatMessage:
      properties:
        role:
          type: string
          title: Role
        content:
          anyOf:
            - type: string
            - items:
                anyOf:
                  - $ref: '#/components/schemas/TextContent-Input'
                  - $ref: '#/components/schemas/ImageContent'
                  - $ref: '#/components/schemas/FileContent'
              type: array
            - type: 'null'
          title: Content
        name:
          type: string
          title: Name
        tool_call_id:
          type: string
          title: Tool Call Id
        tool_calls:
          items: {}
          type: array
          title: Tool Calls
        function_call:
          additionalProperties: true
          type: object
          title: Function Call
      type: object
      required:
        - role
      title: ChatMessage
    Tool-Input:
      properties:
        type:
          type: string
          const: function
          title: Type
        function:
          $ref: '#/components/schemas/FunctionDefinition'
      type: object
      required:
        - type
        - function
      title: Tool
    schemas__openai__TextResponseFormat:
      properties:
        type:
          type: string
          const: text
          title: Type
      type: object
      required:
        - type
      title: TextResponseFormat
    JsonObjectResponseFormat-Input:
      properties:
        type:
          type: string
          const: json_object
          title: Type
      type: object
      required:
        - type
      title: JsonObjectResponseFormat
    JsonSchemaResponseFormat-Input:
      properties:
        type:
          type: string
          const: json_schema
          title: Type
        json_schema:
          $ref: '#/components/schemas/JsonSchemaConfig'
      type: object
      required:
        - type
        - json_schema
      title: JsonSchemaResponseFormat
    StreamOptions:
      properties:
        include_usage:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Include Usage
      type: object
      title: StreamOptions
    ReasoningEffort:
      type: string
      enum:
        - none
        - minimal
        - low
        - medium
        - high
        - xhigh
      title: ReasoningEffort
    ChatCompletionChoice:
      properties:
        index:
          type: integer
          title: Index
          description: The index of the choice in the list of choices.
        message:
          $ref: '#/components/schemas/ChatCompletionMessage'
          description: A chat completion message generated by the model.
        finish_reason:
          type: string
          title: Finish Reason
          description: The reason the model stopped generating tokens.
        logprobs:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Logprobs
          description: Log probability information for the choice, if requested.
      type: object
      required:
        - index
        - message
        - finish_reason
      title: ChatCompletionChoice
    ChatCompletionUsage:
      properties:
        prompt_tokens:
          type: integer
          title: Prompt Tokens
          description: Number of tokens in the prompt.
        completion_tokens:
          type: integer
          title: Completion Tokens
          description: Number of tokens in the generated completion.
        total_tokens:
          type: integer
          title: Total Tokens
          description: Total number of tokens used in the request (prompt + completion).
      type: object
      required:
        - prompt_tokens
        - completion_tokens
        - total_tokens
      title: ChatCompletionUsage
    ErrorDetail:
      properties:
        message:
          type: string
          title: Message
          description: A human-readable description of the error.
        type:
          type: string
          title: Type
          description: >-
            The type of error returned (e.g. invalid_request_error,
            rate_limit_error).
        param:
          anyOf:
            - type: string
            - type: 'null'
          title: Param
          description: The parameter that caused the error, if applicable.
        code:
          anyOf:
            - type: string
            - type: 'null'
          title: Code
          description: >-
            A short code identifier for the error type (e.g. invalid_request,
            rate_limit).
        status_code:
          type: integer
          title: Status Code
          description: The HTTP status code.
        id:
          type: string
          title: Id
          description: Unique error or trace ID associated with this request.
        tip:
          anyOf:
            - type: string
            - type: 'null'
          title: Tip
          description: An optional troubleshooting tip or resolution suggestion.
        docs:
          anyOf:
            - type: string
            - type: 'null'
          title: Docs
          description: Link to the documentation related to this error.
          default: https://docs.routeway.ai
        support:
          anyOf:
            - type: string
            - type: 'null'
          title: Support
          description: Link to support for further help.
          default: https://discord.gg/RjX2CpdPpd
        community:
          anyOf:
            - additionalProperties:
                type: string
              type: object
            - type: 'null'
          title: Community
          description: Links to community resources like Discord and Twitter.
      type: object
      required:
        - message
        - type
        - status_code
        - id
      title: ErrorDetail
    TextContent-Input:
      properties:
        type:
          type: string
          title: Type
        text:
          type: string
          title: Text
      type: object
      required:
        - type
        - text
      title: TextContent
    ImageContent:
      properties:
        type:
          type: string
          title: Type
        image_url:
          $ref: '#/components/schemas/ImageUrl'
      type: object
      required:
        - type
        - image_url
      title: ImageContent
    FileContent:
      properties:
        type:
          type: string
          const: file
          title: Type
        file:
          $ref: '#/components/schemas/FilePayload'
      type: object
      required:
        - type
        - file
      title: FileContent
    FunctionDefinition:
      properties:
        name:
          type: string
          title: Name
        description:
          anyOf:
            - type: string
            - type: 'null'
          title: Description
        parameters:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Parameters
        strict:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Strict
      type: object
      required:
        - name
      title: FunctionDefinition
    JsonSchemaConfig:
      properties:
        name:
          type: string
          title: Name
        description:
          anyOf:
            - type: string
            - type: 'null'
          title: Description
        schema:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Schema
        strict:
          anyOf:
            - type: boolean
            - type: 'null'
          title: Strict
      type: object
      required:
        - name
      title: JsonSchemaConfig
    ChatCompletionMessage:
      properties:
        role:
          type: string
          title: Role
          description: The role of the author of this message.
        content:
          anyOf:
            - type: string
            - type: 'null'
          title: Content
          description: The contents of the message.
        refusal:
          anyOf:
            - type: string
            - type: 'null'
          title: Refusal
          description: The refusal message generated by the model, if any.
        tool_calls:
          anyOf:
            - items: {}
              type: array
            - type: 'null'
          title: Tool Calls
          description: The tool calls generated by the model, if any.
      type: object
      required:
        - role
      title: ChatCompletionMessage
    ImageUrl:
      properties:
        url:
          type: string
          minLength: 1
          format: uri
          title: Url
        detail:
          type: string
          enum:
            - low
            - high
            - auto
          title: Detail
          default: low
      type: object
      required:
        - url
      title: ImageUrl
    FilePayload:
      properties:
        filename:
          type: string
          title: Filename
        file_data:
          type: string
          minLength: 1
          format: uri
          title: File Data
      type: object
      required:
        - filename
        - file_data
      title: FilePayload
  securitySchemes:
    HTTPBearer:
      type: http
      scheme: bearer

````