Task Endpoint
Overview
The task endpoint in the Haidy API allows you to retrieve current information about specific tasks using a task_id. This guide explains how to interact with this endpoint using Node.js, focusing on polling task statuses and handling responses effectively.
Endpoint Details
- URL:
/v2/tasks - Method: POST
- Authentication: Bearer Token (API key required)
- Description: Retrieves current task information. Can be called continuously to get intermediate results.
Task Request Model
When making a request to this endpoint, you need to send a TaskRequest object that includes the task_id. Here’s the basic structure expected by the API:
{
"task_id": "your_task_id_here"
}
Task Status Enum
The task may have one of the following statuses:
- CREATED: The task record was created.
- QUEUED: The task is queued for processing.
- STARTED: Processing has started.
- AUDIO_PREPROCESSING: Audio preprocessing is running.
- AUDIO_PROCESSING: Audio processing is running.
- AUDIO_PROCESSING_DONE: Audio processing completed.
- TRANSCRIPTION_DONE: Transcription completed.
- DOCUMENT_DOWNLOAD_DONE: Document download completed.
- DOCUMENT_PARSING_DONE: Document parsing completed.
- DOCUMENT_GENERATION_DONE: Document generation completed.
- REPORT_ANNOTATION_DRAFT: Draft report annotations were created.
- SERVICE_DONE: Upstream service processing completed.
- DONE: The task completed successfully.
- FAILED: The task failed.
Handling Task Information Responses
The response model includes status, percentage, result, started_at, completed_at, status_changes_at, task_id, and optional additional_info. The API returns HTTP 404 if the task is not found.
Tasks in terminal states such as DONE and FAILED can still be returned while they are retained by task storage. Clients should treat the task endpoint as a pollable status endpoint, not as a one-time retrieval endpoint.
Setup
Ensure you have the necessary packages installed:
npm install axios
Example Code
This example shows how to send requests to the task endpoint and handle different statuses:
const axios = require("axios");
const API_URL = "https://api.44ai.ch/v2/tasks";
const token = "your_api_token"; // Replace with your actual API token
async function getTaskInformation(taskId) {
try {
const response = await axios.post(
API_URL,
{
task_id: taskId,
},
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);
const taskInfo = response.data;
console.log(`Task Status: ${taskInfo.status}`);
switch (taskInfo.status) {
case "CREATED":
case "QUEUED":
case "STARTED":
case "AUDIO_PREPROCESSING":
case "AUDIO_PROCESSING":
case "AUDIO_PROCESSING_DONE":
case "DOCUMENT_DOWNLOAD_DONE":
case "DOCUMENT_PARSING_DONE":
case "DOCUMENT_GENERATION_DONE":
case "REPORT_ANNOTATION_DRAFT":
case "SERVICE_DONE":
console.log("Task still processing...");
setTimeout(() => getTaskInformation(taskId), 2000); // Poll every 2 seconds
break;
case "TRANSCRIPTION_DONE":
console.log("Transcription completed. Result:", taskInfo.result);
setTimeout(() => getTaskInformation(taskId), 2000); // Continue polling for final status
break;
case "DONE":
console.log("Task fully completed. Final results:", taskInfo.result);
break;
case "FAILED":
console.error("Task failed:", taskInfo.result);
break;
default:
console.error("Unexpected status:", taskInfo.status);
}
} catch (error) {
if (error.response && error.response.status === 404) {
console.error("Task not found. Please check the task ID.");
} else {
console.error("Error while checking task status:", error);
}
}
}
// Replace 'your_task_id' with the actual task ID
getTaskInformation("your_task_id");