Định dạng API

OpenAI Format

Cách dùng cheapkeyai.shop với SDK OpenAI: chat completions, streaming, vision, tool calling, responses, images, videos, embeddings và audio.

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
npm install openai

Khởi tạo client

Node.js
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

Streaming
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 || "");
}
Vision
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" } }
    ]
  }]
});
Tool calling
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
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

Tạo ảnh
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/catch
try {
  await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: "Hello" }]
  });
} catch (error) {
  console.error(error.status, error.message);
}