# Request Format

URL: https://docs.unli.dev/request-format

> Complete API reference for request formats, including OpenAI Chat Completions and Anthropic Messages endpoints, parameters, and examples.

UNLI supports both **OpenAI Chat Completions** and **Anthropic Messages** API formats. Choose the format that best fits your workflow or existing SDK integrations.

***

## OpenAI Format (`/v1/chat/completions`)

The chat completions endpoint is compatible with the OpenAI Chat Completions API format.

### Endpoint

```
POST https://api.unli.dev/v1/chat/completions
```

### Authentication

Include your API key in the `Authorization` header:

```bash
Authorization: Bearer YOUR_API_KEY
```

### Example Request

JavaScript:

```javascript
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://api.unli.dev/v1",
  apiKey: "YOUR_API_KEY",
});

const completion = await openai.chat.completions.create({
  messages: [{ role: "user", content: "Hello, AI!" }],
  model: "auto",
});

console.log(completion.choices[0].message.content);
```

Python:

```python
import openai

client = openai.OpenAI(
  api_key="YOUR_API_KEY",
  base_url="https://api.unli.dev/v1"
)

response = client.chat.completions.create(
  model="auto",
  messages=[{"role": "user", "content": "Hello, AI!"}]
)

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

cURL:

```bash
curl -X POST https://api.unli.dev/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "auto",
    "messages": [{"role": "user", "content": "Hello, AI!"}],
    "stream": false
  }'
```

### Payload Examples

#### Basic Text Completion

```json
{
  "model": "auto",
  "messages": [
    {
      "role": "user",
      "content": "Hello, how are you?"
    }
  ]
}
```

#### Text with System Message

```json
{
  "model": "auto",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant that speaks like a pirate."
    },
    {
      "role": "user",
      "content": "Tell me about the weather"
    }
  ]
}
```

#### Image Analysis

```json
{
  "model": "auto",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": {
            "url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD..."
          }
        },
        {
          "type": "text",
          "text": "What's in this image?"
        }
      ]
    }
  ]
}
```

### Parameters

| Parameter     | Type    | Required | Default | Description                                                              |
| ------------- | ------- | -------- | ------- | ------------------------------------------------------------------------ |
| `model`       | string  | Yes      | `auto`  | Model to use for completion. Use `auto` for automatic routing            |
| `messages`    | array   | Yes      | -       | Array of message objects representing the conversation                   |
| `stream`      | boolean | No       | `false` | Enable streaming responses                                               |
| `temperature` | number  | No       | `1.0`   | Temperature for sampling (0 to 2). Higher values make output more random |

***

## Anthropic Messages Format (`/v1/messages`)

The messages endpoint is compatible with the **Anthropic Claude Messages API** format.

### Endpoint

```
POST https://api.unli.dev/v1/messages
```

### Authentication & Headers

Pass your API key using the `x-api-key` header (or `Authorization: Bearer`) and specify the `anthropic-version`:

```bash
x-api-key: YOUR_API_KEY
anthropic-version: 2023-06-01
Content-Type: application/json
```

### Example Request

JavaScript:

```javascript
import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
  baseURL: "https://api.unli.dev",
  apiKey: "YOUR_API_KEY",
});

const message = await anthropic.messages.create({
  model: "unli-auto",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "Hello, Claude!" }
  ],
});

console.log(message.content[0].text);
```

Python:

```python
import anthropic

client = anthropic.Anthropic(
  base_url="https://api.unli.dev",
  api_key="YOUR_API_KEY",
)

message = client.messages.create(
  model="unli-auto",
  max_tokens=1024,
  messages=[
    {"role": "user", "content": "Hello, Claude!"}
  ]
)

print(message.content[0].text)
```

cURL:

```bash
curl -X POST https://api.unli.dev/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "unli-auto",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello, Claude!"}
    ]
  }'
```

### Payload Examples

#### Basic Text Message

```json
{
  "model": "unli-auto",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "Hello, how can you help me today?"
    }
  ]
}
```

#### Message with Top-level System Prompt

In Anthropic's format, the `system` parameter is a top-level property rather than a message role:

```json
{
  "model": "unli-auto",
  "max_tokens": 1024,
  "system": "You are an expert full-stack software engineer.",
  "messages": [
    {
      "role": "user",
      "content": "How do I optimize database queries in PostgreSQL?"
    }
  ]
}
```

#### Multi-turn Conversation

```json
{
  "model": "unli-auto",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "What is the capital of France?"
    },
    {
      "role": "assistant",
      "content": "The capital of France is Paris."
    },
    {
      "role": "user",
      "content": "What is its population?"
    }
  ]
}
```

#### Image Analysis (Vision)

Pass image data inside `content` blocks with base64 encoding:

```json
{
  "model": "unli-auto",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "image",
          "source": {
            "type": "base64",
            "media_type": "image/jpeg",
            "data": "/9j/4AAQSkZJRgABAQAAAQABAAD..."
          }
        },
        {
          "type": "text",
          "text": "Describe the contents of this image in detail."
        }
      ]
    }
  ]
}
```

#### Tool Calling / Function Calling

```json
{
  "model": "unli-auto",
  "max_tokens": 1024,
  "tools": [
    {
      "name": "get_weather",
      "description": "Get the current weather in a given location",
      "input_schema": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"]
          }
        },
        "required": ["location"]
      }
    }
  ],
  "messages": [
    {
      "role": "user",
      "content": "What is the weather in Jakarta?"
    }
  ]
}
```

### Parameters

| Parameter        | Type            | Required | Default     | Description                                                     |
| ---------------- | --------------- | -------- | ----------- | --------------------------------------------------------------- |
| `model`          | string          | Yes      | `unli-auto` | Model identifier to use. Use `unli-auto` for automatic routing  |
| `messages`       | array           | Yes      | -           | Array of conversational input messages (`user` and `assistant`) |
| `max_tokens`     | integer         | Yes      | -           | The maximum number of tokens to generate before stopping        |
| `system`         | string \| array | No       | -           | System prompt specifying context and instructions               |
| `stream`         | boolean         | No       | `false`     | Enable incremental streaming via Server-Sent Events             |
| `temperature`    | number          | No       | `1.0`       | Randomness amount (0.0 to 1.0)                                  |
| `top_p`          | number          | No       | -           | Nucleus sampling probability cutoff (0.0 to 1.0)                |
| `top_k`          | integer         | No       | -           | Sample from the top K options for each subsequent token         |
| `stop_sequences` | string\[]       | No       | -           | Custom stop sequences                                           |
| `tools`          | array           | No       | -           | Definitions of tools the model may use                          |
| `tool_choice`    | object          | No       | -           | Tool choice specification (`auto`, `any`, `tool`)               |
| `metadata`       | object          | No       | -           | Metadata object such as `user_id`                               |

***

## Supported Image Formats

| Format | Media Type   | Max Size | Notes                       |
| ------ | ------------ | -------- | --------------------------- |
| JPEG   | `image/jpeg` | 20MB     | Recommended for photos      |
| PNG    | `image/png`  | 20MB     | Good for screenshots and UI |
| GIF    | `image/gif`  | 20MB     | Static images               |
| WebP   | `image/webp` | 20MB     | Modern web format           |
