Core API

Chat Completions

Create AI model responses from a conversation. Supports both non-streaming and streaming (SSE).

Create model responses from a conversation. Supports both non-streaming and streaming (SSE). Requires API key authentication.

Endpoint#

POST /v1/chat/completions

Request Parameters#

ParameterTypeRequiredDefaultDescription
messagesChatMessage[]YesArray of messages. Each item has role ('system' | 'user' | 'assistant') and content (string).
modelstringNolunaby-proModel ID to use.
max_tokensintegerNoMaximum tokens in the response.
temperaturefloatNo1Creativity from 0 to 2. Higher = more random.
top_pfloatNo1Nucleus sampling. Alternative to temperature.
streambooleanNofalseEnable streaming SSE response.
stopstring | string[]NoString or array of strings to stop generation.
presence_penaltyfloatNo0Penalize tokens that have appeared, -2.0 to 2.0.
frequency_penaltyfloatNo0Penalize frequent tokens, -2.0 to 2.0.
userstringNoUser ID for tracking.

Example — Non-streaming#

typescript
import Lunaby from 'lunaby-sdk';

const client = new Lunaby({ apiKey: process.env.LUNABY_API_KEY });

const response = await client.chat.create([
  { role: 'system', content: 'You are a helpful assistant.' },
  { role: 'user', content: 'What is TypeScript?' }
], {
  model: 'lunaby-pro',
  temperature: 0.7,
  max_tokens: 1024,
});

console.log(response.data.choices[0].message.content);
console.log('Tokens:', response.data.usage.total_tokens);

Example — cURL#

bash
curl https://api.lunie.dev/v1/chat/completions \
  -H "Authorization: Bearer $LUNABY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "lunaby-pro",
    "messages": [
      { "role": "system", "content": "You are a helpful assistant." },
      { "role": "user", "content": "What is TypeScript?" }
    ],
    "temperature": 0.7
  }'

Response#

json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1742500000,
  "model": "lunaby-pro",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "TypeScript is a programming language..."
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 150,
    "total_tokens": 175
  }
}

Multi-turn Conversation#

typescript
import type { ChatMessage } from 'lunaby-sdk';

const messages: ChatMessage[] = [
  { role: 'system', content: 'You are a math tutor.' }
];

// Turn 1
messages.push({ role: 'user', content: 'What is 2 + 2?' });
let res = await client.chat.create(messages);
const answer1 = res.data.choices[0].message.content;
messages.push({ role: 'assistant', content: answer1 });

// Turn 2
messages.push({ role: 'user', content: 'Multiply that result by 3?' });
res = await client.chat.create(messages);
console.log(res.data.choices[0].message.content);