How to Use API Guide

คู่มือการใช้งาน API360 Web API

Authentication Guide (วิธีใช้งาน API ที่ต้อง Login)
  1. สร้าง API Key ผ่านหน้า Dashboard หรือหน้า /keys/create
  2. เมื่อสร้างสำเร็จ จะได้รับ API Key กลับมา (แสดงเพียงครั้งเดียว)
  3. การยืนยันตัวตน: แนบ header X-API-KEY: <API_KEY> ในทุกคำขอ
  4. ถ้าไม่แนบข้อมูลยืนยันตัวตน หรือข้อมูลไม่ถูกต้อง/หมดอายุ จะได้รับ
    401 Unauthorized

💡 วิธีขอ API Key

  1. เข้าสู่ระบบผ่านหน้า /login
  2. ไปที่หน้า Dashboard (หน้าแรก)
  3. คลิกปุ่ม "สร้าง API Key ใหม่"
  4. กรอกข้อมูลและเลือกสิทธิ์การเข้าถึง (Read, Insert, Update, Delete)
  5. คัดลอก API Key ทันที เพราะจะแสดงเพียงครั้งเดียว

ตัวอย่างการแนบ API Key ใน Header

X-API-KEY: sk_live_your_api_key_here

ตัวอย่าง Error

{
  "success": false,
  "error": "Unauthorized",
  "message": "Invalid or missing API key"
}
Products API Guide (วิธีใช้งาน API ข้อมูลสินค้า)

GET /api/v1/products - ดึงรายการสินค้าทั้งหมด

  1. เรียก GET /api/v1/products เพื่อดึงข้อมูลสินค้าทั้งหมด
  2. API จะส่งข้อมูลกลับมาในรูปแบบ JSON
  3. รองรับ Query Parameters: limit (จำนวนต่อหน้า), offset (จำนวนที่ข้าม)
  4. ต้องมีสิทธิ์
    Read

ตัวอย่างการเรียก Products API

GET https://webapi360.trirex.cloud/api/v1/products?limit=10&offset=0
X-API-KEY: sk_live_your_api_key_here

ตัวอย่าง Response

{
  "success": true,
  "message": "Products retrieved successfully",
  "data": [
    {
      "id": 1,
      "name": "iPhone 15 Pro",
      "price": 45900,
      "description": "สมาร์ทโฟนรุ่นล่าสุด",
      "createdAt": "2024-01-01T00:00:00.000Z",
      "updatedAt": "2024-01-01T00:00:00.000Z"
    }
  ],
  "pagination": {
    "total": 100,
    "limit": 10,
    "offset": 0,
    "hasMore": true
  },
  "auditId": 123
}

GET /api/v1/products/:id - ดึงสินค้าตาม ID

  1. เรียก GET /api/v1/products/:id เพื่อดึงข้อมูลสินค้าตาม ID
  2. ต้องมีสิทธิ์
    Read

ตัวอย่างการเรียก

GET https://webapi360.trirex.cloud/api/v1/products/1
X-API-KEY: sk_live_your_api_key_here

POST /api/v1/products - สร้างสินค้าใหม่

  1. เรียก POST /api/v1/products เพื่อสร้างสินค้าใหม่
  2. ต้องแนบ API Key ใน header: X-API-KEY: <API_KEY>
  3. ส่งข้อมูลในรูปแบบ JSON ประกอบด้วย:
    • name - ชื่อสินค้า (บังคับ)
    • price - ราคา (บังคับ)
    • description - คำอธิบาย (ไม่บังคับ)
  4. ต้องมีสิทธิ์
    Insert

ตัวอย่างการเรียก

POST https://webapi360.trirex.cloud/api/v1/products
X-API-KEY: sk_live_your_api_key_here
Content-Type: application/json

{
  "name": "iPhone 15 Pro Max",
  "price": 49900,
  "description": "สมาร์ทโฟนรุ่นล่าสุด พร้อมกล้อง Pro"
}

ตัวอย่าง Response สำเร็จ

{
  "success": true,
  "message": "Product created successfully",
  "data": {
    "id": 1,
    "name": "iPhone 15 Pro Max",
    "price": 49900,
    "description": "สมาร์ทโฟนรุ่นล่าสุด พร้อมกล้อง Pro",
    "createdAt": "2024-01-01T00:00:00.000Z",
    "updatedAt": "2024-01-01T00:00:00.000Z"
  },
  "auditId": 123
}

ตัวอย่าง Response ไม่สำเร็จ

{
  "success": false,
  "error": "Bad Request",
  "message": "Name and price are required"
}

PUT /api/v1/products/:id - อัพเดทสินค้า

  1. เรียก PUT /api/v1/products/:id เพื่ออัพเดทสินค้า
  2. ต้องมีสิทธิ์
    Update
  3. ส่งข้อมูลที่ต้องการอัพเดทในรูปแบบ JSON (ไม่บังคับทุกฟิลด์)

ตัวอย่างการเรียก

PUT https://webapi360.trirex.cloud/api/v1/products/1
X-API-KEY: sk_live_your_api_key_here
Content-Type: application/json

{
  "name": "iPhone 15 Pro Max (Updated)",
  "price": 47900
}

ตัวอย่าง Response สำเร็จ

{
  "success": true,
  "message": "Product updated successfully",
  "data": {
    "id": 1,
    "name": "iPhone 15 Pro Max (Updated)",
    "price": 47900,
    "description": "สมาร์ทโฟนรุ่นล่าสุด พร้อมกล้อง Pro",
    "createdAt": "2024-01-01T00:00:00.000Z",
    "updatedAt": "2024-01-01T01:00:00.000Z"
  },
  "auditId": 124
}

DELETE /api/v1/products/:id - ลบสินค้า

  1. เรียก DELETE /api/v1/products/:id เพื่อลบสินค้า
  2. ต้องมีสิทธิ์
    Delete

ตัวอย่างการเรียก

DELETE https://webapi360.trirex.cloud/api/v1/products/1
X-API-KEY: sk_live_your_api_key_here

ตัวอย่าง Response สำเร็จ

{
  "success": true,
  "message": "Product deleted successfully",
  "auditId": 125
}
SQL Query API Guide (วิธีใช้งาน API เรียกข้อมูลด้วย SQL)
  1. เรียก POST /api/sqlquery/execute เพื่อรันคำสั่ง SQL
  2. ต้องแนบ API Key ใน header: X-API-KEY: <API_KEY>
  3. ส่งคำสั่ง SQL ในรูปแบบ JSON: {sqlQuery: "SELECT * FROM Products"}
  4. อนุญาตคำสั่ง SELECT, INSERT, UPDATE, DELETE (เพื่อความปลอดภัย)
  5. ระบบจะส่งข้อมูลกลับมาในรูปแบบ JSON

Endpoints ที่มี

  • POST /api/sqlquery/execute - รันคำสั่ง SQL (SELECT, INSERT, UPDATE, DELETE)
  • GET /api/sqlquery/query - รันคำสั่ง SQL แบบ GET (SELECT, INSERT, UPDATE, DELETE)

ตัวอย่างการเรียก SQL Query API

POST /api/sqlquery/execute

POST https://webapi360.trirex.cloud/api/sqlquery/execute
X-API-KEY: sk_live_your_api_key_here
Content-Type: application/json

{
  "sqlQuery": "SELECT * FROM Product"
}

GET /api/sqlquery/query

GET https://webapi360.trirex.cloud/api/sqlquery/query?sqlQuery=SELECT * FROM Product
X-API-KEY: sk_live_your_api_key_here

ตัวอย่าง Response สำเร็จ (SELECT)

{
  "success": true,
  "type": "select",
  "data": [
    {
      "id": 1,
      "name": "iPhone 15 Pro",
      "price": 45900,
      "description": "สมาร์ทโฟนรุ่นล่าสุด"
    }
  ],
  "count": 1,
  "message": "เรียกข้อมูลสำเร็จ",
  "auditId": 123
}

ตัวอย่าง Response สำเร็จ (INSERT/UPDATE/DELETE)

INSERT:

{
  "success": true,
  "type": "insert",
  "rowsAffected": 1,
  "message": "เพิ่มข้อมูลสำเร็จ",
  "auditId": 124
}

UPDATE:

{
  "success": true,
  "type": "update",
  "rowsAffected": 1,
  "message": "อัปเดตข้อมูลสำเร็จ",
  "auditId": 125
}

DELETE:

{
  "success": true,
  "type": "delete",
  "rowsAffected": 1,
  "message": "ลบข้อมูลสำเร็จ",
  "auditId": 126
}

ตัวอย่างคำสั่ง SQL ที่แนะนำ

  • SELECT * FROM Product - ดูข้อมูล Products ทั้งหมด
  • SELECT COUNT(*) as TotalProducts FROM Product - นับจำนวน Products
  • SELECT id, name, price FROM Product WHERE price > 10000 - ดู Products ที่ราคามากกว่า 10000
  • INSERT INTO Product (name, price, description) VALUES ('iPhone 16', 49900, 'สมาร์ทโฟนรุ่นใหม่') - เพิ่มข้อมูล
  • UPDATE Product SET price = 44900 WHERE id = 1 - อัปเดตข้อมูล
  • DELETE FROM Product WHERE id = 999 - ลบข้อมูล

ตัวอย่าง Response Error

{
  "success": false,
  "error": "Forbidden SQL Command",
  "message": "คำสั่ง SQL นี้ไม่ได้รับอนุญาตเพื่อความปลอดภัย",
  "details": "อนุญาตเฉพาะคำสั่ง SELECT, INSERT, UPDATE, DELETE เท่านั้น"
}

🚫 คำสั่ง SQL ที่ไม่ได้รับอนุญาต

  • DROP, TRUNCATE
  • ALTER, CREATE
  • EXEC, EXECUTE

⚠️ สิทธิ์การเข้าถึง

API Key ต้องมีสิทธิ์ที่เหมาะสมสำหรับแต่ละคำสั่ง SQL:

  • SELECT - ต้องมีสิทธิ์
    Read
  • INSERT - ต้องมีสิทธิ์
    Insert
  • UPDATE - ต้องมีสิทธิ์
    Update
  • DELETE - ต้องมีสิทธิ์
    Delete
ตัวอย่างการเรียกใช้งาน (cURL)

GET - ดึงรายการสินค้า

curl -X GET "https://webapi360.trirex.cloud/api/v1/products?limit=10&offset=0" \
  -H "X-API-KEY: YOUR_API_KEY"

POST - สร้างสินค้าใหม่

curl -X POST "https://webapi360.trirex.cloud/api/v1/products" \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "iPhone 15 Pro",
    "price": 45900,
    "description": "สมาร์ทโฟนรุ่นล่าสุด"
  }'

PUT - อัพเดทสินค้า

curl -X PUT "https://webapi360.trirex.cloud/api/v1/products/1" \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "iPhone 15 Pro (Updated)",
    "price": 44900
  }'

DELETE - ลบสินค้า

curl -X DELETE "https://webapi360.trirex.cloud/api/v1/products/1" \
  -H "X-API-KEY: YOUR_API_KEY"

SQL Query API

SELECT - ดึงข้อมูลด้วย SQL

curl -X POST "https://webapi360.trirex.cloud/api/sqlquery/execute" \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "sqlQuery": "SELECT * FROM Product"
  }'

INSERT - เพิ่มข้อมูลด้วย SQL

curl -X POST "https://webapi360.trirex.cloud/api/sqlquery/execute" \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "sqlQuery": "INSERT INTO Product (name, price, description) VALUES ('iPhone 16', 49900, 'สมาร์ทโฟนรุ่นใหม่')"
  }'

UPDATE - อัพเดทข้อมูลด้วย SQL

curl -X POST "https://webapi360.trirex.cloud/api/sqlquery/execute" \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "sqlQuery": "UPDATE Product SET price = 44900 WHERE id = 1"
  }'

DELETE - ลบข้อมูลด้วย SQL

curl -X POST "https://webapi360.trirex.cloud/api/sqlquery/execute" \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "sqlQuery": "DELETE FROM Product WHERE id = 999"
  }'
หมายเหตุสำคัญ

⚠️ สิทธิ์การเข้าถึง

API Key ต้องมีสิทธิ์ที่เหมาะสมสำหรับแต่ละ endpoint:

  • GET - ต้องมีสิทธิ์
    Read
  • POST - ต้องมีสิทธิ์
    Insert
  • PUT - ต้องมีสิทธิ์
    Update
  • DELETE - ต้องมีสิทธิ์
    Delete

ℹ️ Audit Logs

ทุกการเรียก API จะถูกบันทึกใน Audit Logs อัตโนมัติ เพื่อความปลอดภัยและการตรวจสอบ

🚫 Rate Limiting

ระบบจำกัดการเรียก API ที่ 100 requests ต่อนาทีต่อ IP Address