> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.mixlayer.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.mixlayer.com/_mcp/server.

# Client Libraries

> Use Mixlayer from Python, TypeScript, Rust, or curl with any OpenAI-compatible client

Mixlayer exposes OpenAI-compatible Chat Completions and Responses APIs at `https://mixlayer.ai/v1`. Any client library that supports either API can use Mixlayer — point it at the Mixlayer base URL and pass your Mixlayer API key.

You can [create an API key](https://console.mixlayer.com/app/api-keys) from the Mixlayer console.

## Installation

#### curl

No installation required — `curl` is preinstalled on most systems.

#### Python

```bash
pip install openai
```

#### TypeScript

```bash
npm install openai
```

#### Rust

**`file=`**

```toml file=Cargo.toml
[dependencies]
async-openai = "0.27"
tokio = { version = "1", features = ["full"] }
```

## Basic chat completion

#### curl

**`file=`**

```bash file=chat.sh
curl https://mixlayer.ai/v1/chat/completions \
  -H "Authorization: Bearer $MIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen/qwen3.5-4b-free",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Tell me a fun fact about chihuahuas."}
    ]
  }'
```

#### Python

**`file=`**

```python file=chat.py
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["MIXLAYER_API_KEY"],
    base_url="https://mixlayer.ai/v1",
)

response = client.chat.completions.create(
    model="qwen/qwen3.5-4b-free",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Tell me a fun fact about chihuahuas."},
    ],
)

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

#### TypeScript

**`file=`**

```typescript file=chat.ts
import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env["MIXLAYER_API_KEY"]!,
  baseURL: "https://mixlayer.ai/v1",
});

const response = await openai.chat.completions.create({
  model: "qwen/qwen3.5-4b-free",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Tell me a fun fact about chihuahuas." },
  ],
});

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

#### Rust

**`file=`**

```rust file=src/main.rs
use async_openai::{
    config::OpenAIConfig,
    types::{ChatCompletionRequestMessage, CreateChatCompletionRequestArgs},
    Client,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = OpenAIConfig::new()
        .with_api_key(std::env::var("MIXLAYER_API_KEY")?)
        .with_api_base("https://mixlayer.ai/v1");
    let client = Client::with_config(config);

    let request = CreateChatCompletionRequestArgs::default()
        .model("qwen/qwen3.5-4b-free")
        .messages([
            ChatCompletionRequestMessage::System(
                "You are a helpful assistant.".into(),
            ),
            ChatCompletionRequestMessage::User(
                "Tell me a fun fact about chihuahuas.".into(),
            ),
        ])
        .build()?;

    let response = client.chat().create(request).await?;
    if let Some(content) = &response.choices[0].message.content {
        println!("{content}");
    }

    Ok(())
}
```

## Streaming

Pass `stream: true` to receive tokens as Server-Sent Events as they're generated.

#### curl

**`file=`**

```bash file=stream.sh
curl https://mixlayer.ai/v1/chat/completions \
  -H "Authorization: Bearer $MIXLAYER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen/qwen3.5-4b-free",
    "stream": true,
    "messages": [
      {"role": "user", "content": "Tell me a fun fact about chihuahuas."}
    ]
  }'
```

#### Python

**`file=`**

```python file=stream.py
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["MIXLAYER_API_KEY"],
    base_url="https://mixlayer.ai/v1",
)

stream = client.chat.completions.create(
    model="qwen/qwen3.5-4b-free",
    messages=[{"role": "user", "content": "Tell me a fun fact about chihuahuas."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content:
        print(delta.content, end="", flush=True)
```

#### TypeScript

**`file=`**

```typescript file=stream.ts
import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env["MIXLAYER_API_KEY"]!,
  baseURL: "https://mixlayer.ai/v1",
});

const stream = await openai.chat.completions.create({
  model: "qwen/qwen3.5-4b-free",
  messages: [{ role: "user", content: "Tell me a fun fact about chihuahuas." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0].delta.content ?? "");
}
```

#### Rust

**`file=`**

```rust file=src/main.rs
use async_openai::{config::OpenAIConfig, types::CreateChatCompletionRequestArgs, Client};
use futures::StreamExt;
use std::io::Write;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = OpenAIConfig::new()
        .with_api_key(std::env::var("MIXLAYER_API_KEY")?)
        .with_api_base("https://mixlayer.ai/v1");
    let client = Client::with_config(config);

    let request = CreateChatCompletionRequestArgs::default()
        .model("qwen/qwen3.5-4b-free")
        .messages([async_openai::types::ChatCompletionRequestMessage::User(
            "Tell me a fun fact about chihuahuas.".into(),
        )])
        .stream(true)
        .build()?;

    let mut stream = client.chat().create_stream(request).await?;
    while let Some(result) = stream.next().await {
        let response = result?;
        if let Some(content) = &response.choices[0].delta.content {
            print!("{content}");
            std::io::stdout().flush()?;
        }
    }

    Ok(())
}
```

> **Info**
>
> See [Chat Completions](/api-reference/inference-ap-is/chat-completions/chat-completions) for the full list of supported
> request parameters and the streaming event shape.

## Vercel AI SDK

You can also use Mixlayer via the OpenAI-compatible provider in the [Vercel AI SDK](https://sdk.vercel.ai/).

```bash
npm install ai @ai-sdk/openai
```

**`file=`**

```typescript file=route.ts
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";
import type { NextRequest } from "next/server";

const mixlayer = createOpenAI({
  apiKey: process.env.MIXLAYER_API_KEY,
  baseURL: "https://mixlayer.ai/v1",
});

export async function POST(req: NextRequest) {
  const { prompt } = await req.json();

  const result = streamText({
    model: mixlayer.chat("qwen/qwen3.5-397b-a17b"),
    prompt,
    maxTokens: 1000,
  });

  return result.toDataStreamResponse();
}
```