Paramètres de chemin
L’ID de tâche renvoyé par la requête de création de vidéo. Considérez id et task_id comme la même identité asynchrone.
Si une réponse de création renvoie poll_url, appelez cette URL exacte. Lorsqu’elle pointe vers /v1/tasks/{id}, considérez-la comme le point de terminaison canonique et fixe pour le statut. Si la tâche n’existe plus ou n’est plus disponible dans l’enregistrement public de tâche, TokenLab renvoie async_task_not_found avec le message Task not found or no longer available.
Réponse
Identifiant canonique de la tâche asynchrone.
Alias de l’identifiant de tâche asynchrone.
URL de polling préférée lorsque la réponse de création en fournit une.
ID de transaction de facturation TokenLab lorsque le règlement est déjà terminé. Il s’agit de l’identifiant utilisé pour le dashboard / le rapprochement, distinct de l’id / task_id asynchrone.
Statut de la tâche : pending, processing, completed, failed.
Valeur de progression facultative. Renvoyée uniquement lorsqu’une progression réelle est disponible ; utilisez status pour déterminer la fin de la tâche.
URL de la vidéo générée (lorsqu’elle est terminée).
Charge utile d’une seule vidéo avec url, duration, width et height lorsque disponible.
Plusieurs charges utiles vidéo lorsque la tâche de génération renvoie plus d’une sortie.
Message d’erreur (en cas d’échec).
Horodatage de la dernière mise à jour.
Modèle utilisé pour la tâche.
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
}
Bonnes pratiques de polling
Effectuer un polling toutes les 5 à 10 secondes
Implémenter un backoff exponentiel pour les tâches longues
Définir un délai d’attente maximal (par exemple, 10 minutes)
Gérer le statut failed de manière appropriée
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" )