# POST /cancel-enrich-job: cancel enrichment jobs

> Cancel one or up to 500 pending or in-flight LocalPipe enrichment jobs. Records already enriched are kept.
>
> Part of the LocalPipe API reference: https://www.localpipe.io/docs/cancel-enrich-job (full reference in one file: https://www.localpipe.io/localpipe-api-docs.md, categories CSV: https://www.localpipe.io/localpipe-business-categories.csv)

## POST /cancel-enrich-job

Cancel one or more in-flight enrichment jobs, including jobs still `pending` (queued) that haven't started processing yet. Any records already enriched **remain preserved** and stay retrievable via `GET /get-job-results`. Only jobs still `pending` or `processing` are cancellable — jobs already `completed`, `failed`, or `cancelled` are returned in `already_terminal` untouched.

### Request

```bash
# Cancel one job
curl -X POST "https://uunrlffonnvmbsstkpze.supabase.co/functions/v1/cancel-enrich-job" \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{"job_id": "uuid-here"}'

# Cancel many at once (up to 500 per call)
curl -X POST "https://uunrlffonnvmbsstkpze.supabase.co/functions/v1/cancel-enrich-job" \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{"job_ids": ["uuid-a", "uuid-b", "uuid-c"]}'
```

### Request Fields

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| job_id | string | Either this or `job_ids` | Single `job_id` returned by `/enrich`. |
| job_ids | string[] | Either this or `job_id` | Up to 500 `job_id`s to cancel in one call. |

### Response (200 OK)

```json
{
  "success": true,
  "cancelled": ["uuid-a", "uuid-b"],
  "already_terminal": [{ "job_id": "uuid-c", "status": "completed" }],
  "not_found": [],
  "failed": [],
  "message": "Cancelled 2 enrichment jobs. Records already enriched are preserved and remain retrievable via GET /get-job-results."
}
```

**Notes:**
- Queued jobs (`pending`, no records leased yet) cancel instantly with zero records processed.
- In-flight records already leased by workers finish out and are counted in the final stats; the job stops accepting new work immediately.
- Cancellation is safe to call twice on the same `job_id` — the second call returns it in `already_terminal` with `status: "cancelled"`.
- API keys can only cancel jobs owned by their user; unknown or foreign IDs come back in `not_found`.
