📖 API Reference v2.9.0
APIOTA — REST API Reference

APIOTA ให้บริการ REST API สำหรับจัดการ OTA updates ของ ESP32 อย่างครบวงจร ทุก request ส่งไปที่ base URL ด้านล่าง และรับ response เป็น JSON

Base URL
https://apiota.net

Request ทุกตัวต้องมี Content-Type: application/json สำหรับ POST/PUT body ส่วน GET ส่ง parameters เป็น query string

ℹ️ Note: API v2.9.0 — authentication ใช้ JWT Bearer tokens สำหรับ user endpoints และ Device Secret สำหรับ device endpoints

Authentication

ESP32 ยืนยันตัวตนด้วย device_id และ device_secret ที่ได้รับตอน register ส่วน Admin API ใช้ pk_ provisioning keys

Device Authentication

ใส่ device_id และ device_secret เป็น query parameter หรือ request body ในทุก API call

// ตัวอย่าง ESP32 Arduino
String url = "https://apiota.net/api/check-update";
url += "?device_id=" + String(DEVICE_ID);
url += "&version=" + String(CURRENT_VERSION);
url += "&device_secret=" + String(DEVICE_SECRET);
Provisioning Keys (pk_)

สำหรับ register อุปกรณ์ใหม่อัตโนมัติ ใช้ pk_ key ที่สร้างจาก Dashboard

// Register device ด้วย pk_ key
POST /api/register
{
  "device_id":     "esp32-prod-001",
  "device_secret": "your_device_secret",
  "pk_key":        "pk_live_xxxxxxxxxxxx",
  "firmware_ver":  "1.0.0"
}

API Endpoints

Endpoints ทั้งหมดที่ให้บริการ แบ่งตามหมวดหมู่

System
GET /health ตรวจสอบสถานะ server

ตรวจสอบว่า server ทำงานปกติ ไม่ต้องการ authentication

Response 200
{
  "status": "ok",
  "version": "1.0.1",
  "uptime": 86400
}
OTA Updates
GET /api/check-update ตรวจสอบ firmware ใหม่

อุปกรณ์ใช้ endpoint นี้เพื่อตรวจว่ามี firmware version ใหม่หรือไม่ ESP32 ควร poll ทุก 5–15 นาที

Query Parameters
ParameterTypeRequiredDescription
device_idstringrequiredDevice ID ที่ลงทะเบียนไว้
versionstringrequiredFirmware version ปัจจุบัน เช่น 1.0.9
device_secretstringrequiredSecret key ของอุปกรณ์
Response
// มี update ใหม่
{
  "update": true,
  "version": "1.1.0",
  "url": "https://apiota.net/api/firmware/fw-1.1.0.bin",
  "sha256": "a3f8b2c1...",
  "size": 524288,
  "channel": "stable"
}

// ไม่มี update
{ "update": false }
POST /api/firmware/upload อัปโหลด firmware

อัปโหลดไฟล์ .bin firmware ส่งเป็น multipart/form-data

Form Data Parameters
FieldTypeRequiredDescription
filefile (.bin)requiredไฟล์ firmware .bin
versionstringrequiredVersion string เช่น 1.2.0
channelstringoptionalstable / beta / dev (default: stable)
project_idstringoptionalโปรเจกต์ที่เชื่อมโยง
# curl ตัวอย่าง
curl -X POST https://apiota.net/api/firmware/upload \
  -F "file=@firmware-1.2.0.bin" \
  -F "version=1.2.0" \
  -F "channel=stable"
GET /api/firmware/:filename ดาวน์โหลด firmware

ดาวน์โหลดไฟล์ firmware binary โดยตรง ESP32 ใช้ URL นี้ใน OTA update process

Path Parameters
ParameterTypeDescription
filenamestringชื่อไฟล์ .bin ที่ได้รับจาก check-update
GET /api/firmware/fw-1.1.0-stable.bin
→ Returns: application/octet-stream
Devices
POST /api/register ลงทะเบียนอุปกรณ์

ลงทะเบียนอุปกรณ์ใหม่ด้วย provisioning key ใช้ใน ESP32 ตอน boot ครั้งแรก

Request Body (JSON)
FieldTypeRequiredDescription
device_idstringrequiredDevice ID ไม่ซ้ำกัน
device_secretstringrequiredSecret key ที่กำหนดเอง
pk_keystringrequiredProvisioning key (pk_live_...)
firmware_verstringoptionalFirmware version ปัจจุบัน
{
  "success": true,
  "device_id": "esp32-prod-001",
  "registered_at": "2026-05-17T10:30:00Z"
}
Logging
POST /api/ota-log บันทึก OTA status

ESP32 ส่ง status update ระหว่าง OTA process มี 8 สถานะ: pending → checking → downloading → verifying → flashing → rebooting → done / failed

Request Body (JSON)
FieldTypeRequiredDescription
device_idstringrequired
device_secretstringrequired
statusstringrequiredOTA status ปัจจุบัน
from_versionstringoptionalVersion เดิม
to_versionstringoptionalVersion ใหม่
messagestringoptionalข้อความเพิ่มเติม / error message
Remote Config
GET /api/config ดึง remote config

ดึง JSON config ที่ตั้งค่าไว้สำหรับอุปกรณ์นั้น

Query Parameters
ParameterTypeRequiredDescription
device_idstringrequired
device_secretstringrequired
{
  "config": {
    "interval": 5000,
    "log_level": "info",
    "retry_count": 3
  },
  "updated_at": "2026-05-17T08:00:00Z"
}

ESP32 Integration

ตัวอย่าง code สำหรับ Arduino / ESP-IDF เพื่อ integrate กับ APIOTA

Device Configuration
// config.h — ตั้งค่าหลัก
#define DEVICE_ID       "esp32-prod-001"
#define DEVICE_SECRET   "your_device_secret"
#define CURRENT_VERSION "1.0.9"
#define OTA_SERVER      "https://apiota.net"
#define OTA_INTERVAL    300000  // 5 นาที (ms)
OTA Check Loop
void checkOTA() {
  HTTPClient http;
  String url = String(OTA_SERVER) + "/api/check-update";
  url += "?device_id="     + String(DEVICE_ID);
  url += "&version="       + String(CURRENT_VERSION);
  url += "&device_secret=" + String(DEVICE_SECRET);

  http.begin(url);
  int code = http.GET();

  if (code == 200) {
    DynamicJsonDocument doc(1024);
    deserializeJson(doc, http.getString());

    if (doc["update"] == true) {
      String fwUrl = doc["url"];
      sendOTALog("downloading", doc["version"]);
      performOTA(fwUrl);
    }
  }
  http.end();
}
Sending OTA Logs
void sendOTALog(String status, String toVer = "") {
  HTTPClient http;
  http.begin(String(OTA_SERVER) + "/api/ota-log");
  http.addHeader("Content-Type", "application/json");

  DynamicJsonDocument body(256);
  body["device_id"]     = DEVICE_ID;
  body["device_secret"] = DEVICE_SECRET;
  body["status"]        = status;
  body["from_version"]  = CURRENT_VERSION;
  if (toVer.length() > 0) body["to_version"] = toVer;

  String out;
  serializeJson(body, out);
  http.POST(out);
  http.end();
}
⚠️ ใช้ WiFiClientSecure และตรวจสอบ SHA256 ของ firmware ก่อน flash เสมอ เพื่อป้องกัน firmware ปลอม

Error Codes

API ใช้ HTTP status codes มาตรฐาน response body มี error field อธิบายปัญหา

HTTP CodeErrorDescription
200OKสำเร็จ
400Bad Requestขาด parameter หรือ format ไม่ถูกต้อง
401Unauthorizeddevice_secret ไม่ถูกต้อง หรือ device_id ไม่พบ
403Forbiddenเกินขีดจำกัดของแผน หรืออุปกรณ์ถูกบล็อก
404Not Foundไม่พบ resource ที่ขอ
413Payload Too Largeไฟล์ firmware ใหญ่เกิน limit ของแผน
429Rate Limitedส่ง request เร็วเกินไป รอ 60 วินาทีแล้วลองใหม่
500Server ErrorServer error ติดต่อ support
// ตัวอย่าง Error response
{
  "error": "DEVICE_NOT_FOUND",
  "message": "device_id not registered",
  "status": 401
}

Changelog
v2.9.0 2026-05-19 current stable

รายละเอียด release ทุก version รวมถึงข้อมูลสำหรับ rollback firmware เก็บไว้ในหน้า Internal Changelog (หลังบ้าน)

📋 ดู Internal Changelog →