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

# Approve Lexi Task

> Approve a pending task for Lexi to execute

## Request

### Path Parameters

<ParamField path="id" type="string" required>
  Task UUID
</ParamField>

### Headers

```
Authorization: Bearer wbk_your_api_key_here
Content-Type: application/json
```

## Response

<ResponseField name="data" type="object">
  <Expandable title="properties">
    <ResponseField name="task_id" type="string">
      Task UUID
    </ResponseField>

    <ResponseField name="status" type="string">
      New task status (`approved`)
    </ResponseField>

    <ResponseField name="execution_started" type="boolean">
      Whether execution has begun
    </ResponseField>
  </Expandable>
</ResponseField>

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST \
    https://data.leadlex.com/functions/v1/api-gateway/v1/lexi/tasks/task-uuid/approve \
    -H "Authorization: Bearer wbk_your_api_key_here" \
    -H "Content-Type: application/json"
  ```

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

  API_KEY = "wbk_your_api_key_here"
  BASE_URL = "https://data.leadlex.com/functions/v1/api-gateway"
  TASK_ID = "task-uuid"

  headers = {
      "Authorization": f"Bearer {API_KEY}",
      "Content-Type": "application/json"
  }

  response = requests.post(
      f"{BASE_URL}/v1/lexi/tasks/{TASK_ID}/approve",
      headers=headers
  )

  result = response.json()["data"]
  print(f"Task approved: {result['status']}")
  ```

  ```javascript JavaScript theme={null}
  const TASK_ID = 'task-uuid';

  const response = await fetch(
    `https://data.leadlex.com/functions/v1/api-gateway/v1/lexi/tasks/${TASK_ID}/approve`,
    {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer wbk_your_api_key_here',
        'Content-Type': 'application/json'
      }
    }
  );

  const { data } = await response.json();
  console.log(`Task approved: ${data.status}`);
  ```
</CodeGroup>

### Example Response

```json theme={null}
{
  "data": {
    "task_id": "task-uuid",
    "status": "approved",
    "execution_started": true
  }
}
```

## Complete Workflow Example

### Step 1: Chat with Lexi

```python theme={null}
# Ask Lexi to search for prospects
chat_response = requests.post(
    f"{BASE_URL}/v1/lexi/chat",
    headers=headers,
    json={
        "message": "Find 50 CEOs at law firms in California"
    }
)

result = chat_response.json()["data"]
if result["requires_approval"]:
    task_id = result["task_id"]
    print(f"Task created: {task_id}")
```

### Step 2: Review the Task

```python theme={null}
# Get task details
tasks_response = requests.get(
    f"{BASE_URL}/v1/lexi/tasks",
    headers=headers,
    params={"status": "pending"}
)

tasks = tasks_response.json()["data"]["tasks"]
for task in tasks:
    if task["id"] == task_id:
        print(f"Task: {task['description']}")
        print(f"Query: {task['metadata']['query']}")
```

### Step 3: Approve the Task

```python theme={null}
# Approve the task
approve_response = requests.post(
    f"{BASE_URL}/v1/lexi/tasks/{task_id}/approve",
    headers=headers
)

print("Task approved! Lexi is executing it now.")
```

### Step 4: Check Completion

```python theme={null}
import time

# Poll for completion
while True:
    status_response = requests.get(
        f"{BASE_URL}/v1/lexi/tasks",
        headers=headers,
        params={"status": "completed"}
    )
    
    completed = status_response.json()["data"]["tasks"]
    if any(t["id"] == task_id for t in completed):
        print("Task completed!")
        break
    
    time.sleep(5)
```

## Auto-Approval Pattern

<Tip>
  For trusted automation, you can auto-approve tasks without manual review:
</Tip>

```python theme={null}
def auto_approve_lexi_tasks():
    """Automatically approve all pending Lexi tasks"""
    response = requests.get(
        f"{BASE_URL}/v1/lexi/tasks",
        headers=headers,
        params={"status": "pending"}
    )
    
    tasks = response.json()["data"]["tasks"]
    
    for task in tasks:
        # Optional: add conditions to filter which tasks to auto-approve
        if task["type"] == "prospect_search":
            requests.post(
                f"{BASE_URL}/v1/lexi/tasks/{task['id']}/approve",
                headers=headers
            )
            print(f"Auto-approved: {task['description']}")
```

## Errors

| Status | Code                       | Description                     |
| ------ | -------------------------- | ------------------------------- |
| 400    | `validation_error`         | Task is not in `pending` status |
| 401    | `invalid_key`              | Invalid API key                 |
| 403    | `insufficient_permissions` | Missing `lexi` permission       |
| 404    | `not_found`                | Task not found                  |
| 429    | `rate_limited`             | Rate limit exceeded             |

### Example Error (Already Approved)

```json theme={null}
{
  "error": {
    "code": "validation_error",
    "message": "Task has already been approved"
  }
}
```
