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

# Cancel Task

> Cancel a running or pending workflow task

## Endpoint

```
POST /tasks/{task_id}/cancel
```

## Authentication

This endpoint requires authentication via API key. Include your API key in the `X-API-Key` header:

```bash theme={null}
X-API-Key: ca_your_api_key_here
```

## Path Parameters

| Parameter | Type    | Required | Description                              |
| --------- | ------- | -------- | ---------------------------------------- |
| `task_id` | integer | Yes      | Unique identifier for the task to cancel |

## Request Example

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://platform.chamelio.ai/tasks/12345/cancel" \
    -H "X-API-Key: ca_your_api_key_here"
  ```

  ```python Python theme={null}
  import requests

  task_id = 12345

  url = f"https://platform.chamelio.ai/tasks/{task_id}/cancel"
  headers = {
      "X-API-Key": "ca_your_api_key_here"
  }

  response = requests.post(url, headers=headers)
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const taskId = 12345;

  const response = await fetch(
    `https://platform.chamelio.ai/tasks/${taskId}/cancel`,
    {
      method: 'POST',
      headers: {
        'X-API-Key': 'ca_your_api_key_here'
      }
    }
  );

  const data = await response.json();
  console.log(data);
  ```
</CodeGroup>

## Response

### Success Response

**Status Code:** `200 OK`

```json theme={null}
{
  "task_id": 12345,
  "status": "cancelled",
  "message": "Task cancelled successfully"
}
```

### Response Fields

| Field     | Type    | Description                                   |
| --------- | ------- | --------------------------------------------- |
| `task_id` | integer | ID of the cancelled task                      |
| `status`  | string  | Updated task status (should be `"cancelled"`) |
| `message` | string  | Success confirmation message                  |

## Error Responses

### 401 Unauthorized

Returned when authentication fails. See the [authentication errors](/api-reference/introduction#authentication-errors) section for details.

```json theme={null}
{
  "detail": "Invalid API key"
}
```

### 404 Not Found

Returned when the task doesn't exist or you don't have access to it.

```json theme={null}
{
  "detail": "Task not found"
}
```

### 409 Conflict

Returned when the task cannot be cancelled (e.g., already completed or already cancelled).

```json theme={null}
{
  "detail": "Task cannot be cancelled in its current state"
}
```

### 500 Internal Server Error

Returned when cancellation fails due to a server error.

```json theme={null}
{
  "detail": "Failed to cancel task"
}
```

## Notes

<Warning>
  Cancelling a task is irreversible. The workflow will stop at its current step and cannot be resumed.
</Warning>

<Info>
  Only tasks with status `pending` or `in_progress` can be cancelled. Completed, failed, or already cancelled tasks cannot be cancelled.
</Info>

## Use Cases

This endpoint is useful for:

* **User-initiated cancellation** - Allow users to stop workflows they no longer need
* **Error handling** - Cancel workflows that are stuck or encountering issues
* **Workflow management** - Clean up unnecessary or duplicate workflow instances
* **Resource optimization** - Stop workflows that are no longer relevant to free up resources
