โพสต์แรกของคุณ
บทเรียนครบวงจรตั้งแต่การสร้าง draft เผยแพร่ทันที ตรวจสอบสถานะราย target ตั้งเวลา ยกเลิก ไปจนถึงการ retry อย่างปลอดภัย
อัปเดตล่าสุด
เนื้อหาในหน้านี้
- 1. สร้าง text draft
- 2. เผยแพร่ทันที
- 3. ตรวจสอบสถานะราย target
- ความหมายของแต่ละสถานะ
- 4. ตั้งเวลาโพสต์แทน
- ตั้งเวลาแบบระบุเวลาชัดเจน (local time + IANA timezone + UTC instant)
- ตั้งเวลาลง queue slot รายสัปดาห์ถัดไป
- 5. ยกเลิก target ที่ตั้งเวลาไว้
- 6. Retry target ที่ล้มเหลว
- เอกสารอ้างอิง API และคู่มือที่เกี่ยวข้อง
บทเรียนนี้พาคุณไล่ครบทุกขั้นตอนของวงจรชีวิตโพสต์ผ่าน REST API ตั้งแต่การสร้าง draft เผยแพร่ทันที ตรวจสอบสถานะการทำงานราย target ตั้งเวลาโพสต์หรือจอง queue slot ยกเลิกงานที่ยังไม่เริ่ม และ retry อย่างปลอดภัยเมื่อเกิดข้อผิดพลาด
ก่อนเริ่มต้น ตรวจสอบให้แน่ใจว่า API key ของคุณมีสิทธิ์ post:create, post:read, post:publish, post:schedule และ post:cancel สำหรับ Brand และบัญชีเป้าหมาย (ดู Authentication & Scopes) และตรวจสอบว่า availability.publishing.available เป็น true ผ่าน GET /api/v1/accounts/{id}/capabilities
หมายเหตุ
REST API พร้อมใช้งานแล้ววันนี้ ส่วนคำสั่ง postkub CLI ที่เทียบเท่าในหน้านี้เป็น client รุ่น preview ของ @postkub/cli ซึ่งยังไม่เผยแพร่บน npm
1. สร้าง text draft
ทุกโพสต์เริ่มต้นจากการเป็น draft ที่ revision: 1 ให้ส่ง UUID ชุดใหม่ใน header Idempotency-Key พร้อม brandId, อาร์เรย์ socialAccountIds อย่างน้อย 1 บัญชี (1–10 บัญชีที่ไม่ซ้ำกันภายใต้ Brand นั้น) และ text (ยาว 1–5,000 ตัวอักษร และต้องไม่ใช่ช่องว่างล้วน)
curl -sS -X POST https://postkub.com/api/v1/posts \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 11111111-1111-4111-8111-111111111111" \
-d '{
"brandId": "brand_example01",
"socialAccountIds": ["acc_example01"],
"text": "Our new cold brew menu is live in all branches today."
}'
คำสั่ง CLI ที่เทียบเท่าแบบใช้ flag หรือส่ง JSON input (preview):
postkub posts create \
--brand-id brand_example01 \
--account-id acc_example01 \
--text "Our new cold brew menu is live in all branches today." \
--output json
postkub posts create \
--json '{"brandId":"brand_example01","socialAccountIds":["acc_example01"],"text":"Our new cold brew menu is live in all branches today."}' \
--output json
ผลลัพธ์ (201 Created):
{
"requestId": "11111111-1111-4111-8111-111111111111",
"post": {
"id": "post_example123",
"brandId": "brand_example01",
"status": "draft",
"revision": 1,
"text": "Our new cold brew menu is live in all branches today.",
"socialAccountIds": ["acc_example01"],
"mediaAssetIds": [],
"mediaUrls": [],
"mediaKind": "image",
"firstComment": null,
"firstCommentOverrides": [],
"authorActorType": "user",
"authorId": "user_example01",
"sourcePrincipalType": "api_key",
"sourcePrincipalId": "key_example01",
"createdAt": "2026-12-03T09:00:00.000Z",
"updatedAt": "2026-12-03T09:00:00.000Z",
"targets": [
{
"socialAccountId": "acc_example01",
"status": "pending"
}
],
"allowedActions": ["post:read", "post:update"]
},
"url": "/api/v1/posts/post_example123"
}
ในขั้นนี้ยังไม่มีการนำเข้าคิวหรือส่งข้อมูลออกไปยังแพลตฟอร์มภายนอก Postkub เก็บข้อความ Unicode ตรงตามที่ส่ง (ไม่ตัดช่องว่าง) และจะตรวจสอบขีดจำกัดความยาวข้อความรายแพลตฟอร์มเป็นจำนวน UTF-8 byte ตอนเผยแพร่หรือตั้งเวลา (อักษรภาษาไทยใช้จำนวน UTF-8 byte ต่อตัวอักษรมากกว่าอักษร ASCII) หากต้องการแนบรูปภาพ ดูได้ที่ Hosted Media
2. เผยแพร่ทันที
เรียก POST /api/v1/posts/{id}/publish พร้อม Idempotency-Key UUID ชุดใหม่ ค่า expectedRevision ปัจจุบันของ draft และรายการ socialAccountIds จาก target ที่บันทึกไว้ซึ่งต้องการเผยแพร่
curl -sS -X POST https://postkub.com/api/v1/posts/post_example123/publish \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 22222222-2222-4222-8222-222222222222" \
-d '{
"expectedRevision": 1,
"socialAccountIds": ["acc_example01"]
}'
คำสั่ง CLI ที่เทียบเท่า (preview; เพิ่ม --wait เพื่อรอจนงานประมวลผลเสร็จสิ้น):
postkub posts publish \
--id post_example123 \
--expected-revision 1 \
--account-id acc_example01 \
--wait \
--output json
ผลลัพธ์ (202 Accepted):
{
"requestId": "22222222-2222-4222-8222-222222222222",
"post": {
"id": "post_example123",
"brandId": "brand_example01",
"revision": 1,
"socialAccountIds": ["acc_example01"]
},
"targets": [
{
"socialAccountId": "acc_example01",
"publicationId": "pub_example456",
"revision": 1,
"status": "queued",
"scheduleMode": "now",
"fireAtUtc": "2026-12-03T09:01:00.000Z",
"localTime": null,
"resolvedLocalTime": null,
"timezone": null,
"cancellationRequested": false,
"allowedActions": ["cancel"],
"firstCommentState": "skipped",
"firstCommentError": null
}
],
"statusUrl": "/api/v1/posts/post_example123"
}
สถานะ HTTP 202 หมายถึง Postkub ตอบรับเจตนาการเผยแพร่ที่เปลี่ยนแปลงไม่ได้และแจ้ง worker ให้เริ่มทำงานแล้ว ไม่ใช่ หลักฐานว่าเครือข่ายโซเชียลเผยแพร่เสร็จเรียบร้อยแล้ว
3. ตรวจสอบสถานะราย target
เรียก GET /api/v1/posts/{id} (ตรงกับ statusUrl) เพื่อตรวจดูออบเจ็กต์ publication ล่าสุดของแต่ละ target:
curl -sS https://postkub.com/api/v1/posts/post_example123 \
-H "Authorization: Bearer <API_KEY>"
คำสั่ง CLI ที่เทียบเท่า (preview):
postkub posts get --id post_example123 --output json
ตัวอย่างผลลัพธ์เมื่อเผยแพร่สำเร็จ (200 OK):
{
"id": "post_example123",
"brandId": "brand_example01",
"status": "published",
"revision": 1,
"text": "Our new cold brew menu is live in all branches today.",
"socialAccountIds": ["acc_example01"],
"mediaAssetIds": [],
"mediaUrls": [],
"mediaKind": "image",
"firstComment": null,
"firstCommentOverrides": [],
"authorActorType": "user",
"authorId": "user_example01",
"sourcePrincipalType": "api_key",
"sourcePrincipalId": "key_example01",
"createdAt": "2026-12-03T09:00:00.000Z",
"updatedAt": "2026-12-03T09:01:09.000Z",
"targets": [
{
"socialAccountId": "acc_example01",
"status": "published",
"publication": {
"socialAccountId": "acc_example01",
"publicationId": "pub_example456",
"revision": 3,
"status": "published",
"scheduleMode": "now",
"fireAtUtc": "2026-12-03T09:01:00.000Z",
"localTime": null,
"resolvedLocalTime": null,
"timezone": null,
"cancellationRequested": false,
"allowedActions": [],
"firstCommentState": "skipped",
"firstCommentError": null
}
}
],
"allowedActions": ["post:read"]
}
ความหมายของแต่ละสถานะ
แต่ละ target รายงานสถานะของตนเองใน targets[].publication.status (หรือ pending หากยังไม่เคยถูก accept) ส่วน post.status ระดับบนสุดสรุปภาพรวมของทุก target:
| สถานะของ target | ความหมาย |
|---|---|
pending | บันทึกอยู่บน draft แล้ว แต่ยังไม่ได้สั่งเผยแพร่หรือตั้งเวลา |
queued | รับคำสั่งแล้วและกำลังรอเวลา fireAtUtc หรือรอ worker รับงาน |
processing | worker กำลังส่งคำขอหรือตรวจสอบสถานะการประมวลผลของแพลตฟอร์ม |
published | ยืนยันการเผยแพร่บนแพลตฟอร์มโซเชียลแล้ว |
failed | การทำงานล้มเหลวพร้อมรหัสข้อผิดพลาดที่ทราบแน่ชัด (errorCode) ตรวจดู allowedActions เพื่อดูว่าสั่ง retry อย่างปลอดภัยได้หรือไม่ |
cancelled | งานที่ยังไม่เริ่มถูกยกเลิกก่อนเกิดผลลัพธ์ภายนอก |
outcome_unknown | เริ่มเรียกแพลตฟอร์มไปแล้วแต่หมดเวลาหรือได้คำตอบไม่ชัดเจน Postkub จะทำ reconciliation แทนการส่งซ้ำ |
needs_review | ต้องให้เจ้าของหรือผู้ดูแลตรวจสอบ (เช่น เกินกรอบเวลาล่าช้า 15 นาทีหรือไม่สามารถ reconcile อัตโนมัติได้) |
blocked | target ทำงานต่อไม่ได้เนื่องจากมีเงื่อนไขบล็อก เช่น บัญชีหลุดการเชื่อมต่อ |
ค่า post.status ระดับบนสุดสรุปสถานะรวมเป็นหนึ่งในค่าต่อไปนี้ ได้แก่ draft, scheduled, publishing, partially_published, published, needs_attention, failed หรือ cancelled
4. ตั้งเวลาโพสต์แทน
สร้าง draft ใหม่ (เช่น post_example456 ที่ revision: 1) แล้วเรียก POST /api/v1/posts/{id}/schedule โดย key ต้องมีทั้งสิทธิ์ post:publish และ post:schedule
ตั้งเวลาแบบระบุเวลาชัดเจน (local time + IANA timezone + UTC instant)
ส่งอาร์เรย์ targets ที่ระบุเวลาท้องถิ่น localTime (ไม่มี offset), IANA timezone ปัจจุบันของบัญชี และค่า fireAtUtc ที่แปลงตรงกัน Postkub ตรวจสอบว่าทั้งสามค่าสอดคล้องกันเพื่อป้องกันเวลาคลาดเคลื่อนจากการเปลี่ยน timezone หรือ DST
- สำหรับบัญชีที่ใช้ timezone ภูมิภาค เช่น
Asia/Bangkok: ระบุtimezoneเป็น"Asia/Bangkok",localTimeเป็น"2026-12-10T09:00:00"และfireAtUtcเป็น"2026-12-10T02:00:00.000Z" - สำหรับบัญชีที่ตั้งค่าเป็น UTC: ระบุ
timezoneเป็น"UTC",localTimeเป็น"2026-12-10T09:00:00"และfireAtUtcเป็น"2026-12-10T09:00:00.000Z"
curl -sS -X POST https://postkub.com/api/v1/posts/post_example456/schedule \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 33333333-3333-4333-8333-333333333333" \
-d '{
"expectedRevision": 1,
"targets": [
{
"socialAccountId": "acc_example01",
"localTime": "2026-12-10T09:00:00",
"timezone": "Asia/Bangkok",
"fireAtUtc": "2026-12-10T02:00:00.000Z"
}
]
}'
คำสั่ง CLI ที่เทียบเท่าแบบส่ง JSON ในบรรทัดเดียว (preview):
postkub posts schedule \
--id post_example456 \
--expected-revision 1 \
--json '{"targets":[{"socialAccountId":"acc_example01","localTime":"2026-12-10T09:00:00","timezone":"Asia/Bangkok","fireAtUtc":"2026-12-10T02:00:00.000Z"}]}' \
--output json
ตั้งเวลาลง queue slot รายสัปดาห์ถัดไป
หากบัญชีตั้งค่า weekly queue slot ไว้แล้ว (ดู Scheduling) คุณสามารถส่ง schedule: { "nextSlot": true } แทน targets เพื่อจองช่วงเวลาว่างที่ใกล้ที่สุดในอนาคตโดยอัตโนมัติ:
curl -sS -X POST https://postkub.com/api/v1/posts/post_example456/schedule \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 44444444-4444-4444-8444-444444444444" \
-d '{
"expectedRevision": 1,
"socialAccountIds": ["acc_example01"],
"schedule": { "nextSlot": true }
}'
คำสั่ง CLI ที่เทียบเท่า (preview):
postkub posts schedule \
--id post_example456 \
--expected-revision 1 \
--next-slot \
--account-id acc_example01 \
--output json
ผลลัพธ์ (202 Accepted):
{
"requestId": "44444444-4444-4444-8444-444444444444",
"post": {
"id": "post_example456",
"brandId": "brand_example01",
"revision": 1,
"socialAccountIds": ["acc_example01"]
},
"targets": [
{
"socialAccountId": "acc_example01",
"publicationId": "pub_example789",
"revision": 1,
"status": "queued",
"scheduleMode": "next_slot",
"fireAtUtc": "2026-12-07T02:00:00.000Z",
"localTime": "2026-12-07T09:00:00",
"resolvedLocalTime": "2026-12-07T09:00:00.000",
"timezone": "Asia/Bangkok",
"cancellationRequested": false,
"allowedActions": ["cancel"],
"firstCommentState": "skipped",
"firstCommentError": null
}
],
"statusUrl": "/api/v1/posts/post_example456"
}
5. ยกเลิก target ที่ตั้งเวลาไว้
หากต้องการหยุดงานในคิวที่ยังไม่เริ่มเผยแพร่ ให้เรียก POST /api/v1/posts/{id}/cancel พร้อม expectedRevision ของโพสต์และ socialAccountIds ที่ต้องการยกเลิก:
curl -sS -X POST https://postkub.com/api/v1/posts/post_example456/cancel \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 55555555-5555-4555-8555-555555555555" \
-d '{
"expectedRevision": 1,
"socialAccountIds": ["acc_example01"]
}'
คำสั่ง CLI ที่เทียบเท่า (preview):
postkub posts cancel \
--id post_example456 \
--expected-revision 1 \
--account-id acc_example01 \
--output json
ผลลัพธ์ (200 OK เมื่อยกเลิกทันก่อนเริ่มส่งไปยังแพลตฟอร์ม):
{
"requestId": "55555555-5555-4555-8555-555555555555",
"post": {
"id": "post_example456",
"brandId": "brand_example01",
"revision": 1,
"socialAccountIds": ["acc_example01"]
},
"targets": [
{
"socialAccountId": "acc_example01",
"publicationId": "pub_example789",
"revision": 2,
"status": "cancelled",
"scheduleMode": "next_slot",
"fireAtUtc": "2026-12-07T02:00:00.000Z",
"localTime": "2026-12-07T09:00:00",
"resolvedLocalTime": "2026-12-07T09:00:00.000",
"timezone": "Asia/Bangkok",
"cancellationRequested": true,
"allowedActions": []
}
],
"statusUrl": "/api/v1/posts/post_example456"
}
ผลลัพธ์ของการยกเลิกขึ้นอยู่กับสถานะการทำงานขณะนั้น:
200 OK(status: "cancelled"): target ยังไม่ได้เริ่มเรียกแพลตฟอร์ม ระบบยกเลิกทันทีและคืนโควต้าประจำวันรวมถึง queue slot ที่จองไว้202 Accepted(cancellationRequested: true): เริ่มเรียกแพลตฟอร์มไปแล้วหรือยังไม่ทราบผลลัพธ์แน่ชัด Postkub บันทึกคำขอยกเลิกและทำ reconciliation แทนการระบุว่ายกเลิกสำเร็จแบบผิดพลาด409 ALREADY_PUBLISHED: โพสต์เผยแพร่บนเครือข่ายโซเชียลแล้ว การยกเลิกไม่เคยลบโพสต์ที่เผยแพร่ไปแล้ว
6. Retry target ที่ล้มเหลว
เมื่อ target อยู่ในสถานะ status: "failed" และ allowedActions มี "retry" คุณสามารถนำกลับเข้าคิวใหม่ด้วย POST /api/v1/posts/{id}/retry:
curl -sS -X POST https://postkub.com/api/v1/posts/post_example123/retry \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 66666666-6666-4666-8666-666666666666" \
-d '{
"expectedRevision": 1,
"socialAccountIds": ["acc_example01"]
}'
คำสั่ง CLI ที่เทียบเท่า (preview):
postkub posts retry \
--id post_example123 \
--expected-revision 1 \
--account-id acc_example01 \
--output json
ผลลัพธ์ (202 Accepted):
{
"requestId": "66666666-6666-4666-8666-666666666666",
"post": {
"id": "post_example123",
"brandId": "brand_example01",
"revision": 1,
"socialAccountIds": ["acc_example01"]
},
"targets": [
{
"socialAccountId": "acc_example01",
"publicationId": "pub_example456",
"revision": 4,
"status": "queued",
"scheduleMode": "now",
"fireAtUtc": "2026-12-03T09:01:00.000Z",
"localTime": null,
"resolvedLocalTime": null,
"timezone": null,
"cancellationRequested": false,
"allowedActions": ["cancel"]
}
],
"statusUrl": "/api/v1/posts/post_example123"
}
Postkub อนุญาตให้ใช้ /retry เฉพาะเมื่อครบเงื่อนไขความปลอดภัยทั้ง 3 ข้อ:
- target มี
statusเป็นfailedและพิสูจน์แล้วว่า ไม่เกิดผลข้างเคียงภายนอก (ไม่ใช่outcome_unknownหรือneeds_review) - target ไม่ได้ถูกส่งคำขอยกเลิกไว้
- คำขอ retry ส่งเข้ามาภายใน 15 นาที นับจาก
fireAtUtcเดิม
หากไม่ตรงตามเงื่อนไขใดข้อหนึ่ง Postkub จะปฏิเสธด้วย 409 UNSAFE_RETRY เพื่อป้องกันโพสต์ซ้ำหรือโพสต์ล่าช้าเกินเวลา ทั้งนี้การ retry target ที่ตั้งเวลาไว้ต้องมีสิทธิ์ post:schedule ร่วมกับ post:publish ด้วย
เคล็ดลับ
หากคำขอหมดเวลาก่อนได้รับ HTTP response อย่า เปลี่ยน Idempotency-Key ใหม่ ให้ตรวจสอบสถานะด้วย request ID เดิมผ่าน GET /api/v1/requests/{requestId} หรือส่งซ้ำด้วย key และ payload เดิมทุกประการ อ่านเพิ่มที่ Idempotency & Request Recovery
เอกสารอ้างอิง API และคู่มือที่เกี่ยวข้อง
- API Reference: Posts — รายละเอียดฟิลด์ทั้งหมดสำหรับการสร้าง ดูรายการ แก้ไข ลบ และ archive โพสต์
- API Reference: Publications — รายละเอียดฟิลด์ทั้งหมดสำหรับ
/publish,/schedule,/cancelและ/retry - Hosted Media — การแนบสื่อผ่าน public HTTPS URL บน draft
- Scheduling — การตั้งค่า weekly queue slot, การจัดการ DST และการเปลี่ยนเวลาด้วย
replaceExisting - Errors — โครงสร้างข้อผิดพลาดมาตรฐานและรหัสสถานะทั้งหมด
