เรียกใช้งาน Workflow

เรียกใช้งาน Workflow

หลังจากเปิดใช้งาน Workflow API และสร้าง API Key แล้ว คุณสามารถเรียกใช้งาน workflow ผ่าน API input และรับผลลัพธ์การทำงานของ workflow ได้

วิธีการ

POST

URL

https://api-${endpoint}.gptbots.ai/v1/workflow/invoke

การยืนยันตัวตน

ดูรายละเอียดวิธีการยืนยันตัวตนได้ที่ API Overview

คำขอ (Request)

ตัวอย่าง

curl -X POST 'https://api-${endpoint}.gptbots.ai/v1/workflow/invoke' \ -H 'Authorization: Bearer ${API Key}' \ -H 'Content-Type: application/json' \ -d '{ "userId": "<your_user_id>", "input": { <your_workflow_input_params> }, "isAsync": true, "webhook": [ { "method": "POST", "url": "https://example-1.com", "headers": { "Accept": "application/json", "Authorization": "Bearer <your_token>" } }, { "method": "GET", "url": "https://example-2.com?fr=google", "headers": { "Accept": "application/json", "Authorization": "Bearer <your_token>" } } ] }'
                      
                      curl -X POST 'https://api-${endpoint}.gptbots.ai/v1/workflow/invoke' \
-H 'Authorization: Bearer ${API Key}' \
-H 'Content-Type: application/json' \
-d '{
    "userId": "<your_user_id>",
    "input": {
        <your_workflow_input_params>
    },
    "isAsync": true,
    "webhook": [
        {
            "method": "POST",
            "url": "https://example-1.com",
            "headers": {
                "Accept": "application/json",
                "Authorization": "Bearer <your_token>"
            }
        },
        {
            "method": "GET",
            "url": "https://example-2.com?fr=google",
            "headers": {
                "Accept": "application/json",
                "Authorization": "Bearer <your_token>"
            }
        }
    ]
}'

                    
บล็อกโค้ดนี้ในหน้าต่างลอย

Headers

ฟิลด์ ประเภท จำเป็น คำอธิบาย
Authorization Bearer ${API Key} Y ใช้สำหรับยืนยันการเรียกใช้งานด้วย Authorization: Bearer ${API Key}; สามารถรับคีย์ได้ที่หน้า API keys ในรูปแบบ API Key
Content-Type application/json Y ประเภทข้อมูล กำหนดเป็น application/json เท่านั้น

Body

ฟิลด์ ประเภท จำเป็น คำอธิบาย
userId String N ใช้สำหรับระบุ user ID ของคำขอนี้
input Object Y โหนด "start" ของ workflow อ็อบเจกต์นี้ต้องมีโครงสร้าง input parameter ตรงกับที่ตั้งค่าไว้ในโหนด "start" ของ workflow
isAsync Boolean N กำหนดให้คำขอนี้ทำงานแบบอะซิงโครนัสหรือไม่
- true: ทำงานแบบอะซิงโครนัส
- false (ค่าตั้งต้น): ทำงานแบบซิงโครนัส
หมายเหตุ: หากเป็น true คุณสามารถใช้ "Query workflow run result" เพื่อดึงผลลัพธ์ หรือให้ระบบส่งผลลัพธ์ไปยัง Webhook ที่กำหนดไว้ ทั้งสองวิธีนี้สามารถใช้ร่วมกันได้
webhook Array Object N ในกรณีที่ทำงานแบบอะซิงโครนัส สามารถส่งผลลัพธ์ workflow ไปยัง Webhook ที่ระบุได้ สูงสุด 5 รายการ
webhook[].method String Y วิธีการส่ง request ไปยัง Webhook
webhook[].url String Y URL (ที่อยู่) ของ Webhook
webhook[].headers Object N Header สำหรับ Webhook request สามารถกำหนดเพิ่มเติมได้ตามต้องการ

การตอบกลับ (Response)

ตัวอย่าง

หากเป็นการรันแบบซิงโครนัส ผลลัพธ์จะเป็นดังนี้:

{ "workflowId": "xxxxxxxx", "workflowName": "todayNews", "workflowVersion": "1.0.1", "workflowRunId": "xxxxxxxx", "input": { "topic": "News" }, "output": { "news": [ { "summary": "Fatal crash shuts down major highway in Haleiwa. According to Emergency Medical Services, paramedics responded to the scene of the crash Wednesday morning.", "media": "Hawaii News Now", "title": "Hawaii News Now - Breaking News, Latest News, Weather & Traffic" }, { "summary": "Hawaii Crime: Man, 65, critically injured in Waikīkī assault. Jamil Hart found guilty in Mililani murder case. HPD busts illegal gambling room in Nanakuli.", "media": "KHON2", "title": "KHON2: Hawaii News, Weather, Sports, Breaking News & Live" } ] }, "workflowExecutionTime": 8347, "status": "SUCCEED", "totalCost": 0.6928, "totalTokens": 1745, "startTime": 1758765323024, "endTime": 1758765331373 }
                      
                      {
    "workflowId": "xxxxxxxx",
    "workflowName": "todayNews",
    "workflowVersion": "1.0.1",
    "workflowRunId": "xxxxxxxx",
    "input": {
        "topic": "News"
    },
    "output": {
        "news": [
            {
                "summary": "Fatal crash shuts down major highway in Haleiwa. According to Emergency Medical Services, paramedics responded to the scene of the crash Wednesday morning.",
                "media": "Hawaii News Now",
                "title": "Hawaii News Now - Breaking News, Latest News, Weather & Traffic"
            },
            {
                "summary": "Hawaii Crime: Man, 65, critically injured in Waikīkī assault. Jamil Hart found guilty in Mililani murder case. HPD busts illegal gambling room in Nanakuli.",
                "media": "KHON2",
                "title": "KHON2: Hawaii News, Weather, Sports, Breaking News & Live"
            }
        ]
    },
    "workflowExecutionTime": 8347,
    "status": "SUCCEED",
    "totalCost": 0.6928,
    "totalTokens": 1745,
    "startTime": 1758765323024,
    "endTime": 1758765331373
}

                    
บล็อกโค้ดนี้ในหน้าต่างลอย

หากเป็นการรันแบบอะซิงโครนัส API จะตอบกลับทันทีดังนี้:

{ "workflowId": "xxxxxxxx", "workflowName": "todayNews", "workflowVersion": "1.0.1", "workflowRunId": "xxxxxxxx", "status": "PENDING" }
                      
                      {
    "workflowId": "xxxxxxxx",
    "workflowName": "todayNews",
    "workflowVersion": "1.0.1",
    "workflowRunId": "xxxxxxxx",
    "status": "PENDING"
}

                    
บล็อกโค้ดนี้ในหน้าต่างลอย

คุณสามารถใช้ workflowRunId ที่ได้มา เพื่อตรวจสอบผลลัพธ์การทำงานแบบอะซิงโครนัสได้

Body

ฟิลด์ ประเภท คำอธิบาย
workflowId String รหัส Workflow
workflowName String ชื่อ Workflow
workflowVersion String เวอร์ชันของ Workflow
workflowRunId String รหัสการรัน Workflow ใช้ระบุการทำงานนี้โดยเฉพาะ
input Object ข้อมูล input สำหรับโหนด "start" เหมือนกับ input ใน request
output Object ข้อมูล output สำหรับโหนด "end" ซึ่งเป็นผลลัพธ์การทำงานของ workflow
workflowExecutionTime Number เวลาที่ใช้ในการประมวลผล workflow (มิลลิวินาที)
status String สถานะการรัน workflow ค่าที่เป็นไปได้:
- SUCCEED: สำเร็จ
- FAILED: ล้มเหลว
- PENDING: อยู่ในคิว
- RUNNING: กำลังดำเนินการ
totalCost Number เครดิตค่าใช้จ่ายรวมของการรันนี้
totalTokens Number จำนวนโทเคนที่ใช้ทั้งหมดในการรันนี้
startTime Number เวลาที่เริ่มต้น (timestamp, มิลลิวินาที)
endTime Number เวลาสิ้นสุด (timestamp, มิลลิวินาที)