# Bắt đầu nhanh

> Ba bước để gọi AI qua KONG API AI: tạo tài khoản, tạo khoá, gửi yêu cầu đầu tiên bằng cURL, Python hoặc Node.js.

Source: https://kongapiai.com/docs/quickstart

## 1. Tạo tài khoản

Mở [trang đăng ký](https://kongapiai.com/app#signup), nhập email và mật khẩu. Mọi tài khoản bắt đầu ở gói Free, không cần thẻ.

## 2. Tạo khoá

Trong console, vào **API Keys** và tạo một khoá. Khoá bắt đầu bằng `gw_live_` hoặc `gw_test_`.

> Khoá chỉ hiện đầy đủ một lần lúc tạo. Hãy chép ngay vào nơi an toàn. Nếu mất, hãy thu hồi khoá đó và tạo khoá mới.

## 3. Gửi yêu cầu đầu tiên

Địa chỉ gốc của API là:

```text
https://kongapiai.com/v1
```

Thay `gw_live_…` bằng khoá của bạn và `MODEL_ID` bằng một model trong trang [Model và giá](https://kongapiai.com/docs/models).

```bash cURL
curl https://kongapiai.com/v1/chat/completions \
  -H "Authorization: Bearer gw_live_…" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"model": "MODEL_ID", "messages": [{"role": "user", "content": "Xin chào"}]}'
```

```python
# pip install openai
from openai import OpenAI
import uuid

client = OpenAI(base_url="https://kongapiai.com/v1", api_key="gw_live_…")
r = client.chat.completions.create(
    model="MODEL_ID",
    messages=[{"role": "user", "content": "Xin chào"}],
    extra_headers={"Idempotency-Key": str(uuid.uuid4())},
)
print(r.choices[0].message.content)
```

```js
// npm install openai
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://kongapiai.com/v1", apiKey: "gw_live_…" });
const r = await client.chat.completions.create(
  { model: "MODEL_ID", messages: [{ role: "user", content: "Xin chào" }] },
  { headers: { "Idempotency-Key": crypto.randomUUID() } },
);
console.log(r.choices[0].message.content);
```

## Vì sao nên gửi Idempotency-Key

`Idempotency-Key` là một mã bạn tự sinh cho mỗi yêu cầu. Nếu mạng rớt và mã của bạn gửi lại cùng yêu cầu với cùng mã đó, KONG API AI không gọi nhà cung cấp lần hai và không tính tiền hai lần. Chi tiết ở trang [API](https://kongapiai.com/docs/api).

## Bước tiếp theo

- Đặt hạn mức cho từng khoá: [Hạn mức và cách chọn nguồn](https://kongapiai.com/docs/limits).
- Cài khoá vào Claude Code, Codex, Cursor: [Cài vào công cụ](https://kongapiai.com/docs/tools).
- Xử lý lỗi trong mã: [Mã lỗi](https://kongapiai.com/docs/errors).
