# Create a chat completion

`POST https://ai.inovacc.dev/v1/chat/completions`

Generate the next message of a conversation, in the OpenAI chat-completions format.

## Body params

- `messages` (array), required: The conversation: objects with a `role` (`system`, `user`, `assistant` or `tool`) and a `content`, either a string or an array of `text` and `image_url` parts. At most 20 images.
- `model` (string): A route id from `GET /v1/models`. `auto`, or no value, uses your organization's default route.
- `stream` (boolean): `true` sends the reply as server-sent events, ending with `data: [DONE]`.
- `stream_options` (object): With `{"include_usage": true}`, a stream ends with a chunk that carries the token counts.
- `max_tokens` (integer): The most output tokens you want. It can only lower the ceiling your route and organization set.
- `max_completion_tokens` (integer): Same as `max_tokens`, under the newer name.
- `temperature` (number): Sampling temperature, passed to the model.
- `top_p` (number): Nucleus sampling, passed to the model.
- `stop` (string | array): Sequences that end the reply.
- `presence_penalty` (number): Penalty for repeating topics, passed to the model.
- `frequency_penalty` (number): Penalty for repeating tokens, passed to the model.
- `logit_bias` (object): Token biases, passed to the model.
- `logprobs` (boolean): Ask for log probabilities of the output tokens.
- `top_logprobs` (integer): How many alternatives per token to return with `logprobs`.
- `tools` (array): Functions the model may call. Some routes take a narrower set of fields and refuse it (`unsupported_field_for_route`).
- `tool_choice` (string | object): Whether, and which, tool the model must call.
- `parallel_tool_calls` (boolean): Whether the model may call several tools at once.
- `response_format` (object): The output format, for example `{"type": "json_object"}`.
- `seed` (integer): A seed for more repeatable sampling.
- `reasoning_effort` (string): How much the model should reason, on models that do.
- `verbosity` (string): How long the model should answer, on models that support it.
- `user` (string): Your end user's id. Replaced, before it leaves Inovacc, by a value derived from your organization.
- `safety_identifier` (string): A stable id of your end user for safety purposes; derived from your organization like `user`.
- `prompt_cache_key` (string): A key that groups requests for caching; derived from your organization like `user`.
- `prediction` (object): Predicted output content, passed to the model.
- `n` (integer): Accepted only when your organization has been granted this field (`403 field_not_allowed` otherwise).
- `service_tier` (string): Accepted only when your organization has been granted this field (`403 field_not_allowed` otherwise).
- `store` (boolean): Accepted only when your organization has been granted this field (`403 field_not_allowed` otherwise).
- `metadata` (object): Accepted only when your organization has been granted this field (`403 field_not_allowed` otherwise).
- `modalities` (array): Accepted only when your organization has been granted this field (`403 field_not_allowed` otherwise).
- `audio` (object): Accepted only when your organization has been granted this field (`403 field_not_allowed` otherwise).
- `web_search_options` (object): Accepted only when your organization has been granted this field (`403 field_not_allowed` otherwise).

## Headers

- `Authorization` (string), required: Bearer <API key>
- `X-Operation-Id` (string), required: a unique id you choose, on every call except GET
- `Content-Type` (string), required: application/json, on a call with a body

## Request

**cURL**

```sh
curl -X POST "https://ai.inovacc.dev/v1/chat/completions" \
  -H "Authorization: Bearer $INOVACC_API_KEY" \
  -H "X-Operation-Id: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "r00",
  "messages": [
    {
      "role": "user",
      "content": "hello"
    }
  ]
}'
```

**TypeScript**

```ts
import crypto from "node:crypto";

const body: Record<string, unknown> = {
  "model": "r00",
  "messages": [
    {
      "role": "user",
      "content": "hello"
    }
  ]
};

const url = "https://ai.inovacc.dev/v1/chat/completions";

const response = await fetch(url, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.INOVACC_API_KEY}`,
    "X-Operation-Id": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
});

console.log(response.status, await response.text());
```

**Python**

```py
import json
import os
import urllib.request
import uuid

body = {
    "model": "r00",
    "messages": [
        {
            "role": "user",
            "content": "hello",
        },
    ],
}

request = urllib.request.Request(
    "https://ai.inovacc.dev/v1/chat/completions",
    data=json.dumps(body).encode(),
    method="POST",
    headers={
        "User-Agent": "inovacc-python-sample",
        "Authorization": "Bearer " + os.environ["INOVACC_API_KEY"],
        "X-Operation-Id": str(uuid.uuid4()),
        "Content-Type": "application/json",
    },
)

with urllib.request.urlopen(request) as response:
    print(response.status, response.read().decode())
```

**Go**

```go
package main

import (
	"crypto/rand"
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
)

const url = "https://ai.inovacc.dev/v1/chat/completions"

const body = `{
  "model": "r00",
  "messages": [
    {
      "role": "user",
      "content": "hello"
    }
  ]
}`

// newUUID returns a random (version 4) UUID, from the standard library alone.
func newUUID() string {
	b := make([]byte, 16)
	if _, err := rand.Read(b); err != nil {
		panic(err)
	}
	b[6] = b[6]&0x0f | 0x40
	b[8] = b[8]&0x3f | 0x80
	return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}

func main() {
	req, err := http.NewRequest("POST", url, strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Authorization", "Bearer "+os.Getenv("INOVACC_API_KEY"))
	req.Header.Set("X-Operation-Id", newUUID())
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	out, err := io.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(res.Status, string(out))
}
```

**Rust**

```rs
// Cargo.toml: reqwest = { version = "0.12", features = ["blocking", "json"] }
// Cargo.toml: serde_json = "1"
// Cargo.toml: uuid = { version = "1", features = ["v4"] }

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = std::env::var("INOVACC_API_KEY")?;
    let body = serde_json::json!({
        "model": "r00",
        "messages": [
            {
                "role": "user",
                "content": "hello"
            }
        ]
    });
    let response = reqwest::blocking::Client::new()
        .post("https://ai.inovacc.dev/v1/chat/completions")
        .bearer_auth(api_key)
        .header("X-Operation-Id", uuid::Uuid::new_v4().to_string())
        .json(&body)
        .send()?;
    println!("{} {}", response.status(), response.text()?);
    Ok(())
}
```

**JavaScript**

```mjs
import crypto from "node:crypto";

const body = {
  "model": "r00",
  "messages": [
    {
      "role": "user",
      "content": "hello"
    }
  ]
};

const url = "https://ai.inovacc.dev/v1/chat/completions";

const response = await fetch(url, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.INOVACC_API_KEY}`,
    "X-Operation-Id": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
});

console.log(response.status, await response.text());
```

**PHP**

```php
<?php

$body = <<<'JSON'
{
  "model": "r00",
  "messages": [
    {
      "role": "user",
      "content": "hello"
    }
  ]
}
JSON;

function uuid4(): string
{
    $bytes = random_bytes(16);
    $bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40);
    $bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80);
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
}

$curl = curl_init('https://ai.inovacc.dev/v1/chat/completions');
curl_setopt_array($curl, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('INOVACC_API_KEY'),
        'X-Operation-Id: ' . uuid4(),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => $body,
]);

$response = curl_exec($curl);
echo curl_getinfo($curl, CURLINFO_HTTP_CODE), ' ', $response, "\n";
curl_close($curl);
```

**Ruby**

```rb
require "net/http"
require "securerandom"
require "uri"

uri = URI('https://ai.inovacc.dev/v1/chat/completions')
body = <<~'JSON'
{
  "model": "r00",
  "messages": [
    {
      "role": "user",
      "content": "hello"
    }
  ]
}
JSON

request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer #{ENV.fetch('INOVACC_API_KEY')}"
request["X-Operation-Id"] = SecureRandom.uuid
request["Content-Type"] = "application/json"
request.body = body

response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
  http.request(request)
end

puts "#{response.code} #{response.body}"
```

**Java**

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;

public class Main {
    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("INOVACC_API_KEY");
        String body = "{" +
                "\"model\": \"r00\"," +
                "\"messages\": [" +
                "{" +
                "\"role\": \"user\"," +
                "\"content\": \"hello\"" +
                "}" +
                "]" +
                "}";

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://ai.inovacc.dev/v1/chat/completions"))
                .header("Authorization", "Bearer " + apiKey)
                .header("X-Operation-Id", UUID.randomUUID().toString())
                .header("Content-Type", "application/json")
                .method("POST", HttpRequest.BodyPublishers.ofString(body))
                .build();

        HttpResponse<String> response = HttpClient.newHttpClient()
                .send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.statusCode() + " " + response.body());
    }
}
```

**C#**

```cs
using System.Text;

var body = "{" +
    "\"model\": \"r00\"," +
    "\"messages\": [" +
    "{" +
    "\"role\": \"user\"," +
    "\"content\": \"hello\"" +
    "}" +
    "]" +
    "}";

var url = "https://ai.inovacc.dev/v1/chat/completions";
var apiKey = Environment.GetEnvironmentVariable("INOVACC_API_KEY");

using var client = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("Authorization", $"Bearer {apiKey}");
request.Headers.Add("X-Operation-Id", Guid.NewGuid().ToString());
request.Content = new StringContent(body, Encoding.UTF8, "application/json");

using var response = await client.SendAsync(request);
Console.WriteLine($"{(int)response.StatusCode} {await response.Content.ReadAsStringAsync()}");
```

**Kotlin**

```kt
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.util.UUID

fun main() {
    val apiKey = System.getenv("INOVACC_API_KEY")
    val body = "{" +
        "\"model\": \"r00\"," +
        "\"messages\": [" +
        "{" +
        "\"role\": \"user\"," +
        "\"content\": \"hello\"" +
        "}" +
        "]" +
        "}"

    val request = HttpRequest.newBuilder()
        .uri(URI.create("https://ai.inovacc.dev/v1/chat/completions"))
        .header("Authorization", "Bearer " + apiKey)
        .header("X-Operation-Id", UUID.randomUUID().toString())
        .header("Content-Type", "application/json")
        .method("POST", HttpRequest.BodyPublishers.ofString(body))
        .build()

    val response = HttpClient.newHttpClient()
        .send(request, HttpResponse.BodyHandlers.ofString())
    println("${response.statusCode()} ${response.body()}")
}
```

**Swift**

```swift
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

let body = #"""
{
  "model": "r00",
  "messages": [
    {
      "role": "user",
      "content": "hello"
    }
  ]
}
"""#

let apiKey = ProcessInfo.processInfo.environment["INOVACC_API_KEY"] ?? ""

let url = URL(string: "https://ai.inovacc.dev/v1/chat/completions")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.setValue(UUID().uuidString, forHTTPHeaderField: "X-Operation-Id")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = Data(body.utf8)

let (data, response) = try await URLSession.shared.data(for: request)
let status = (response as? HTTPURLResponse)?.statusCode ?? 0
print(status, String(decoding: data, as: UTF8.self))
```

## Response

**200**

```json
{
  "id": "chatcmpl-client_op_00000001",
  "object": "chat.completion",
  "created": 1790000000,
  "model": "r00",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "hi there"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 5,
    "total_tokens": 15
  }
}
```

**400**: missing_operation_id, invalid_operation_id, invalid_body, invalid_messages, unsupported_field, unsupported_field_for_route, model_not_allowed, input_too_large

**401**: missing_credentials, invalid_credentials

**403**: key_disabled, organization_disabled, field_not_allowed, route_forbidden

**409**: operation_in_progress

**413**: request_too_large

**429**: rate_limited, quota_exceeded, concurrency_limited, budget_exhausted

**502**: upstream_error

**503**: service_unavailable, route_unavailable

**504**: timeout_error

## See also

- [Authentication](https://developer.inovacc.dev/en/guides/authentication/)
- [Errors](https://developer.inovacc.dev/en/guides/errors/)
- [Limits](https://developer.inovacc.dev/en/guides/limits/)
