# API Reference

หน้านี้แสดงรายการ API Endpoint ทั้งหมดสำหรับการเชื่อมต่อจากบุคคลที่สาม

:::note[ต้องใช้แพ็กเกจ Business ขึ้นไป]
API ใช้ได้เฉพาะองค์กรที่มีแพ็กเกจ **Business** ขึ้นไปเท่านั้น
:::
## การยืนยันตัวตน

การเชื่อมต่อจากบุคคลที่สามใช้ **API Key** ที่ส่งผ่านหัวข้อ `x-api-key`:

```bash
curl -H "x-api-key: dpk_YOUR_API_KEY" https://app.dailyplay.ai/api/...
```

Endpoint สำหรับจัดการจากแดชบอร์ดใช้ **Clerk Bearer Token** แทน:

```bash
curl -H "Authorization: Bearer <clerk_token>" https://app.dailyplay.ai/api/...
```

---

## Session Endpoints

Endpoint เหล่านี้ใช้สำหรับการเชื่อมต่อจากบุคคลที่สามในการสร้างและจัดการเซสชันโทเค็นแบบใช้ครั้งเดียว

### สร้างเซสชัน

สร้างเซสชันโทเค็นแบบใช้ครั้งเดียวสำหรับเกมหรือสตรีม

```
POST /api/org-api-keys?action=create-session
```

**Headers:**

| Header         | ค่า                  |
| -------------- | -------------------- |
| `Content-Type` | `application/json`   |
| `x-api-key`   | API Key (`dpk_...`)  |

**Body:**

```json
{
  "game_id": 42,
  "stream_id": null,
  "external_ref": "user-12345",
  "metadata": { "campaign": "summer-promo" },
  "expires_in_minutes": 60
}
```

| ฟิลด์               | ประเภท | จำเป็น | คำอธิบาย                        |
| -------------------- | ------ | ------ | ------------------------------- |
| `game_id`            | number | \*     | Game ID เป้าหมาย                |
| `stream_id`          | number | \*     | Stream ID เป้าหมาย              |
| `external_ref`       | string | ไม่    | อ้างอิงสำหรับติดตาม             |
| `metadata`           | object | ไม่    | ข้อมูล JSON ตามต้องการ           |
| `expires_in_minutes` | number | ไม่    | ระยะเวลาหมดอายุ (ค่าเริ่มต้น: 1440) |

\* ต้องระบุ `game_id` หรือ `stream_id` อย่างน้อย 1 อย่าง

**Response `200`:**

```json
{
  "success": true,
  "data": {
    "token": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "stream_id": null,
    "game_id": 42,
    "expires_at": "2026-02-17T12:00:00.000Z",
    "external_ref": "user-12345"
  }
}
```

---

### ตรวจสอบเซสชัน

ตรวจสอบว่าเซสชันโทเค็นยังใช้ได้หรือไม่โดยไม่ใช้โทเค็น

```
GET /api/org-api-keys?action=validate-session&token=<uuid>
```

**Query Parameters:**

| พารามิเตอร์ | ประเภท        | จำเป็น | คำอธิบาย                      |
| ------------ | ------------- | ------ | ----------------------------- |
| `token`      | string (UUID) | ใช่    | เซสชันโทเค็นที่ต้องการตรวจสอบ |

**Response `200`:**

```json
{
  "success": true,
  "data": {
    "valid": true,
    "status": "active",
    "stream_id": null,
    "game_id": 42,
    "expires_at": "2026-02-17T12:00:00.000Z",
    "external_ref": "user-12345",
    "metadata": { "campaign": "summer-promo" }
  }
}
```

---

### ใช้เซสชัน

ใช้เซสชันโทเค็นแบบ atomic ทำให้ไม่สามารถใช้ซ้ำ

```
POST /api/org-api-keys?action=consume-session
```

**Headers:**

| Header         | ค่า                |
| -------------- | ------------------ |
| `Content-Type` | `application/json` |

**Body:**

```json
{
  "token": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "player_uuid": "player-uuid-here"
}
```

| ฟิลด์        | ประเภท        | จำเป็น | คำอธิบาย                      |
| ------------- | ------------- | ------ | ----------------------------- |
| `token`       | string (UUID) | ใช่    | เซสชันโทเค็นที่ต้องการใช้      |
| `player_uuid` | string        | ไม่    | UUID สำหรับระบุผู้เล่น         |

**Response `200`:**

```json
{
  "success": true,
  "data": {
    "session_id": 1,
    "org_id": 5,
    "stream_id": null,
    "game_id": 42,
    "external_ref": "user-12345",
    "metadata": { "campaign": "summer-promo" }
  }
}
```

**Response `410` (ใช้แล้ว/หมดอายุ):**

```json
{
  "success": false,
  "error": "Session is invalid, already consumed, or expired"
}
```

---

## API Key Management Endpoints

Endpoint เหล่านี้ต้องใช้ **Clerk Bearer Token** และใช้สำหรับจัดการ API Keys จากแดชบอร์ด DailyPlay

### แสดงรายการ API Keys

```
GET /api/org-api-keys?org_id=<id>
```

แสดง API Keys ทั้งหมดขององค์กรที่ระบุ

---

### สร้าง API Key

```
POST /api/org-api-keys
```

**Body:**

```json
{
  "org_id": 1,
  "name": "Production CRM",
  "description": "CRM ใช้สำหรับออกลิงก์เกม",
  "allowed_game_ids": [42, 43],
  "rate_limit_per_minute": 60,
  "rate_limit_per_day": 10000,
  "expires_at": "2027-01-01T00:00:00Z"
}
```