Bắt đầu nhanh
Tạo API key và gửi request đầu tiên tới NEXAI trong vài phút.
Ba bước để chạy: tạo key → trỏ base_url → gửi request. NEXAI theo đúng chuẩn OpenAI nên mọi thư viện/tool hỗ trợ OpenAI đều dùng được.
1. Tạo API key
Đăng nhập dashboard → API Keys → Tạo key. Key có dạng sk-...; sao chép và lưu an toàn — vì lý do bảo mật, key đầy đủ chỉ hiện một lần.
2. Endpoint
https://getnexai.net/api/v1Đây là base_url bạn đặt cho thư viện OpenAI (hoặc bất kỳ client tương thích OpenAI nào). Mọi request qua HTTPS, xác thực bằng header Authorization: Bearer sk-....
3. Request đầu tiên
cURL
curl -X POST 'https://getnexai.net/api/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-YOUR_API_KEY' \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role":"user","content":"Say hello from NEXAI"}]
}'Python
from openai import OpenAI
client = OpenAI(
api_key="sk-YOUR_API_KEY",
base_url="https://getnexai.net/api/v1",
)
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hello from NEXAI"}],
)
print(resp.choices[0].message.content)Node.js
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-YOUR_API_KEY",
baseURL: "https://getnexai.net/api/v1",
});
const resp = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Say hello from NEXAI" }],
});
console.log(resp.choices[0].message.content);Streaming
Thêm "stream": true để nhận token theo thời gian thực (Server-Sent Events) — y hệt OpenAI:
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Viết một bài haiku"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")Tool calling (function calling)
Khai tools theo chuẩn OpenAI; model trả về tool_calls, bạn thực thi rồi gửi kết quả lại:
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Thời tiết Hà Nội?"}],
tools=[{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
},
}],
)
print(resp.choices[0].message.tool_calls)Xử lý lỗi thường gặp
- 401 — key sai/thiếu: kiểm tra header
Authorization: Bearer sk-.... - 402 — hết số dư: nạp thêm ở dashboard.
- 404 — sai tên model: dùng
idtừGET /v1/models. - 429 — vượt tần suất: thêm retry có backoff (đợi tăng dần rồi thử lại).
Body lỗi theo dạng OpenAI: { "error": { "message": "...", "code": "..." } }. Xem đầy đủ ở API Reference.
Bước tiếp theo
- API Reference — endpoint, tham số, mã lỗi đầy đủ.
- Model — danh sách model khả dụng và cách chọn.