NEXAI Docs

API Reference

Endpoint, tham số, streaming, tool-calling và mã lỗi của NEXAI — tương thích OpenAI.

NEXAI theo đúng chuẩn OpenAI. Mọi thư viện/tool OpenAI dùng được khi trỏ base_url sang NEXAI.

Base URL & xác thực

https://getnexai.net/api/v1

Mỗi request gửi kèm header:

Authorization: Bearer sk-YOUR_API_KEY
Content-Type: application/json

Key sk-... tạo ở dashboard → API Keys. Có thể đặt hạn mức chi cho từng key để kiểm soát chi tiêu.

POST /v1/chat/completions

Endpoint chính để trò chuyện / sinh văn bản.

Tham số

TrườngKiểuBắt buộcMô tả
modelstring✓Tên model (vd gpt-4o-mini, deepseek-v4-pro, hoặc nexai nếu không muốn chọn)
messagesarray✓Danh sách hội thoại: { role: "system"|"user"|"assistant", content }
streambooleantrue để nhận token theo thời gian thực (SSE)
temperaturenumberĐộ ngẫu nhiên (0–2)
max_tokensintegerGiới hạn token đầu ra
top_pnumberNucleus sampling
toolsarrayĐịnh nghĩa function/tool (chuẩn OpenAI)
tool_choicestring/objectÉp/gợi ý dùng tool
stopstring/arrayChuỗi dừng

Ví dụ

curl -X POST 'https://getnexai.net/api/v1/chat/completions' \
  -H 'Authorization: Bearer sk-YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role":"user","content":"Xin chào"}]
  }'

Phản hồi (rút gọn):

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "model": "gpt-4o-mini",
  "choices": [
    { "index": 0, "message": { "role": "assistant", "content": "Chào bạn!" }, "finish_reason": "stop" }
  ],
  "usage": { "prompt_tokens": 9, "completion_tokens": 4, "total_tokens": 13 }
}

Streaming

Đặt "stream": true → nhận các data: event (SSE) theo chuẩn OpenAI; kết thúc bằng data: [DONE].

stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Đếm từ 1 đến 5"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

Tool calling

Hỗ trợ function calling theo chuẩn OpenAI: khai tools, model trả tool_calls, bạn thực thi rồi gửi kết quả lại bằng message role: "tool". Hoạt động cả ở chế độ streaming.

GET /v1/models

Liệt kê model đang mở cho tài khoản:

curl 'https://getnexai.net/api/v1/models' -H 'Authorization: Bearer sk-YOUR_API_KEY'

Trả về { "object": "list", "data": [{ "id": "...", "object": "model", ... }] }. Dùng đúng id trong trường model của request.

POST /v1/responses

Bề mặt Responses API (tương thích OpenAI) — dùng cho các tool như Codex (wire_api = "responses"). Xem Dùng với Codex.

POST /v1/messages

Bề mặt tương thích Anthropic Messages — dùng cho Claude Code (đặt ANTHROPIC_BASE_URL). Model dạng claude-... được ánh xạ sang model tương ứng. Xem Dùng với Claude Code.

Mã lỗi

HTTPÝ nghĩaCách xử lý
401Sai/thiếu/hết hạn keyKiểm tra header Authorization; tạo key mới nếu cần
402Không đủ số dưNạp thêm ở dashboard → Nạp tiền
403Không có quyền dùng modelKiểm tra model + trạng thái tài khoản
404Model không tồn tạiDùng id từ GET /v1/models
429Vượt giới hạn tần suấtGiảm nhịp gọi, thử lại sau
502Lỗi tạm thời phía dịch vụThử lại sau vài giây
503Model chưa cấu hình nguồn khả dụngChọn model khác đang mở, hoặc liên hệ hỗ trợ

Body lỗi theo dạng OpenAI: { "error": { "message": "...", "code": "..." } }.

Giới hạn tần suất

Cổng API có giới hạn tần suất để bảo vệ hệ thống. Nếu gặp 429, hãy thêm cơ chế retry có backoff (thử lại sau vài trăm ms, tăng dần). Streaming được khuyến nghị cho phản hồi dài để giảm thời gian chờ.