> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aiandgpu.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cancel Task

export const QueryAndCancelTask = ({taskId, path}) => {
  const baseUrl = `https://aiandgpu.com/v1/video-tasks/${path}`;
  const curlExample = `curl -X POST '${baseUrl}' \\
-H 'Content-Type: application/json' \\
-H 'Authorization: Bearer <YOUR_API_KEY>' \\
-d '{
  "task_id": "${taskId}"
}'`;
  const pythonExample = `import requests

url = "${baseUrl}"

payload = {
    "task_id": "${taskId}"
}

headers = {
    "Authorization": "Bearer <YOUR_API_KEY>",
    "Content-Type": "application/json",
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())`;
  const jsExample = `const response = await fetch('${baseUrl}', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <YOUR_API_KEY>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    task_id: '${taskId}'
  }),
});

const data = await response.json();
console.log(data);`;
  const goExample = `package main

import (
\t"fmt"
\t"io"
\t"net/http"
\t"strings"
)

func main() {
\turl := "${baseUrl}"

\tpayload := strings.NewReader(\`{
  "task_id": "${taskId}"
}\`)

\treq, _ := http.NewRequest("POST", url, payload)
\treq.Header.Add("Authorization", "Bearer <YOUR_API_KEY>")
\treq.Header.Add("Content-Type", "application/json")

\tres, _ := http.DefaultClient.Do(req)
\tdefer res.Body.Close()

\tbody, _ := io.ReadAll(res.Body)
\tfmt.Println(string(body))
}`;
  const javaExample = `import java.net.http.*;
import java.net.URI;

HttpClient client = HttpClient.newHttpClient();

String body = """
{
  "task_id": "${taskId}"
}
""";

HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("${baseUrl}"))
    .header("Authorization", "Bearer <YOUR_API_KEY>")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());`;
  const phpExample = `<?php

$curl = curl_init();

$payload = json_encode([
    'task_id' => '${taskId}'
]);

curl_setopt_array($curl, [
    CURLOPT_URL => "${baseUrl}",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer <YOUR_API_KEY>",
        "Content-Type: application/json",
    ],
]);

$response = curl_exec($curl);
curl_close($curl);

echo $response;`;
  const csharpExample = `using System.Net.Http;
using System.Text;

var client = new HttpClient();

var payload = @"{
    ""task_id"": ""${taskId}""
}";

var request = new HttpRequestMessage(HttpMethod.Post, "${baseUrl}");
request.Headers.Add("Authorization", "Bearer <YOUR_API_KEY>");
request.Content = new StringContent(payload, Encoding.UTF8, "application/json");

var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);`;
  return <>
      <h2>Request Examples</h2>

      <CodeGroup>
        <CodeBlock language="shellscript" filename="cURL">
          {curlExample}
        </CodeBlock>
        <CodeBlock language="python" filename="Python">
          {pythonExample}
        </CodeBlock>
        <CodeBlock language="javascript" filename="JavaScript">
          {jsExample}
        </CodeBlock>
        <CodeBlock language="go" filename="Go">
          {goExample}
        </CodeBlock>
        <CodeBlock language="java" filename="Java">
          {javaExample}
        </CodeBlock>
        <CodeBlock language="php" filename="PHP">
          {phpExample}
        </CodeBlock>
        <CodeBlock language="csharp" filename="C#">
          {csharpExample}
        </CodeBlock>
      </CodeGroup>
    </>;
};

export const ByteDanceCancelTaskResponse = ({successJson, successTitle = "200 - Success"}) => {
  const error400 = `{
    "result":null,
    "requestId":"02da09ed44fcc5e4f8db2ee08885e41e-b2pjW",
    "error":{
        "code":400,
        "cause":"",
        "message":"任务已处于终态，无法取消",
        "status":"FAILED_PRECONDITION"
    }
}`;
  const error401 = `{
    "error": {
        "code": "auth_missing",
        "message": "Missing Authorization Header",
        "param": null,
        "type": "authentication_error"
    }
}`;
  const error402 = `{
   "error": {
        "code": "insufficient_balance",
        "message": "Insufficient balance",
        "param": null,
        "type": "authentication_error"
   }
}`;
  const error403 = `{
   "error": {
        "code": "permission_denied",
        "message": "Permission denied on this model",
        "param": null,
        "type": "authentication_error"
   }
}`;
  const error404 = `{
   "error": {
        "code": "invalid_url",
        "message": "Invalid URL",
        "param": null,
        "type": "invalid_request_error"
   }
}`;
  const error429 = `{
   "error": {
        "code": "rate_limit_exceeded",
        "message": "Too many requests",
        "param": null,
        "type": "rate_limit_error"
   }
}`;
  const error500 = `{
    "error": {
        "code": "internal_server_error",
        "message": "Internal server error",
        "param": null,
        "type": "server_error"
   }
}`;
  return <>
      <h2>Response Examples</h2>

      <CodeGroup>
        <CodeBlock language={"json"} filename={successTitle}>
          {successJson}
        </CodeBlock>
        <CodeBlock language="json" filename="400 - Bad Request">
          {error400}
        </CodeBlock>
        <CodeBlock language="json" filename="401 - Unauthorized">
          {error401}
        </CodeBlock>
        <CodeBlock language="json" filename="402 - Insufficient Balance">
          {error402}
        </CodeBlock>
        <CodeBlock language="json" filename="403 - Permission Denied">
          {error403}
        </CodeBlock>
        <CodeBlock language="json" filename="404 - Invalid URL">
          {error404}
        </CodeBlock>
        <CodeBlock language="json" filename="429 - Too Many Requests">
          {error429}
        </CodeBlock>
        <CodeBlock language="json" filename="500 - Internal Server Error">
          {error500}
        </CodeBlock>
      </CodeGroup>
    </>;
};

## Authorization

* **Auth Type**: `Bearer Auth` (In: `header`)
* **Format**: `Authorization: Bearer <YOUR_API_KEY>`
* **Description**: Use `Bearer <YOUR_API_KEY>`. Format: `Authorization: Bearer sk-xxxxxx.`
* **API Key**: where <strong>API Key</strong> is your <a href="https://developer.aiandgpu.com" target="_blank" rel="noopener noreferrer">AGCloud API KEY</a>

## Parameters

<ParamField path="task_id" type="string" required>
  The processing task ID.
</ParamField>

<QueryAndCancelTask taskId="<your-task-id-here>x" path="cancel" />

<ByteDanceCancelTaskResponse successJson={`{}`} />

<Warning>
  You can cancel a task only while its status is `queued`. Once the task enters a running state, cancellation is no longer possible.
</Warning>
