กำลังโหลด...
กำลังโหลด...
EduSync Pro — ระบบจัดตารางสอนอัจฉริยะสำหรับสถานศึกษาไทย
คู่มือนี้อธิบาย REST API endpoints ทั้งหมดที่พร้อมใช้งาน
API ที่มีสัญลักษณ์ ต้องยืนยันตัวตนผ่าน session cookie __Host-pv_session (ได้จากการ login — ดูรายละเอียดด้านล่าง)
ใช้ custom session-based authentication ด้วย CSRF double-submit cookie pattern และ session cookie __Host-pv_session สำหรับคำขอที่ต้องยืนยันตัวตน
GET /auth/login
Server ตอบ JSON: { csrfToken: "..." } และตั้ง cookie csrf_token (httpOnly, SameSite=strict, อายุ 15 นาที)
POST /auth/login Content-Type: application/x-www-form-urlencoded x-csrf-token: <token from step 1> [email protected]&password=secret
CSRF token ต้องตรงกับค่าใน cookie (double-submit pattern) — Server ตรวจสอบ 3 อย่าง:
x-csrf-token ต้องมีค่าcsrf_tokenCSRF_SIGNING_KEY)เมื่อ login สำเร็จ Server จะ redirect (303) ไปยังหน้าที่ร้องขอ และตั้ง session cookie:
Set-Cookie: __Host-pv_session=<token>; HttpOnly; SameSite=Lax; Secure; Path=/; Max-Age=2592000
Cookie __Host-pv_session ถูกส่งอัตโนมัติในทุก request หลังจากนี้ (SameSite=Lax, Secure ใน production)
Rate Limit: 5 ครั้ง/15 นาที/IP (เฉพาะ request ที่ผ่าน CSRF แล้วเท่านั้น)
Role: SUPER_ADMIN, ADMIN, DEPARTMENT_HEAD, TEACHER, VIEWER — สิทธิ์การเข้าถึงถูกกำหนดตามบทบาท
ตรวจสอบสถานะการทำงานของระบบและฐานข้อมูล
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| GET | /api/health | ตรวจสอบสถานะ API, การเชื่อมต่อฐานข้อมูล, ปีการศึกษาและภาคเรียนที่ใช้งานอยู่ Response: { status, database, activeAcademicYear, activeTerm, checkedAt } | สาธารณะ |
ระบบลงทะเบียนและเข้าสู่ระบบ ใช้ custom session-based authentication ผ่าน CSRF double-submit cookie pattern และ session cookie โดยตรง
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| GET | /auth/login | สร้าง CSRF token สำหรับ login — ส่ง JSON { csrfToken } และตั้ง cookie csrf_token (httpOnly, SameSite=strict, อายุ 15 นาที) Double-submit cookie pattern: client เก็บ csrfToken จาก JSON response แล้วส่งกลับเป็น header x-csrf-token ใน POST. Token มีลายเซ็น HMAC จาก CSRF_SIGNING_KEY. Cookie ตั้งแบบ httpOnly + SameSite=strict เพื่อป้องกัน XSS และ cross-origin access. | สาธารณะ |
| POST | /auth/login | เข้าสู่ระบบด้วย email และ password (urlencoded) — ต้องส่ง x-csrf-token header ที่ได้จาก GET /auth/login Rate limit: 5 ครั้ง/15 นาที/IP. Header: x-csrf-token (required). Body: email, password (urlencoded). สำเร็จ: 303 redirect + Set-Cookie: __Host-pv_session. ล้มเหลว: 303 redirect กลับ /login พร้อม error query param. | สาธารณะ |
| POST | /api/auth/register | ลงทะเบียนองค์กรใหม่ (self-service) — สร้าง OrganizationProfile, OrganizationPlan (free tier) และ SUPER_ADMIN ใน transaction เดียว Body: { organizationName, organizationNameEnglish?, email, password, confirmPassword, displayName }. Rate limit: 10 req/hr/IP. Validates email format, password ≥8 chars, matching confirmPassword. Returns 201 on success, 409 if email already exists. | สาธารณะ |
API สำหรับจัดการข้อมูลหลักของระบบ เช่น ครูผู้สอน, วิชา, ห้องเรียน, กลุ่มเรียน — จัดการผ่าน Server Actions ของ Next.js ไม่ใช้ REST API แยก
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| POST | /api/integrations/sis/import | นำเข้าข้อมูลจากระบบ SIS (Student Information System) — รองรับ entityType: TEACHER, ROOM Requires: settings:write permission. Body: { entityType: 'TEACHER'|'ROOM', records: [...] }. Returns: { ok, data: { jobId, source, entityType, status, summary } } | ใช่ |
| POST | /api/integrations/hr/import | นำเข้าข้อมูลจากระบบ HR — รองรับเฉพาะ entityType: TEACHER เท่านั้น Requires: settings:write permission. Body: { entityType: 'TEACHER', records: [...] }. Returns: { ok, data: { jobId, source, entityType, status, summary } } | ใช่ |
API สำหรับดู, ส่งออก, และติดตามตารางสอนแบบเรียลไทม์
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| GET | /api/schedule/live | Server-Sent Events (SSE) stream สำหรับติดตามการเปลี่ยนแปลงตารางสอนแบบเรียลไทม์ Requires: schedule:view permission. Content-Type: text/event-stream. Events: 'ready' (initial state), 'schedule-update' (changes), 'heartbeat' (every 15s). Use EventSource() on client to connect. | ใช่ |
| GET | /api/schedule/export/csv?runId=...&view=... | ส่งออกตารางสอนเป็นไฟล์ CSV — รองรับมุมมอง teacher, group, room Requires: schedule:view permission. Query params: runId (required), view (teacher|group|room, default: teacher), teacherId?, groupId?, roomId?, subjectId?, includeSubstitutions=1?. Returns: CSV file download (UTF-8 with BOM). | ใช่ |
| GET | /api/schedule/[runId]/ical | ส่งออกตารางสอนเป็นไฟล์ iCalendar (.ics) สำหรับ Google Calendar, Apple Calendar, Outlook RFC 5545 compliant. Each slot exported as a weekly recurring VEVENT. Timezone: Asia/Bangkok. Compatible with any calendar client. | สาธารณะ |
บันทึกและดึงข้อมูลการเข้าสอนของครู
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| GET | /api/attendance?dateKey=YYYY-MM-DD | ดึงข้อมูลการเข้าสอนทั้งหมดในวันที่ระบุ Requires: attendance:view permission. Returns: { records: Record<slotId, 'present'|'absent'|'late'> } | ใช่ |
| POST | /api/attendance | บันทึก/อัปเดต/ลบการเข้าสอน (upsert) Requires: schedule:write permission. Rate limit: 30 req/min/IP. Body: { slotId, teacherId, dateKey, status: 'present'|'absent'|'late'|null }. If status is null → deletes the record. Otherwise upserts. | ใช่ |
ดาวน์โหลดรายงานในรูปแบบ CSV
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| GET | /api/reports/workload?runId=... | รายงานภาระสอนครู — แสดงคาบสอนรวมแยกตามวัน (จ.–อา.) ต่อครู Requires: schedule:view permission. Columns: รหัสครู, ชื่อ-นามสกุล, แผนก, คาบรวม, จ.–อา. Returns: CSV file download (UTF-8 with BOM for Excel). | ใช่ |
| GET | /api/reports/attendance?from=YYYY-MM-DD&to=YYYY-MM-DD | รายงานการเข้าสอน — สรุปสถานะการเข้าสอนของครูในช่วงวันที่กำหนด Requires: attendance:view permission. Columns: วันที่, คาบ, รหัสครู, ชื่อ-นามสกุล, วิชา, กลุ่ม, ห้อง, สถานะ. Returns: CSV file download (UTF-8 with BOM for Excel). | ใช่ |
API ที่ไม่ต้องยืนยันตัวตน — ใช้สำหรับเผยแพร่ตารางสอนให้บุคคลภายนอกเข้าถึงได้
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| GET | /api/public/schedule | ดึงรายการตารางสอนที่เผยแพร่แล้ว (published schedule runs) Cache: 60s (s-maxage=300, stale-while-revalidate=600). Returns array of published runs. | สาธารณะ |
| GET | /api/public/schedule/[runId] | ดึงรายละเอียดตารางสอนที่เผยแพร่แล้วตาม runId Cache: 60s. Returns 404 if schedule not found or not published. Full slot details included. | สาธารณะ |
ระบบแจ้งเตือนผ่าน LINE Messaging API และ webhooks จากภายนอก
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| POST | /api/webhooks/line | LINE Messaging API Webhook — รับ events จาก LINE OA (follow, message) สำหรับการเชื่อมต่อบัญชี Signature verification: HMAC-SHA256 with LINE_CHANNEL_SECRET. Account linking: user sends 'เชื่อมต่อ <userId>' to link LINE account to EduSync. Env vars required: LINE_CHANNEL_SECRET, LINE_CHANNEL_ACCESS_TOKEN. | สาธารณะ |
อัปโหลดไฟล์และบันทึกกิจกรรมการใช้งาน
| เมธอด | Path | คำอธิบาย | Auth |
|---|---|---|---|
| POST | /api/upload/logo | อัปโหลดโลโก้สถาบัน — รองรับ PNG, JPG, SVG, WEBP (สูงสุด 2MB) Requires: settings:organization permission. Form data field: 'logo'. Returns: { url: '/uploads/logo-xxx.ext' }. File saved to public/uploads/. | ใช่ |
| POST | /api/telemetry | บันทึก app event สำหรับ observability และ analytics Requires: settings:write permission. Rate limit: 30 req/min/IP. Body: { name, path, detail }. Records to AppEvent table via recordAppEvent(). | ใช่ |
API บาง endpoint มีการจำกัดจำนวนคำขอ (Rate Limiting) เพื่อป้องกันการใช้งานเกินควร:
เมื่อเกิดข้อผิดพลาด API จะส่ง JSON response กลับมาในรูปแบบ:
{
"error": "ข้อความอธิบายข้อผิดพลาด",
"errors": { // (optional) field-level errors
"fieldName": "ข้อความเฉพาะ field"
}
}
// HTTP Status Codes:
// 400 — Bad Request (ข้อมูลไม่ถูกต้อง)
// 401 — Unauthorized (ไม่ได้ยืนยันตัวตน)
// 403 — Forbidden (ไม่มีสิทธิ์เข้าถึง)
// 404 — Not Found (ไม่พบข้อมูล)
// 409 — Conflict (ข้อมูลซ้ำ)
// 429 — Too Many Requests (เกิน rate limit)
// 500 — Internal Server Error (ข้อผิดพลาดภายใน)EduSync Pro API Documentation · อัปเดตล่าสุด: พฤษภาคม 2026
สร้างโดย EduSync Pro — ระบบจัดตารางสอนอัจฉริยะสำหรับสถานศึกษาไทย