KAIMAIL API
ภาพรวม
- Base URL:
https://api.kaimail.shop/v1 - OpenAPI 3.1: https://api.kaimail.shop/v1/openapi.json นำเข้า Postman / Insomnia / Swagger Editor ได้ทันที หรือใช้สร้าง client อัตโนมัติ (เช่น openapi-generator)
- รับส่งเป็น JSON (UTF-8) ทุก endpoint ยกเว้นดาวน์โหลดบัญชีแบบ
format=txt - เงินทุกช่องเป็น สตางค์ (จำนวนเต็ม) เช่น
6700= ฿67.00 ; เวลาเป็น ISO-8601 แบบ UTC - สำเร็จ:
{ "data": ... }; ผิดพลาด:{ "error": { "code", "message", "details" } }พร้อม HTTP status ตามความหมาย - ทุกคำตอบมี
X-Request-Idส่งค่านี้ให้ทีมงานเมื่อต้องการให้ตรวจสอบ - ราคาเท่ากับหน้าเว็บ รวมราคาแพ็กตามจำนวนและส่วนลดตัวแทน ; เติมเงินผ่านหน้าเว็บหรือ LINE ตามปกติ
เริ่มต้นใช้งาน
- เข้าสู่ระบบ แล้วไปที่ บัญชี → API สำหรับนักพัฒนา
- กดขอใช้ API แล้วรอทีมงานอนุมัติ
- สร้าง API key เลือกสิทธิ์ (อ่านอย่างเดียว หรืออนุญาตให้ซื้อ) ตั้ง IP เซิร์ฟเวอร์และเพดานซื้อต่อวันได้
- คัดลอก key เก็บไว้ทันที (แสดงครั้งเดียว) แล้วเรียก
GET /meเพื่อทดสอบ
การยืนยันตัวตนและสิทธิ์
ส่ง key ใน header ทุกคำขอ:
Authorization: Bearer km_live_xxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx- read ทุก key มี: ดูสินค้า ยอดเงิน ออเดอร์ ข้อมูลบัญชีที่ซื้อ และอ่านเมล/OTP
- buy เลือกตอนสร้าง key: สั่งซื้อด้วยยอดเงินในบัญชี
- จำกัด IP: key ใช้ได้เฉพาะจาก IP/ช่วง IPv4 ที่กำหนด (แนะนำสำหรับ key ที่ซื้อได้)
- เพดานซื้อต่อ 24 ชม.: ยอดซื้อรวมของ key ใน 24 ชม.ล่าสุดเกินนี้จะถูกปฏิเสธ
- ใช้จากเซิร์ฟเวอร์เท่านั้น ห้ามใส่ key ในหน้าเว็บหรือแอปฝั่งผู้ใช้ ถ้าคิดว่า key หลุด ให้ยกเลิกที่หน้า API ทันที
รายการ endpoint
| method | path | สิทธิ์ | ใช้ทำอะไร |
|---|---|---|---|
| GET | /me | read | ยอดเงิน ระดับตัวแทน และข้อมูลของ key ที่ใช้ |
| GET | /wallet/transactions | read | รายการเงินเข้าออก (limit, cursor) |
| GET | /products | read | สินค้าที่เปิดขาย ราคาสำหรับบัญชีคุณ จำนวนคงเหลือ |
| GET | /products/{id}?qty= | read | สินค้าหนึ่งตัว พร้อมราคาของจำนวนที่ระบุ (ราคาแพ็ก) |
| POST | /orders | buy | ซื้อสินค้าด้วยยอดเงิน (ต้องมี Idempotency-Key) |
| GET | /orders | read | ออเดอร์ของคุณ (status, limit, cursor) |
| GET | /orders/{code} | read | สถานะออเดอร์ เช่น KM-01234 |
| GET | /orders/{code}/items | read | ข้อมูลบัญชีที่ได้รับ (json หรือ txt) |
| GET | /mailboxes/{email}/otp | read | OTP ล่าสุดในกล่องจดหมายของอีเมลที่คุณซื้อ (รวมเมลสำรองถ้าสินค้ามี) |
| GET | /mailboxes/{email}/messages | read | จดหมายล่าสุด 30 ฉบับ |
| GET | /mailboxes/{email}/messages/{id} | read | เนื้อหาเต็มของจดหมาย |
รายการที่แบ่งหน้าคืน next_cursor ส่งกลับมาเป็น ?cursor= เพื่อดูหน้าถัดไป (null = หมดแล้ว)
ซื้อสินค้า
POST /orders body: product_id (บังคับ), qty (ค่าเริ่มต้น 1), max_unit_price_satang (ไม่บังคับ: ราคาต่อชิ้นสูงสุดที่ยอมจ่าย กันราคาเปลี่ยนระหว่างสั่ง)
- Idempotency-Key (header, บังคับ): ค่าไม่ซ้ำต่อคำสั่งซื้อ เช่นเลขออเดอร์ฝั่งคุณ ถ้าเน็ตหลุดหรือไม่แน่ใจว่าสำเร็จ ส่งซ้ำด้วยค่าเดิมได้เลย จะได้ออเดอร์เดิมกลับมา (
idempotent_replay: true) ไม่ถูกหักเงินซ้ำ ?dry_run=true: ตรวจราคา สต๊อก ยอดเงิน และเพดานต่อวันโดยไม่ซื้อจริง- 201 ได้สินค้าแล้ว ; 202 ชำระแล้วกำลังเตรียมสินค้า ให้ถาม
GET /orders/{code}ทุก 3–5 วินาที ; 200 คำขอซ้ำ (คืนสถานะปัจจุบัน) - ซื้อไม่สำเร็จ (ได้ error) ไม่มีการหักเงิน ส่งใหม่ด้วย Idempotency-Key เดิมได้หลังแก้ปัญหา เช่นเติมเงินแล้ว ; ถ้าออเดอร์ถูกยกเลิกและคืนเงิน (
refunded: true) ให้ซื้อใหม่ด้วย key ใหม่ - สถานะออเดอร์:
deliveredได้ของแล้ว ·processingกำลังเตรียม (ไม่ต้องสั่งซ้ำ) ·cancelledยกเลิก (refunded: true= คืนเงินเข้าบัญชีแล้ว) ·pending_paymentสั่งไว้จากหน้าเว็บแต่ยังไม่ชำระ
HTTP/1.1 201 Created
{
"data": {
"code": "KM-01234",
"status": "delivered",
"product_id": 1,
"product_name": "Hotmail - บัญชีใหม่",
"qty": 100,
"unit_price_satang": 67,
"total_satang": 6700,
"list_price_satang": 69,
"refunded": false,
"client_ref": "shop-order-1001",
"source": "api",
"items_url": "/v1/orders/KM-01234/items",
"created_at": "2026-11-15T10:00:00.000Z",
"updated_at": "2026-11-15T10:00:00.000Z",
"balance_satang": 12000
},
"idempotent_replay": false
}รับข้อมูลบัญชี
GET /orders/{code}/items ใช้ได้เมื่อสถานะเป็น delivered
- JSON (ค่าเริ่มต้น):
items=[{ "email", "password", ... , "claim_code" }]แบ่งหน้าด้วยoffset/limit(สูงสุด 5,000) และnext_offset; มีread_urlลิงก์อ่านเมลแบบหน้าเว็บ ?format=txt: ไฟล์ข้อความบรรทัดละบัญชี ทุกชิ้นในครั้งเดียว ;&sep=pipe|colon|comma|tab(ค่าเริ่มต้น|)- ช่องที่มีขึ้นกับสินค้า ดูได้จาก
fieldsในGET /products
อ่านอีเมลและ OTP
ใช้กับอีเมลที่คุณซื้อและได้รับแล้ว ระบุอีเมลใน path ได้เลย ไม่ต้องจำเลขออเดอร์ (สินค้าที่รองรับมี readmail: true)
GET /mailboxes/{email}/otp?since=2026-11-15T10:00:00Zคืนlatestและotpsใหม่สุดก่อน (รวมจดหมายขยะ) ;since= เอาเฉพาะที่ได้รับหลังเวลานี้ กันได้รหัสเก่าGET /mailboxes/{email}/messagesจดหมายล่าสุด 30 ฉบับ ;/messages/{id}เนื้อหาเต็ม (เข้ารหัส id ด้วย URL encoding)- แนะนำถามทุก 5 วินาที ผลภายในไม่กี่วินาทีเป็นผลเดียวกัน (ระบบจำไว้สั้น ๆ ไม่ดึงจาก Microsoft ซ้ำ)
ข้อผิดพลาด
ตัดสินใจจาก error.code (ภาษาอังกฤษ ไม่เปลี่ยน) ส่วน message เป็นภาษาไทยไว้ให้คนอ่าน
HTTP/1.1 402 Payment Required
{
"error": {
"code": "insufficient_balance",
"message": "ยอดเงินไม่พอ",
"details": { "shortfall_satang": 3500 }
}
}| status | code | ความหมาย |
|---|---|---|
| 400 | invalid_json | body ไม่ใช่ JSON object |
| 401 | unauthorized / invalid_api_key | ไม่ได้ส่ง key หรือ key ผิด/ถูกยกเลิก |
| 402 | insufficient_balance | ยอดเงินไม่พอ (details.shortfall_satang = ขาดอีกเท่าไร) |
| 403 | access_not_approved / access_blocked / api_disabled | บัญชียังไม่ได้รับสิทธิ์ ถูกปิดสิทธิ์ หรือร้านปิด API ชั่วคราว |
| 403 | scope_denied | key ไม่มีสิทธิ์ทำสิ่งนี้ (เช่น key อ่านอย่างเดียวสั่งซื้อ) |
| 403 | ip_not_allowed | IP ที่เรียกไม่อยู่ในรายการของ key |
| 403 | daily_limit_reached | เกินเพดานซื้อต่อ 24 ชม. ที่ตั้งไว้กับ key |
| 404 | product_not_found / order_not_found / mailbox_not_found | ไม่พบ หรือไม่ใช่ของบัญชีคุณ |
| 409 | out_of_stock | สต๊อกไม่พอ (details.available = เหลือเท่าไร) |
| 409 | price_changed | ราคาตอนนี้แพงกว่า max_unit_price_satang ที่ส่งมา |
| 409 | idempotency_mismatch | Idempotency-Key นี้เคยใช้กับสินค้า/จำนวนอื่นแล้ว |
| 409 | not_delivered | ออเดอร์ยังไม่ได้ส่งมอบ ดึงข้อมูลบัญชียังไม่ได้ |
| 422 | validation_error / idempotency_key_required / qty_over_limit | ข้อมูลที่ส่งไม่ถูกต้อง (details.field = ช่องที่ผิด) |
| 422 | mailbox_auth_failed | เข้ากล่องจดหมายนี้ไม่ได้ (บัญชีมีปัญหา ติดต่อทีมงานเพื่อเคลม) |
| 429 | rate_limited / mail_throttled | เรียกถี่เกินไป รอตาม header Retry-After |
| 500 | internal_error | ระบบผิดพลาด ลองใหม่ ถ้ายังไม่ได้แจ้งทีมงานพร้อม request_id |
| 503 | mail_unavailable | อ่านเมลไม่ได้ชั่วคราว ลองใหม่ตาม Retry-After |
เพดานการเรียก
- 120 ครั้งต่อนาทีต่อ key (ทุก endpoint รวมกัน) ดูที่เหลือได้จาก header
RateLimit-RemainingและRateLimit-Reset - สั่งซื้อ 30 ครั้งต่อนาทีต่อ key ; อ่านเมลกล่องเดียวกัน 40 ครั้งต่อนาทีต่อ key
- เกินเพดานได้
429พร้อมRetry-After(วินาที) รอแล้วค่อยเรียกใหม่
ตัวอย่างโค้ด
curl
# ยอดเงินและข้อมูล key
curl https://api.kaimail.shop/v1/me -H "Authorization: Bearer $KAIMAIL_KEY"
# ราคาของสินค้า id 1 จำนวน 100 ชิ้น (ราคาแพ็ก + ส่วนลดตัวแทน)
curl "https://api.kaimail.shop/v1/products/1?qty=100" -H "Authorization: Bearer $KAIMAIL_KEY"
# ซื้อ 100 ชิ้น
curl -X POST https://api.kaimail.shop/v1/orders \
-H "Authorization: Bearer $KAIMAIL_KEY" \
-H "Idempotency-Key: shop-order-1001" \
-H "Content-Type: application/json" \
-d '{"product_id": 1, "qty": 100, "max_unit_price_satang": 70}'
# ดาวน์โหลดบัญชีเป็นไฟล์ email|password
curl "https://api.kaimail.shop/v1/orders/KM-01234/items?format=txt" -H "Authorization: Bearer $KAIMAIL_KEY" -o accounts.txt
# OTP ล่าสุดของอีเมลที่ซื้อ
curl "https://api.kaimail.shop/v1/mailboxes/[email protected]/otp" -H "Authorization: Bearer $KAIMAIL_KEY"Node.js (ซื้อแล้วรับบัญชี)
const BASE = "https://api.kaimail.shop/v1";
const headers = { Authorization: `Bearer ${process.env.KAIMAIL_KEY}`, "Content-Type": "application/json" };
async function buy(productId, qty, myOrderId) {
const res = await fetch(`${BASE}/orders`, {
method: "POST",
headers: { ...headers, "Idempotency-Key": myOrderId }, // ส่งซ้ำด้วยค่าเดิมได้ ไม่หักเงินซ้ำ
body: JSON.stringify({ product_id: productId, qty }),
});
const body = await res.json();
if (!res.ok) throw new Error(`${body.error.code}: ${body.error.message}`);
let order = body.data;
while (order.status === "processing") { // 202 = กำลังเตรียมสินค้า
await new Promise((r) => setTimeout(r, 3000));
order = (await (await fetch(`${BASE}/orders/${order.code}`, { headers })).json()).data;
}
if (order.status !== "delivered") throw new Error(`order ${order.code}: ${order.status}`);
const items = await (await fetch(`${BASE}${order.items_url}`, { headers })).json();
return items.data.items; // [{ email, password, ... }]
}Python (รอ OTP)
import os, time, requests
BASE = "https://api.kaimail.shop/v1"
S = requests.Session()
S.headers["Authorization"] = f"Bearer {os.environ['KAIMAIL_KEY']}"
def wait_otp(email, since_iso, timeout=120):
"""รอ OTP ที่ส่งมาหลังเวลา since_iso (เช่นเวลาที่กดขอรหัส)"""
end = time.time() + timeout
while time.time() < end:
r = S.get(f"{BASE}/mailboxes/{email}/otp", params={"since": since_iso})
if r.status_code == 429:
time.sleep(int(r.headers.get("Retry-After", "5"))); continue
r.raise_for_status()
latest = r.json()["data"]["latest"]
if latest:
return latest["code"]
time.sleep(5)
return None