OpenAI-compatible là lựa chọn phổ biến nhất vì nhiều SDK và framework đã hỗ trợ sẵn. Bạn chỉ cần đổi `baseURL` sang cheapkeyai.shop và dùng API key đã mua.
Cài đặt
npm install openaiKhởi tạo client
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.CHEAPKEYAI_API_KEY,
baseURL: "https://cheapkeyai.shop/v1"
});
const result = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Xin chào!" }]
});
console.log(result.choices[0].message.content);Chat Completions
const stream = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Viết hướng dẫn deploy app React." }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}const result = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{
role: "user",
content: [
{ type: "text", text: "Mô tả ảnh này." },
{ type: "image_url", image_url: { url: "https://example.com/image.png" } }
]
}]
});const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Thời tiết Hà Nội?" }],
tools: [{
type: "function",
function: {
name: "get_weather",
description: "Lấy thời tiết theo thành phố",
parameters: {
type: "object",
properties: { location: { type: "string" } },
required: ["location"]
}
}
}]
});Responses API
curl https://cheapkeyai.shop/v1/responses \
-H "Authorization: Bearer <your-api-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5-mini",
"input": "Tạo checklist SEO cho trang docs."
}'Images API
const image = await openai.images.generate({
model: "gpt-image-1",
prompt: "Dashboard docs hiện đại, ánh sáng tự nhiên",
size: "1024x1024"
});Videos API
Video thường là tác vụ bất đồng bộ. Sau khi tạo task, bạn poll endpoint trạng thái cho tới khi hoàn thành rồi lấy URL kết quả.
Embeddings, Rerank, Moderations
- Embeddings dùng cho search, RAG và clustering.
- Rerank dùng sắp xếp lại tài liệu theo độ liên quan.
- Moderations dùng kiểm tra nội dung đầu vào hoặc đầu ra.
Xử lý lỗi
try {
await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello" }]
});
} catch (error) {
console.error(error.status, error.message);
}