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

# Cancel Task

> Cancel a running task

# Cancel Task

Cancel a running or pending task. This terminates the task execution and releases associated resources.

## Request

```bash theme={null}
DELETE /api/v1/tasks/{id}
Authorization: Bearer {api_key}
```

### Path Parameters

| Parameter | Type   | Description                |
| --------- | ------ | -------------------------- |
| `id`      | string | The unique task identifier |

## Response

```json theme={null}
{
  "success": true,
  "data": {
    "message": "Task cancelled"
  }
}
```

## Examples

<CodeGroup>
  ```bash cURL theme={null}
  curl -X DELETE https://api.sigmic.ai/api/v1/tasks/c37c1d78-4d23-4e53-949a-ddd775f25e01 \
    -H "Authorization: Bearer sigmic_your_key_here"
  ```

  ```javascript JavaScript theme={null}
  const taskId = 'c37c1d78-4d23-4e53-949a-ddd775f25e01';

  const response = await fetch(
    `https://api.sigmic.ai/api/v1/tasks/${taskId}`,
    {
      method: 'DELETE',
      headers: {
        'Authorization': 'Bearer sigmic_your_key_here'
      }
    }
  );

  const result = await response.json();
  console.log(result.data.message); // "Task cancelled"
  ```

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

  task_id = 'c37c1d78-4d23-4e53-949a-ddd775f25e01'

  response = requests.delete(
      f'https://api.sigmic.ai/api/v1/tasks/{task_id}',
      headers={'Authorization': 'Bearer sigmic_your_key_here'}
  )

  result = response.json()
  print(result['data']['message'])  # "Task cancelled"
  ```
</CodeGroup>

## Use Cases

### Timeout Handling

Cancel tasks that take too long:

```javascript theme={null}
const API_KEY = 'sigmic_your_key_here';
const BASE_URL = 'https://api.sigmic.ai';

async function createTaskWithTimeout(message, timeoutMs = 60000) {
  // 1. Create the task (returns JSON immediately)
  const formData = new FormData();
  formData.append('message', message);

  const createResponse = await fetch(`${BASE_URL}/api/v1/tasks`, {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${API_KEY}` },
    body: formData
  });

  const { data: { id: taskId } } = await createResponse.json();

  // 2. Set up timeout to cancel
  const timeoutId = setTimeout(async () => {
    await fetch(`${BASE_URL}/api/v1/tasks/${taskId}`, {
      method: 'DELETE',
      headers: { 'Authorization': `Bearer ${API_KEY}` }
    });
    console.log('Task cancelled due to timeout');
  }, timeoutMs);

  // 3. Stream the execution
  const streamResponse = await fetch(`${BASE_URL}/api/v1/tasks/${taskId}/stream`, {
    headers: { 'Authorization': `Bearer ${API_KEY}` }
  });

  const reader = streamResponse.body.getReader();
  const decoder = new TextDecoder();
  let buffer = '';

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;

    buffer += decoder.decode(value, { stream: true });
    const events = buffer.split('\n\n');
    buffer = events.pop() || '';

    for (const event of events) {
      if (!event.trim() || event.startsWith(':')) continue;
      const lines = event.split('\n');
      const eventType = lines[0]?.replace('event: ', '');
      const dataLine = lines[1]?.replace('data: ', '');
      if (!dataLine) continue;

      const data = JSON.parse(dataLine);

      if (eventType === 'content') {
        process.stdout.write(data.text);
      }
      if (eventType === 'done') {
        console.log('\nTask completed');
      }
    }
  }

  clearTimeout(timeoutId);
}
```

### User-Initiated Cancellation

```javascript theme={null}
const API_KEY = 'sigmic_your_key_here';
const BASE_URL = 'https://api.sigmic.ai';

let currentTaskId = null;

// Start a task (fire-and-forget)
async function startTask(message) {
  const formData = new FormData();
  formData.append('message', message);

  const response = await fetch(`${BASE_URL}/api/v1/tasks`, {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${API_KEY}` },
    body: formData
  });

  const { data: { id: taskId } } = await response.json();
  currentTaskId = taskId;

  // Connect to stream in background
  // ... processSSEStream(taskId, handlers)
}

// Cancel button handler
async function onCancelClick() {
  if (currentTaskId) {
    await fetch(`${BASE_URL}/api/v1/tasks/${currentTaskId}`, {
      method: 'DELETE',
      headers: { 'Authorization': `Bearer ${API_KEY}` }
    });
    currentTaskId = null;
    console.log('Task cancelled by user');
  }
}
```

## Task Status After Cancellation

After cancellation, the task's status will be `cancelled`:

```json theme={null}
{
  "success": true,
  "data": {
    "id": "c37c1d78-4d23-4e53-949a-ddd775f25e01",
    "status": "cancelled",
    "error": null,
    "completedAt": "2026-01-26T16:00:00.000Z"
  }
}
```

## Important Notes

<Note>
  Cancelling a task terminates its session. You cannot send follow-up messages to a cancelled task.
</Note>

<Warning>
  If a tool is currently executing when you cancel, it may complete before the cancellation takes effect. The task will still be marked as cancelled.
</Warning>

## Errors

| Code              | Description                              |
| ----------------- | ---------------------------------------- |
| `AUTH_REQUIRED`   | No authentication provided               |
| `INVALID_API_KEY` | Invalid or expired API key               |
| `NOT_FOUND`       | Task with the specified ID was not found |
