Skip to main content
POST
/
v1
/
chat
/
completions
Chat Completions
curl --request POST \
  --url https://api.k-router.com/v1/chat/completions \
  --header 'Authorization: <authorization>' \
  --header 'Content-Type: <content-type>' \
  --data '
{
  "model": "<string>",
  "messages": [
    {}
  ],
  "stream": true,
  "temperature": 123,
  "max_tokens": 123,
  "top_p": 123
}
'

Chat Completions

OpenAI 호환 Chat Completions API입니다.

엔드포인트

POST https://api.k-router.com/v1/chat/completions

헤더

Authorization
string
required
Bearer 토큰. Bearer kr-your-api-key 형식입니다.
Content-Type
string
required
application/json

요청 본문

model
string
required
사용할 모델 ID. 예: kr/gpt4o, kr/claude-sonnet, kr/auto
messages
array
required
대화 메시지 배열. 각 메시지는 rolecontent를 포함합니다.
  • role: system, user, assistant 중 하나
  • content: 메시지 내용 (문자열)
stream
boolean
default:"false"
true로 설정하면 SSE(Server-Sent Events) 스트리밍으로 응답합니다.
temperature
number
default:"1.0"
생성 온도. 0~2 사이의 값. 낮을수록 결정적, 높을수록 창의적입니다.
max_tokens
integer
생성할 최대 토큰 수.
top_p
number
default:"1.0"
Nucleus sampling 파라미터.

응답

일반 응답

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1711000000,
  "model": "kr/gpt4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "안녕하세요! 무엇을 도와드릴까요?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 15,
    "total_tokens": 25
  }
}

SSE 스트리밍 응답

stream: true 설정 시 Server-Sent Events 형식으로 응답합니다:
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"안녕"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"하세요"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

스트리밍 예시

from openai import OpenAI

client = OpenAI(
    api_key="kr-your-api-key",
    base_url="https://api.k-router.com/v1"
)

stream = client.chat.completions.create(
    model="kr/gpt4o",
    messages=[{"role": "user", "content": "K-Router에 대해 설명해주세요"}],
    stream=True
)

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

에러 코드

코드설명
401인증 실패 — API 키를 확인하세요
402크레딧 부족 — 크레딧을 충전하세요
429요청 제한 초과 — 잠시 후 재시도하세요
500서버 오류 — 지속 시 고객센터에 문의하세요