Authentication
Smongrot API ใช้ Bearer token authentication — ส่ง API key ใน Authorization header ของทุก request
Format
Authorization: Bearer sk_live_<64-hex-chars>
Example:
curl "https://api.smongrot.com/v1/health" \
-H "Authorization: Bearer sk_live_a1b2c3d4e5f67890abcdef1234567890abcdef1234567890abcdef1234567890"
Key format
| Part | ตัวอย่าง | หน้าที่ |
|---|---|---|
| Prefix | sk_live_ | บอกว่าเป็น production key |
| Secret | a1b2c3d4... | 64-char hex = 256-bit entropy |
sk_live_a1b2c3d4(12 ตัวแรก) = key prefix — ใช้แสดงใน Dashboard เพื่อแยก key แต่ละอัน (ไม่พอเดา secret)- ทั้ง 72 ตัว = plaintext key — server เก็บแค่ SHA256 hash ไว้ ไม่เห็น plaintext อีก
- key จะโชว์ ครั้งเดียว ตอนสร้าง
Errors
401 Missing header
{
"error": {
"code": "unauthorized",
"message": "missing Authorization: Bearer <key> header",
"docs_url": "https://developers.smongrot.com/docs/errors"
}
}
แก้: เช็คว่าใส่ Authorization: Bearer sk_live_... (มีคำว่า Bearer space นำหน้า)
401 Malformed key
สาเหตุ: format key ผิด — ต้องเป็น sk_live_ + 64 hex chars
401 Unknown key
สาเหตุ: key ไม่มีใน DB — อาจถูก revoke หรือพิมพ์ผิด
แก้: เช็คใน Dashboard ว่า key ยังอยู่และ is_active
401 Key revoked
สาเหตุ: key ถูก revoke ผ่าน Dashboard
แก้: สร้าง key ใหม่
Security best practices
- อย่า commit key เข้า git — ใช้
.env+.gitignore - แยก key ต่อ environment (dev/staging/production) — เวลา leak หรือหมุน key ง่าย
- ตั้งชื่อ key ให้จำได้ (
MyApp-Prod,LocalDev-2026) - Revoke key ทันทีถ้าสงสัยว่าหลุด — ไม่มี grace period
- อย่าใช้ key จาก browser client (public) — key เห็นได้ด้วย DevTools → ใช้เฉพาะ server-side
Rotation
ตอนนี้ยังไม่มี rotate endpoint (planned สำหรับ v1.1). วิธี rotate ตอนนี้:
- สร้าง key ใหม่ใน Dashboard
- Deploy ให้ใช้ key ใหม่
- Revoke key เก่า
Zero downtime rotation ทำได้เพราะ key ทั้ง 2 ใช้งานได้พร้อมกันจนกว่าจะ revoke