Tài liệu kết nối API

Kết nối ứng dụng của bạn với e-Gate AI Gateway trong vài phút — chỉ cần đổi base URL và API key.

1. Tổng quan & Base URL

e-Gate AI Gateway là cổng API tương thích OpenAI. Gọi tất cả model qua một base URL duy nhất. API key bắt đầu bằng máy chủ xử lý (machineId) — gateway tự định tuyến tới đúng server:

  • sk-61d35a1d80f1d3dd-... — Server 1
  • sk-9357e1fe59b66398-... — Server 2

Điều này nghĩa là bạn chỉ cần tạo key trên dashboard, gateway sẽ đưa request tới server tương ứng — không cần cấu hình gì thêm.

2. Tạo API key

Đăng nhập dashboard → mục API Keys → chọn server, đặt tên, gán budget → bấm Create. Full key (sk-<machineId>-...) chỉ hiển thị đúng 1 lần — hãy sao chép và lưu ngay.

3. Xác thực

Mọi request phải kèm header Authorization dạng Bearer với key của bạn:

Authorization: Bearer sk-61d35a1d80f1d3dd-YOUR-KEY

Base URL

Tất cả request gửi tới một endpoint duy nhất. Bỏ qua biến môi trường cũ của từng nhà cung cấp.

https://agent.e-gate.vn/v1

4. Gọi model đầu tiên (curl)

Thay YOUR-KEY bằng key của bạn. Model hợp lệ ví dụ: gpt-4o, claude-3-5-sonnet, gemini-2.0-flash, deepseek-chat — xem danh sách đầy đủ trên trang Prices.

curl https://agent.e-gate.vn/v1/chat/completions \
  -H "Authorization: Bearer sk-61d35a1d80f1d3dd-YOUR-KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{ "role": "user", "content": "Hello!" }]
  }'

5. Dùng SDK (Python / Node.js)

e-Gate tương thích hoàn toàn với SDK chính thức của OpenAI — chỉ cần trỏ base_url tới e-Gate và đổi api_key:

from openai import OpenAI

client = OpenAI(
    base_url="https://agent.e-gate.vn/v1",
    api_key="sk-61d35a1d80f1d3dd-YOUR-KEY",
)

resp = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(resp.choices[0].message.content)

Streaming (SSE):

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
    stream=True,
)

for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")
import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://agent.e-gate.vn/v1',
  apiKey: 'sk-61d35a1d80f1d3dd-YOUR-KEY',
});

const resp = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(resp.choices[0].message.content);

Cài đặt IDE & client

Cấu hình các công cụ phổ biến dùng base URL và key của bạn.

Cursor
# Cursor → Settings → Models → Add custom model
Name:        gateway
Base URL:    https://agent.e-gate.vn/v1
API Key:     sk-61d35a1d80f1d3dd-YOUR-KEY
Model ID:    gpt-4o   (or any model from /v1/models)

Image & Video Generation

Tạo ảnh đồng bộ, tạo video bất đồng bộ (tạo job → poll trạng thái → tải file). Video tính tiền theo giây, xem giá $/giây trên trang Prices.

# Image generation
curl https://agent.e-gate.vn/v1/images/generations \
  -H "Authorization: Bearer sk-61d35a1d80f1d3dd-YOUR-KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"dall-e-3","prompt":"a cat","n":1,"size":"1024x1024"}'

# Video generation (async: create → poll → download)
curl https://agent.e-gate.vn/v1/videos/generations \
  -H "Authorization: Bearer sk-61d35a1d80f1d3dd-YOUR-KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"videopr/wan3.0","prompt":"a cat walking","seconds":"5","size":"1280x720"}'
# → {"id":"task_...","status":"queued"} — poll until completed.
# Use the id exactly as returned (it may look like "task_x:provider").
# If the server says "Query parameter 'model' is required", add ?model=<model>:
curl https://agent.e-gate.vn/v1/videos/task_... \
  -H "Authorization: Bearer sk-61d35a1d80f1d3dd-YOUR-KEY"
# → {"status":"completed","video_url":"https://...mp4"} — download the file

Chuyển văn bản thành giọng nói (TTS)

Trả về file audio nhị phân (không phải JSON) — lưu ra file với đuôi đúng định dạng đã chọn. Input tối đa 4096 ký tự.

# Text-to-speech (returns binary audio, not JSON)
curl https://agent.e-gate.vn/v1/audio/speech \
  -H "Authorization: Bearer sk-61d35a1d80f1d3dd-YOUR-KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"tts-1","input":"Hello world","voice":"alloy","response_format":"mp3"}' \
  --output hello.mp3

8. Lỗi phổ biến & hỗ trợ

Mã lỗi bạn có thể gặp:

  • 401 — Sai hoặc thiếu Authorization — kiểm tra key còn hiệu lực, đúng server.
  • 402 — Key hết budget — nạp thêm budget trong dashboard.
  • 404 — Sai path hoặc model không tồn tại — kiểm tra /v1/chat/completions và tên model.
  • 429 — Quá nhiều request / hết credit tổng — giảm tần suất hoặc nạp thêm.

Cần hỗ trợ? Nhắn Messenger (Messenger), Telegram (t.me/egateai) hoặc email [email protected].