---
title: "คู่มือเริ่มต้นใช้งาน API"
description: "การเรียก API v1 ครั้งแรกของคุณด้วย curl ได้แก่ แสดงรายการกล้อง บันทึกภาพนิ่ง รับทราบการแจ้งเตือนแบบกลุ่ม ส่งออกคลิปและดาวน์โหลด และแชร์ด้วยลิงก์สาธารณะ"
url: https://vigiora.com/th/docs/developers/api-quickstart
updated: 2026-11-25T11:49:35.000Z
---

# คู่มือเริ่มต้นใช้งาน API

> การเรียก API v1 ครั้งแรกของคุณด้วย curl ได้แก่ แสดงรายการกล้อง บันทึกภาพนิ่ง รับทราบการแจ้งเตือนแบบกลุ่ม ส่งออกคลิปและดาวน์โหลด และแชร์ด้วยลิงก์สาธารณะ

หน้านี้แนะนำงานทั่วไปด้วย `curl` แทนที่ `yourname.vigiora.com` ด้วยที่อยู่ไซต์ของคุณ (หรือ `http://<server-ip>:8080` บนเครือข่ายท้องถิ่น) และแทนที่คีย์ด้วยคีย์ของคุณเอง ตัวอย่างใช้ `jq` เพื่อดึงฟิลด์จากคำตอบ

## 1. รับคีย์

สร้างคีย์ที่มี scope ที่ตัวอย่างเหล่านี้ต้องใช้ เข้าสู่ระบบเพื่อรับโทเค็นเซสชัน แล้วใช้โทเค็นนั้นสร้างคีย์:

```bash
BASE="https://yourname.vigiora.com"

TOKEN=$(curl -s -X POST "$BASE/api/auth/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"automation","password":"example-password"}' | jq -r .access_token)

curl -s -X POST "$BASE/api/v1/api-keys" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name":"Quickstart","scopes":["cameras:read","alerts:write","recordings:write","clips:share"],"expires_in_days":30}' \
  | jq -r .key
```

คำสั่งสุดท้ายจะพิมพ์คีย์ออกมา เช่น `vgk_xxxxxxxx_xxxxxxxxxxxxxxxx` คีย์แสดงเพียงครั้งเดียว เก็บไว้ในตัวแปรสำหรับขั้นตอนถัดไป:

```bash
KEY="vgk_xxxxxxxx_xxxxxxxxxxxxxxxx"
```

ตรวจสอบคีย์:

```bash
curl -s "$BASE/api/v1/me" -H "Authorization: Bearer $KEY"
```

ดู [การยืนยันตัวตนและ scope](https://vigiora.com/docs/developers/api-authentication) ว่าแต่ละ scope อนุญาตอะไร คีย์จาก **การตั้งค่า** → **ผู้คนและการเข้าถึง** → **คีย์ API** ในแอปก็ใช้ได้เช่นกัน แต่ไม่มี `clips:share`

## 2. แสดงรายการกล้อง

```bash
curl -s "$BASE/api/v1/cameras" -H "Authorization: Bearer $KEY" \
  | jq '.data[] | {id, name, status, ai_enabled}'
```

```json
{ "id": 3, "name": "Front door", "status": "online", "ai_enabled": true }
{ "id": 4, "name": "Parking", "status": "offline", "ai_enabled": false }
```

จะแสดงเฉพาะกล้องที่ผู้ใช้ของคีย์ดูได้ เพิ่ม `?enabled=true` หรือ `?site_id=2` เพื่อกรอง รายการจะแบ่งเป็นหน้า ดู [การแบ่งหน้า](https://vigiora.com/docs/developers/api-conventions#pagination)

## 3. บันทึกภาพนิ่ง

```bash
curl -s "$BASE/api/v1/cameras/3/snapshot" -H "Authorization: Bearer $KEY" -o front-door.jpg
```

คุณจะได้ JPEG ปัจจุบัน หากกล้องสร้างภาพไม่ได้ในขณะนั้น อาจได้เฟรมที่เก่าไม่เกินหนึ่งชั่วโมง พร้อมอายุเป็นวินาทีใน header `Age` มิฉะนั้นคำตอบจะเป็น `503 snapshot_unavailable`

## 4. รับทราบการแจ้งเตือนแบบกลุ่ม

แสดงรายการการแจ้งเตือนที่ยังไม่รับทราบของกล้องหนึ่งตัว:

```bash
curl -s "$BASE/api/v1/alerts?camera_id=3&is_alert=true&acknowledged=false" \
  -H "Authorization: Bearer $KEY" | jq '.data[] | {id, label, detected_at}'
```

รับทราบบางรายการตาม id (ครั้งละไม่เกิน 500 รายการ):

```bash
curl -s -X POST "$BASE/api/v1/alerts/actions/acknowledge" \
  -H "Authorization: Bearer $KEY" \
  -H "Idempotency-Key: ack-2026-10-30-1" \
  -H "Content-Type: application/json" \
  -d '{"ids":[1201,1202,1203]}'
```

```json
{ "acknowledged_count": 3, "not_found_ids": [], "forbidden_ids": [] }
```

หรือรับทราบทุกรายการก่อนเวลาที่กำหนด โดยจะระบุกล้องหนึ่งตัวก็ได้:

```bash
curl -s -X POST "$BASE/api/v1/alerts/actions/acknowledge" \
  -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json" \
  -d '{"before":"2026-10-30T12:00:00Z","camera_id":3}'
```

id ที่คุณมองไม่เห็นจะกลับมาใน `not_found_ids` ส่วน id บนกล้องที่คุณไม่มีสิทธิ์ operator จะกลับมาใน `forbidden_ids`

## 5. ส่งออกคลิปและดาวน์โหลด

ขอการบันทึกของกล้องได้สูงสุด 30 นาทีเป็น MP4 ไฟล์เดียว คำตอบจะเป็น `202` พร้อมงานส่งออก:

```bash
CLIP=$(curl -s -X POST "$BASE/api/v1/clips" \
  -H "Authorization: Bearer $KEY" \
  -H "Idempotency-Key: clip-front-door-1400" \
  -H "Content-Type: application/json" \
  -d '{"camera_id":3,"from":"2026-10-30T14:00:00Z","to":"2026-10-30T14:05:00Z"}' | jq -r .id)
```

รอจนพร้อม:

```bash
until [ "$(curl -s "$BASE/api/v1/clips/$CLIP" -H "Authorization: Bearer $KEY" | jq -r .status)" != "running" ]; do
  sleep 2
done
```

`status` จะจบด้วย `ready` หรือ `failed` (พร้อมเหตุผลใน `error`) จากนั้นขอ URL ดาวน์โหลด ซึ่งใช้ได้ครั้งเดียวภายใน 60 วินาที โดยไม่ต้องใช้ข้อมูลรับรอง:

```bash
URL=$(curl -s -X POST "$BASE/api/v1/clips/$CLIP/actions/create-download-url" \
  -H "Authorization: Bearer $KEY" | jq -r .url)
curl -s "$BASE$URL" -o clip.mp4
```

คลิปที่พร้อมแล้วจะเก็บไว้ 24 ชั่วโมง และนานกว่านั้นขณะที่ลิงก์แชร์ยังใช้งานอยู่ `GET /api/v1/clips` แสดงรายการคลิปของคุณ

## 6. แชร์คลิปด้วยลิงก์สาธารณะ

ทุกคนที่มีลิงก์ดูคลิปได้โดยไม่ต้องเข้าสู่ระบบ ต้องใช้ scope `clips:share`:

```bash
curl -s -X POST "$BASE/api/v1/clips/$CLIP/shares" \
  -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json" \
  -d '{"expires_in":"24h"}' | jq '{id, url, expires_at}'
```

```json
{ "id": 42, "url": "https://yourname.vigiora.com/s/xxxxxxxxxxxxxxxx", "expires_at": "2026-10-31T14:06:00Z" }
```

`expires_in` เป็น `1h`, `24h` (ค่าเริ่มต้น) หรือ `7d` และลิงก์จะไม่มีอายุเกินคลิปมากกว่า 7 วัน URL จะส่งกลับ **ที่นี่เท่านั้น** ลิงก์ที่สร้างด้วยคีย์จะสิ้นสุดเมื่อคีย์ถูกเพิกถอนหรือหมดอายุด้วย เพิกถอนก่อนกำหนดได้ด้วย:

```bash
curl -s -X DELETE "$BASE/api/v1/clips/$CLIP/shares/42" -H "Authorization: Bearer $KEY"
```

## ขั้นตอนถัดไป

- [ข้อตกลง](https://vigiora.com/docs/developers/api-conventions): ข้อผิดพลาด การลองใหม่ด้วย `Idempotency-Key` ขีดจำกัดอัตรา
- [เอกสารอ้างอิง API](https://vigiora.com/docs/developers/api-reference): ทุกการดำเนินการพร้อมพารามิเตอร์และคำตอบ
- [ลิงก์แชร์](https://vigiora.com/docs/using/share-links): ลิงก์แบบเดียวกันในแอป

---

Source: https://vigiora.com/th/docs/developers/api-quickstart · Last updated 25 Nov 2026, 11:49 UTC
