เริ่มต้นใน 3 ขั้นตอน
{{BASE}}/api
- รับ API Key จากผู้ดูแลระบบ
- แนบ header
X-API-Keyไปกับทุก request - ส่ง JSON ไปที่ endpoint ทุกคำตอบกลับมาเป็น JSON
curl -X POST {{BASE}}/api/sms/send \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"recipients":["0812345678"],"message":"สวัสดีครับ"}'
const res = await fetch("{{BASE}}/api/sms/send", {
method: "POST",
headers: { "X-API-Key": process.env.SMS_API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ recipients: ["0812345678"], message: "สวัสดีครับ" }),
});
const json = await res.json();
if (!json.success) throw new Error(json.error.message);
console.log(json.data.uuid);
$ch = curl_init("{{BASE}}/api/sms/send");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ["X-API-Key: " . getenv("SMS_API_KEY"), "Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode(["recipients" => ["0812345678"], "message" => "สวัสดีครับ"]),
]);
$json = json_decode(curl_exec($ch), true);
if (!$json["success"]) throw new Exception($json["error"]["message"]);
echo $json["data"]["uuid"];
import os, requests
res = requests.post(
"{{BASE}}/api/sms/send",
headers={"X-API-Key": os.environ["SMS_API_KEY"]},
json={"recipients": ["0812345678"], "message": "สวัสดีครับ"},
timeout=20,
)
data = res.json()
if not data["success"]:
raise RuntimeError(data["error"]["message"])
print(data["data"]["uuid"])
การยืนยันตัวตน
ทุก request ที่ขึ้นต้นด้วย /api ต้องมี header นี้:
X-API-Key: YOUR_API_KEY
เก็บ API Key ไว้ฝั่ง server ของคุณเท่านั้น อย่าใส่ในแอปมือถือหรือโค้ดหน้าเว็บที่ผู้ใช้เปิดดูได้
ทุกคำตอบอยู่ในรูปแบบเดียวกัน:
// สำเร็จ
{ "success": true, "data": { ... } }
// ผิดพลาด
{ "success": false, "error": { "code": "...", "message": "...", "details": [...] } }
รูปแบบเบอร์โทร
รองรับเบอร์มือถือไทยทุกรูปแบบ ระบบแปลงเป็น 66xxxxxxxxx ให้อัตโนมัติ และตัดเบอร์ซ้ำออก
| ส่งมาแบบนี้ | ระบบใช้ |
|---|---|
0812345678 | 66812345678 |
081-234-5678 | 66812345678 |
+66 81 234 5678 | 66812345678 |
021234567 (เบอร์บ้าน) | ไม่ส่ง — แจ้งใน invalidRecipients |
POST /api/sms/send
ส่งข้อความถึงหนึ่งหรือหลายเบอร์ หรือตั้งเวลาส่งล่วงหน้า
| ฟิลด์ | ชนิด | คำอธิบาย |
|---|---|---|
recipients | string[] | เบอร์ผู้รับ 1–10,000 เบอร์ |
message | string | ข้อความ ไม่เกิน 1,000 ตัวอักษร |
origin | string? | ชื่อผู้ส่ง (ไม่ใส่ = ใช้ค่าเริ่มต้นของระบบ) |
scheduledTime | string? | เวลาส่ง ISO 8601 เช่น 2026-12-24T09:00:00+07:00 (ไม่ใส่ = ส่งทันที) |
variables | object? | ค่าตัวแปรรายเบอร์สำหรับข้อความเฉพาะบุคคล |
{
"recipients": ["0812345678", "0898765432"],
"message": "โปรโมชันพิเศษ ลด 10% วันนี้",
"scheduledTime": null
}
201 Created
{
"success": true,
"data": {
"uuid": "9f8b2c1e-...",
"refNo": "SP240115...",
"totalMsisdn": 2,
"totalCredit": 2,
"status": "pending",
"invalidRecipients": [],
"estimate": { "encoding": "UCS-2", "length": 27, "segments": 1 }
}
}
เก็บ uuid ไว้ใช้เช็คสถานะภายหลัง
GET /api/sms/{uuid}
สรุปสถานะของการส่งครั้งนั้น
curl {{BASE}}/api/sms/9f8b2c1e-... \
-H "X-API-Key: YOUR_API_KEY"
{
"success": true,
"data": {
"uuid": "9f8b2c1e-...",
"status": "completed",
"totalMsisdn": 2,
"drSummary": [{ "drStatus": "success", "count": 2 }],
"refundedCount": 0
}
}
GET /api/sms/{uuid}/recipients
สถานะทีละเบอร์ ครั้งละไม่เกิน 50 แถว ถ้าส่งเยอะให้วนอ่านด้วย after = pagination.nextCursor จนกว่า hasMore เป็น false
| Query | คำอธิบาย |
|---|---|
after | cursor จากหน้าก่อน |
page | หน้าที่ (เริ่ม 1) ถ้าไม่ใช้ cursor |
limit | 1–50 (ค่าเริ่มต้น 50) |
status | pending success failed expired blocked invalid timeout cancelled |
search | ค้นด้วยบางส่วนของเบอร์ (ตัวเลข 1–12 หลัก) |
{
"success": true,
"data": {
"items": [{ "destination": "66812345678", "drStatus": "success", "drStatusAt": "..." }],
"pagination": { "limit": 50, "total": 1250, "nextCursor": "3c7f...", "hasMore": true },
"summary": [{ "drStatus": "success", "count": 1180 }, { "drStatus": "failed", "count": 70 }]
}
}
POST /api/sms/estimate
คำนวณว่าข้อความจะใช้กี่ SMS ต่อเบอร์ (= เครดิตต่อเบอร์) ก่อนส่งจริง
| ข้อความ | 1 SMS | ยาวกว่านั้น (ต่อ SMS) |
|---|---|---|
| มีภาษาไทย / emoji (UCS-2) | 70 ตัวอักษร | 67 ตัวอักษร |
| อังกฤษล้วน (GSM-7) | 160 ตัวอักษร | 153 ตัวอักษร |
{ "message": "สวัสดีครับ" }
{ "success": true, "data": { "encoding": "UCS-2", "length": 10, "segments": 1 } }
POST /api/otp/send
ระบบสร้างรหัสและส่ง SMS ให้ คำตอบจะ ไม่มีรหัส มีแค่ reference ไว้ใช้ตรวจ
| ฟิลด์ | ชนิด | คำอธิบาย |
|---|---|---|
phone | string | เบอร์ผู้รับ |
template | string? | ต้องมี {code} ใส่ {ref} ได้ เช่น รหัสของคุณคือ {code} (Ref: {ref}) |
ttlSeconds | number? | อายุรหัส 60–600 วินาที (ค่าเริ่มต้น 300) |
codeLength | number? | ความยาวรหัส 4–8 หลัก |
{ "phone": "0812345678" }
201 Created
{
"success": true,
"data": {
"reference": "A3K9X2",
"destination": "66812345678",
"expiresAt": "2026-01-15T10:35:00.000Z",
"ttlSeconds": 300
}
}
POST /api/otp/verify
ตรวจรหัสที่ผู้ใช้กรอก โดยใช้ reference จากตอนส่ง
{ "reference": "A3K9X2", "pin": "123456" }
{ "success": true, "data": { "reference": "A3K9X2", "verified": true } }
ขั้นตอนที่แนะนำ: ส่ง OTP → เก็บ reference ไว้ใน session ของผู้ใช้ → ผู้ใช้กรอกรหัส → เรียก verify → ถ้า verified เป็น true จึงให้ผ่าน
GET /api/credit · GET /api/senders
GET /api/credit
{ "success": true, "data": { "balance": 18420 } }
GET /api/senders
{ "success": true, "data": [
{ "origin": "MYSHOP", "status": "approved",
"privilege": "private", "isOtpOnly": false }
] }
ใช้ค่า origin ที่ status เป็น approved เป็นชื่อผู้ส่ง
ข้อผิดพลาด
ให้ตรวจ error.code ในโค้ดของคุณ ข้อความใน message มีไว้ให้คนอ่าน และอาจเปลี่ยนได้
| HTTP | error.code | ความหมาย / ควรทำอะไร |
|---|---|---|
| 400 | validation.failed | ข้อมูลไม่ถูกต้อง — ดูรายละเอียดใน details |
| 400 | validation.bad_json | JSON ผิดรูปแบบ |
| 400 | validation.no_valid_recipients | ไม่มีเบอร์มือถือไทยที่ใช้ได้เลย |
| 400 | validation.invalid_phone | เบอร์ OTP ไม่ถูกต้อง |
| 401 | auth.invalid_credentials | ไม่มี API Key หรือไม่ถูกต้อง |
| 429 | rate_limited | เรียกถี่เกินไป — รอตาม header Retry-After (วินาที) แล้วลองใหม่ |
| 502 | (ต่างกันไป) | ผู้ให้บริการ SMS ปฏิเสธ เช่น เครดิตไม่พอ — แจ้งผู้ดูแลระบบ |
| 504 | provider.timeout / provider.unreachable | ติดต่อผู้ให้บริการไม่ได้ชั่วคราว — ลองใหม่ภายหลัง |
ถ้าได้ 504 ตอนส่ง SMS อย่าส่งซ้ำทันที เพราะข้อความอาจถูกส่งออกไปแล้ว ให้เช็คเครดิตหรือสอบถามผู้ดูแลก่อน
ข้อจำกัด
เรียก /api รวมทุก endpoint | 120 ครั้ง/นาที ต่อ IP |
| ส่ง OTP | 5 ครั้ง/นาที ต่อ IP (ข้อจำกัดของผู้ให้บริการ) |
| ผู้รับต่อ 1 request | 10,000 เบอร์ |
| ความยาวข้อความ | 1,000 ตัวอักษร |
| ขนาด request | 1 MB |
ทุกคำตอบของ /api มี header RateLimit-Remaining บอกว่าในนาทีนี้เหลือกี่ครั้ง
