Skip to main content
POST
/
v1
/
chat
/
completions
POST /v1/chat/completions
curl --request POST \
  --url https://api.geekhub.mx/v1/chat/completions \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "<string>",
  "messages": [
    {
      "role": {},
      "content": "<string>",
      "name": "<string>"
    }
  ],
  "temperature": 123,
  "top_p": 123,
  "max_tokens": 123,
  "stream": true,
  "stop": [
    "<string>"
  ]
}
'
{
  "id": "<string>",
  "object": "<string>",
  "created": 123,
  "model": "<string>",
  "choices": [
    {
      "index": 123,
      "message.role": "<string>",
      "message.content": "<string>",
      "finish_reason": "<string>"
    }
  ],
  "usage": {
    "prompt_tokens": 123,
    "completion_tokens": 123,
    "total_tokens": 123
  }
}

Request body

model
string
required
El ID namespaced del modelo, ej. anthropic/claude-sonnet-4-6. Ver Modelos chat.
messages
array
required
Array de mensajes en la conversación.
temperature
number
default:"varía por modelo"
Entre 0 y 2. Más alto = más creativo, más bajo = más determinista.
top_p
number
Entre 0 y 1. Nucleus sampling. Alternativa a temperature.
max_tokens
integer
Máximo de tokens a generar. Default varía por modelo.
stream
boolean
default:"false"
Si true, responde con Server-Sent Events. Ver sección Streaming abajo.
stop
string | string[]
Sequencias que terminan la generación.

Response (non-streaming)

id
string
Tu request_id (formato req_<24hex>). Útil para tracing.
object
string
Siempre "chat.completion".
created
integer
Unix timestamp.
model
string
El model id namespaced (e.g. anthropic/claude-sonnet-4-6).
choices
array
Array con un solo elemento (no soportamos n > 1 todavía).
usage
object
Tokens consumidos. Se cobran al cierre.

Streaming

Para responses en tiempo real, manda "stream": true. Recibirás Server-Sent Events:
data: {"id":"req_xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}

data: {"id":"req_xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hola"},"finish_reason":null}]}

data: {"id":"req_xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" mundo"},"finish_reason":null}]}

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

data: [DONE]
Cada data: es un JSON con un delta.content que es el siguiente fragmento del texto (puede ser una palabra, una sílaba, hasta un solo caracter).

Parser TypeScript

const res = await fetch("https://api.geekhub.mx/v1/chat/completions", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.GEEKHUB_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "anthropic/claude-haiku-4-5",
    messages: [{ role: "user", content: "Cuenta hasta 5" }],
    stream: true,
  }),
});

const reader = res.body!.getReader();
const decoder = new TextDecoder();
let buffer = "";

while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  buffer += decoder.decode(value, { stream: true });

  let nl: number;
  while ((nl = buffer.indexOf("\n\n")) !== -1) {
    const block = buffer.slice(0, nl);
    buffer = buffer.slice(nl + 2);
    const data = block.split("\n").find(l => l.startsWith("data:"))?.slice(5).trim();
    if (!data || data === "[DONE]") continue;
    const chunk = JSON.parse(data);
    process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
  }
}

Ejemplos por proveedor

response = client.chat.completions.create(
    model="anthropic/claude-sonnet-4-6",
    messages=[
        {"role": "system", "content": "Eres un asistente conciso."},
        {"role": "user", "content": "¿Capital de México?"}
    ],
    max_tokens=200,
)

Errores comunes

Ver Errores para el catálogo completo. Los más frecuentes en chat:
  • 400 invalid_request_error — body mal formado (Zod te dice el campo en message)
  • 402 insufficient_balance — sin saldo
  • 404 model_not_found — model id inválido (probablemente le faltó namespace)
  • 502 provider_unavailable — el provider rebotó (a veces es prompt rechazado)