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:
/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 has been created and is waiting in the queue.
- STARTED (RUNNING): The task has started processing.
- AUDIO_PROCESSING: The task is currently processing the audio file.
- TRANSCRIPTION_DONE: The transcription is completed, and the result is available in the response.
- DONE: The entire task processing is complete, including any final steps.
- FAILED: The task has encountered an error and has not completed successfully.
Handling Task Information Responses
The response model, TaskInfo, includes the task status. The API will return an HTTP 404 error if the task is not found. Special logging and task result handling occur when a task reaches a DONE or FAILED status.
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 "STARTED":
case "AUDIO_PROCESSING":
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");