路徑參數
從建立影片請求回傳的任務 ID。將 id 與 task_id 視為相同的非同步識別碼。
如果 create 回應回傳 poll_url,請呼叫該 URL。本當它指向 /v1/tasks/{id} 時,視為固定的標準狀態端點。 如果該任務不再存在或無法透過公開的非同步任務協定解析,TokenLab 會回傳 async_task_not_found 並附上訊息 Task not found or no longer available.
當結算已完成時返回 TokenLab 帳單交易 ID。它對應 dashboard / 對帳使用的交易識別,與非同步 id / task_id 不同。
任務狀態:pending、processing、completed、failed。
可選進度值。僅在任務提供真實進度時返回;請使用 status 判斷任務是否完成。
單一影片資料,當可用時包含 url、duration、width 與 height。
cURL
Python
JavaScript
Go
PHP
curl "https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
-H "Authorization: Bearer sk-your-api-key"
import requests
import time
task_id = "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
while True :
response = requests.get(
f "https://api.tokenlab.sh/v1/tasks/ { task_id } " ,
headers = { "Authorization" : "Bearer sk-your-api-key" }
)
data = response.json()
print ( f "Status: { data[ 'status' ] } " )
if data[ "status" ] == "completed" :
print ( f "Video URL: { data[ 'video_url' ] } " )
break
elif data[ "status" ] == "failed" :
print ( f "Error: { data[ 'error' ] } " )
break
time.sleep( 5 ) # Poll every 5 seconds
const taskId = 'ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ;
async function pollForVideo () {
while ( true ) {
const response = await fetch (
`https://api.tokenlab.sh/v1/tasks/ ${ taskId } ` ,
{ headers: { 'Authorization' : 'Bearer sk-your-api-key' } }
);
const data = await response . json ();
console . log ( `Status: ${ data . status } ` );
if ( data . status === 'completed' ) {
console . log ( `Video URL: ${ data . video_url } ` );
return data . video_url ;
} else if ( data . status === 'failed' ) {
throw new Error ( data . error );
}
await new Promise ( r => setTimeout ( r , 5000 ));
}
}
package main
import (
" encoding/json "
" fmt "
" net/http "
" time "
)
func main () {
taskID := "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
for {
req , _ := http . NewRequest ( "GET" ,
fmt . Sprintf ( "https://api.tokenlab.sh/v1/tasks/ %s " , taskID ), nil )
req . Header . Set ( "Authorization" , "Bearer sk-your-api-key" )
client := & http . Client {}
resp , _ := client . Do ( req )
var result map [ string ] interface {}
json . NewDecoder ( resp . Body ). Decode ( & result )
resp . Body . Close ()
fmt . Printf ( "Status: %s \n " , result [ "status" ])
if result [ "status" ] == "completed" {
fmt . Printf ( "Video URL: %s \n " , result [ "video_url" ])
break
} else if result [ "status" ] == "failed" {
fmt . Printf ( "Error: %s \n " , result [ "error" ])
break
}
time . Sleep ( 5 * time . Second )
}
}
<? php
$taskId = 'ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ;
while ( true ) {
$ch = curl_init ( "https://api.tokenlab.sh/v1/tasks/{ $taskId }" );
curl_setopt_array ( $ch , [
CURLOPT_RETURNTRANSFER => true ,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer sk-your-api-key'
]
]);
$response = curl_exec ( $ch );
curl_close ( $ch );
$data = json_decode ( $response , true );
echo "Status: { $data ['status']} \n " ;
if ( $data [ 'status' ] === 'completed' ) {
echo "Video URL: { $data ['video_url']} \n " ;
break ;
} elseif ( $data [ 'status' ] === 'failed' ) {
echo "Error: { $data ['error']} \n " ;
break ;
}
sleep ( 5 );
}
Response (pending)
Response (processing)
Response (completed)
Response (failed)
{
"id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"task_id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"poll_url" : "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"status" : "pending" ,
"model" : "veo3.1" ,
"created" : 1706000000 ,
"updated" : 1706000000
}
{
"id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"task_id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"poll_url" : "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"status" : "processing" ,
"model" : "veo3.1" ,
"created" : 1706000000 ,
"updated" : 1706000030
}
{
"id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"task_id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"poll_url" : "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"billing_transaction_id" : "cmo164zyc0s48jpgegi80vqv4" ,
"status" : "completed" ,
"video_url" : "https://assets.tokenlab.sh/videos/abc123.mp4" ,
"model" : "veo3.1" ,
"created" : 1706000000 ,
"updated" : 1706000060
}
{
"id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"task_id" : "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"poll_url" : "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ,
"status" : "failed" ,
"error" : "Content policy violation" ,
"model" : "veo3.1" ,
"created" : 1706000000 ,
"updated" : 1706000060
}
輪詢最佳做法
每 5 到 10 秒輪詢一次
對於較長的任務實作指數退避
設定最大逾時(例如:10 分鐘)
優雅地處理 failed 狀態
import time
def wait_for_video ( task_id , max_wait = 600 , interval = 5 ):
"""Wait for video with timeout."""
start = time.time()
while time.time() - start < max_wait:
response = requests.get(
f "https://api.tokenlab.sh/v1/tasks/ { task_id } " ,
headers = { "Authorization" : "Bearer sk-your-api-key" }
)
data = response.json()
if data[ "status" ] == "completed" :
return data[ "video_url" ]
elif data[ "status" ] == "failed" :
raise Exception (data.get( "error" , "Video generation failed" ))
time.sleep(interval)
raise TimeoutError ( "Video generation timed out" )