Skip to main content
SDK xác thực mọi request bằng API key dạng bearer. Bạn khởi tạo một YoukaClient một lần và tái sử dụng nó trong suốt vòng đời của tiến trình.

new YoukaClient(options)

import { YoukaClient } from "@youka/vi/sdk";

const client = new YoukaClient({
  apiKey: process.env.YOUKA_API_KEY!,
});

Options

apiKey
string
required
API key Youka của bạn. Tạo một key tại online.youka.io/account trong mục API keys.
fetch
typeof fetch
Triển khai fetch tuỳ chỉnh. Mặc định là fetch toàn cục. Dùng phần này để chèn logging, proxy, hoặc middleware retry tuỳ chỉnh.

Lưu trữ API key

API key cấp quyền truy cập vào thông tin thanh toán của bạn. Không bao giờ commit chúng lên hệ thống quản lý mã nguồn.
Các mẫu khuyến nghị:
const client = new YoukaClient({
  apiKey: process.env.YOUKA_API_KEY!,
});

Inject một fetch tuỳ chỉnh

Mọi phương thức SDK đều dùng fetch bạn truyền vào constructor. Điều này giúp dễ dàng thêm logging hoặc middleware:
import { YoukaClient } from "@youka/vi/sdk";

const instrumentedFetch: typeof fetch = async (input, init) => {
  const start = Date.now();
  const response = await fetch(input, init);
  console.log(
    `${init?.method ?? "GET"} ${input} ${response.status} in ${Date.now() - start}ms`,
  );
  return response;
};

const client = new YoukaClient({
  apiKey: process.env.YOUKA_API_KEY!,
  fetch: instrumentedFetch,
});

Tuỳ chọn theo từng request

Mỗi phương thức chấp nhận một đối số thứ hai hoặc thứ ba (không bắt buộc) với các tuỳ chọn ở cấp request:
await client.projects.create(body, {
  idempotencyKey: "import-2026-04-08-song-001",
  signal: abortController.signal,
});
idempotencyKey
string
Được truyền dưới dạng header Idempotency-Key. Tái sử dụng cùng một key với cùng payload sẽ trả về kết quả ban đầu thay vì tạo bản trùng lặp. Xem API idempotency.
signal
AbortSignal
Abort signal tiêu chuẩn. Huỷ các request đang chạy và các lượt poll chạy lâu của client.tasks.wait(...), client.projects.wait(...), và client.exports.wait(...).

Tiếp theo