จัดการ API Pagination ใน n8n: ดึงข้อมูลครบทุกหน้า
เรียนรู้วิธีจัดการ API Pagination ทุกรูปแบบใน n8n ตั้งแต่ Page-based, Cursor-based จนถึง Link Header เพื่อดึงข้อมูลครบทุก Record
สมมติต้องดึงรายชื่อลูกค้าทั้งหมดจาก CRM ผ่าน API คุณเรียก endpoint แล้วได้ข้อมูลมา 100 รายการ ดูเหมือนครบ แต่ความจริงมีลูกค้า 2,847 คน API ส่งมาแค่ 100 รายการแรก รายการที่เหลือต้องดึงมาเองทีละหน้า
นี่คือ Pagination ปัญหาที่คนใช้ n8n เจอบ่อย และถ้าไม่รู้จักจัดการก็อาจทำงานกับข้อมูลที่ไม่ครบโดยไม่รู้ตัว
Pagination คืออะไรและทำไมถึงมี
API ไม่ส่งข้อมูลทั้งหมดในครั้งเดียวเพราะหลายเหตุผล — Response size จะใหญ่มาก, Server จะ overload, Network จะช้า แทนที่จะส่งข้อมูล 100,000 รายการใน response เดียว API แบ่งเป็น “หน้า” แล้วให้ Client ขอทีละหน้า
API แต่ละตัวออกแบบ Pagination ต่างกัน มีสามรูปแบบหลัก:
รูปแบบที่ 1: Page-based Pagination
ส่ง page parameter บอกว่าต้องการหน้าไหน
GET /orders?page=1&per_page=100
GET /orders?page=2&per_page=100
GET /orders?page=3&per_page=100
Response มักมี metadata:
{
"data": [...],
"pagination": {
"current_page": 1,
"total_pages": 15,
"total_count": 1487,
"per_page": 100
}
}
วิธีจัดการใน n8n:
ใช้ HTTP Request Node พร้อมเปิด Pagination:
- เปิด HTTP Request Node
- ไปที่ Options → Pagination
- เลือก mode “Update a Parameter in Each Request”
- Parameter Name:
page - Pagination Complete When: เลือก “Received Items fewer than specified”
- กำหนด items per page ตรงกับที่ API ระบุ
n8n จะเพิ่มค่า page ทีละ 1 อัตโนมัติจนกว่าจะได้ items น้อยกว่าที่กำหนด (แปลว่าถึงหน้าสุดท้ายแล้ว)
รูปแบบที่ 2: Cursor-based Pagination
แทนที่จะใช้เลขหน้า API ให้ cursor — ค่าที่บอกว่า “เริ่มดึงต่อจากจุดนี้” มักเป็น record ID สุดท้าย หรือ opaque string ที่ encode ตำแหน่ง
GET /posts → ได้ posts 1-50 + next_cursor: "eyJpZCI6NTB9"
GET /posts?cursor=eyJpZCI6NTB9 → ได้ posts 51-100 + next_cursor: "eyJpZCI6MTAwfQ=="
GET /posts?cursor=eyJpZCI6MTAwfQ== → ได้ posts สุดท้าย + next_cursor: null
วิธีจัดการใน n8n:
HTTP Request Node รองรับ Cursor Pagination ผ่าน:
- Options → Pagination
- Mode: “Response Contains Next URL” หรือ “Update a Parameter in Each Request”
- Complete When: “Next Cursor is Empty”
ถ้า API ส่ง cursor ใน response body:
- Next Cursor Path:
$.pagination.next_cursor - Parameter Name:
cursor
รูปแบบที่ 3: Link Header Pagination
API บางตัว (โดยเฉพาะ GitHub, Shopify) ส่ง URL ของหน้าถัดไปใน HTTP Header ชื่อ Link:
Link: <https://api.example.com/orders?page=2>; rel="next",
<https://api.example.com/orders?page=15>; rel="last"
วิธีจัดการใน n8n:
- เปิด Full Response ใน HTTP Request Node
- Pagination → Mode: “Response Contains Next URL”
- URL Path: ใช้ Expression ดึง URL จาก
Linkheader
อาจต้องใช้ Code Node parse Link header:
const linkHeader = $input.item.json.headers?.link || '';
const nextMatch = linkHeader.match(/<([^>]+)>;\s*rel="next"/);
return {
next_url: nextMatch ? nextMatch[1] : null
};
รูปแบบที่ 4: Offset-based Pagination
คล้าย Page-based แต่ใช้ offset (จำนวน records ที่ข้ามไป) แทนเลขหน้า
GET /products?offset=0&limit=50 → records 1-50
GET /products?offset=50&limit=50 → records 51-100
GET /products?offset=100&limit=50 → records 101-150
วิธีจัดการใน n8n ด้วย Loop:
เมื่อ HTTP Request Node ไม่รองรับ Offset pagination โดยตรง ใช้ Loop แทน:
Set Node (offset = 0, limit = 50, has_more = true)
→ Loop While has_more
→ HTTP Request (GET /products?offset={{ $json.offset }}&limit=50)
→ Code Node:
const items = $input.item.json.data;
const total = $input.item.json.total;
const offset = $('Set Node').item.json.offset;
return {
items,
offset: offset + 50,
has_more: (offset + 50) < total
}
→ Append items to result array
→ IF has_more → กลับ Loop
→ Aggregate all items
Manual Pagination ด้วย Loop (Universal Method)
สำหรับ API ที่มี Pagination แบบพิเศษ หรือเมื่อต้องการควบคุม Logic เองทั้งหมด ใช้ Loop:
// Set Node ตั้งต้น
{
"page": 1,
"all_data": [],
"has_more": true
}
Loop While has_more = true
→ HTTP Request (ดึงข้อมูล)
→ Code Node (ตรวจสอบและเก็บข้อมูล):
const response = $('HTTP Request').first().json;
const current_page = $('Loop').first().json.page;
const all_data = $('Loop').first().json.all_data;
const new_data = [...all_data, ...response.data];
const has_more = response.pagination.current_page < response.pagination.total_pages;
return {
page: current_page + 1,
all_data: new_data,
has_more: has_more
};
→ IF has_more → กลับไป HTTP Request
→ Done → ใช้ all_data ต่อ
ป้องกัน Infinite Loop
เสมอต้องมีเงื่อนไขหยุด Loop ถ้า API มีบัคและส่ง has_more: true ไม่มีวันสิ้นสุด Loop จะรันตลอดไป
เพิ่มเงื่อนไข safety brake:
const MAX_PAGES = 500; // ไม่ควรมี API ที่มีมากกว่า 500 หน้า
const current_page = $('Loop').first().json.page;
const has_more = response.has_more && current_page < MAX_PAGES;
if (current_page >= MAX_PAGES) {
console.warn(`Pagination stopped at page ${MAX_PAGES} (safety limit)`);
}
ตัวอย่างจริง: ดึง Shopee Orders ทั้งหมด
Shopee API ใช้ cursor-based pagination กับ offset:
GET /api/v2/order/get_order_list?
time_range_field=create_time&
time_from=1700000000&
time_to=1700086400&
page_size=100&
cursor= (ว่างสำหรับหน้าแรก)
Response:
{
"response": {
"order_list": [...],
"more": true,
"next_cursor": "100"
}
}
Set Node (cursor = "", more = true)
→ Loop While more = true
→ HTTP Request (Shopee API + cursor)
→ Code Node:
const response = $('HTTP Request').first().json.response;
return {
orders: response.order_list,
cursor: response.next_cursor || '',
more: response.more
};
→ Append orders
→ Wait Node (0.5 วินาที — ป้องกัน Rate Limit)
→ IF more → กลับ Loop
→ Aggregate all orders
Pagination อาจดูซับซ้อนตอนแรก แต่เมื่อเข้าใจ pattern แต่ละรูปแบบแล้ว มันก็กลายเป็นเรื่องปกติที่ทำซ้ำได้ สิ่งสำคัญคือต้องตรวจสอบ API documentation ของแต่ละบริการก่อนเพื่อเข้าใจว่าใช้ Pagination แบบไหน แล้วเลือก approach ที่เหมาะสม
อยากเรียน n8n แบบเป็นระบบ ตั้งแต่เริ่มต้นจนสร้าง Workflow ใช้งานจริงได้ ลองดู คอร์สสอน n8n ที่ aiunlock.co
Related posts
HTTP Request Node: เชื่อมต่อ API อะไรก็ได้ด้วย n8n
คู่มือใช้งาน HTTP Request Node ใน n8n ครอบคลุม GET, POST, Authentication ทุกประเภท Headers, Body และการจัดการ Response
n8n Advanced: 10 เทคนิคขั้นสูงสำหรับมืออาชีพ
รวม 10 เทคนิค n8n ขั้นสูงที่มืออาชีพใช้จริง ตั้งแต่ Error Handling, Sub-Workflow, Code Node ไปจนถึง API Pagination
สร้าง AI Agent ด้วย n8n: ให้ AI ทำงานแทนคุณ
วิธีสร้าง AI Agent ใน n8n ที่วางแผนและใช้ Tools เองได้ ตั้งแต่ตั้งค่าจนถึง Agent หลายขั้นตอนขั้นสูง