# Integrate into a pipeline

> The production flow — upload, extract, receive a webhook, read the result and handle fields that need review.

This is the recommended shape for a service that processes documents as they arrive.

```text
your service                          Parsefox
POST /v1/documents             ─────▶ document_id
POST /v1/extractions           ─────▶ job_id (status: pending)
  Idempotency-Key: <your id>
                               ◀───── webhook: extraction.completed
GET /v1/extractions/{id}/result ────▶ data, fields, unresolved_fields
store data; send unresolved fields to review
```

## 1. Upload the document

```bash
curl "$PARSEFOX_BASE_URL/v1/documents" \
  -H "Authorization: Bearer $PARSEFOX_API_KEY" \
  -F file=@invoice.pdf
```

```json
{
  "document_id": "doc_a624f2b16bfc",
  "filename": "invoice.pdf",
  "media_type": "application/pdf",
  "sha256": "1e5cfbe9…",
  "status": "prepared"
}
```

The upload is parsed right away; unreadable files fail here with
`malformed_document`, `encrypted_document` or `unsupported_media_type`. A document
can be extracted many times, with different schemas.

## 2. Start the extraction

Pick the schema in exactly one way:

| Field | Use when |
|---|---|
| `schema_id` (+ optional `schema_version`) | You saved a schema in Parsefox. Pin the version in production so results stay stable when someone publishes a new one. |
| `template_id` | A built-in template such as `invoice` fits. `GET /v1/templates` lists them. |
| `schema` (+ optional `instructions`) | You send a JSON Schema inline, for example generated from your own models. |

```bash
curl "$PARSEFOX_BASE_URL/v1/extractions" \
  -H "Authorization: Bearer $PARSEFOX_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: invoice-2026-0042" \
  -d '{
        "document_id": "doc_a624f2b16bfc",
        "schema_id": "sch_8f73a8efa4f34f87",
        "schema_version": 2,
        "metadata": { "order_id": "A-1042" }
      }'
```

The response is `202 Accepted` with the job:

```json
{
  "object": "extraction",
  "job_id": "job_ff48e2ee7a2e",
  "document_id": "doc_a624f2b16bfc",
  "mode": "live",
  "status": "pending",
  "stage": "queued",
  "review_required": false,
  "schema": { "id": "sch_8f73a8efa4f34f87", "version": 2 },
  "template_id": null,
  "metadata": { "order_id": "A-1042" },
  "charge_usd": 0.0,
  "links": {
    "self": "/v1/extractions/job_ff48e2ee7a2e",
    "result": "/v1/extractions/job_ff48e2ee7a2e/result"
  }
}
```

- `metadata` holds up to 20 string pairs of your own, such as an order or customer
  id. It is echoed in the job, the runs list and every webhook, so you can match
  results without a lookup table.
- `Idempotency-Key` makes retries safe: the same key returns the same job. See
  [Idempotency](/docs/idempotency.md).
- Extractions take from a few seconds to a few minutes, depending on document size.

## 3. Wait for completion

**Webhooks (recommended).** Register an endpoint once (console **Webhooks**, or
`POST /v1/webhooks`) and Parsefox sends `extraction.completed`, `extraction.failed`
or `extraction.cancelled` with the job in `data.object`. Verify the signature,
respond `2xx` quickly and do the work asynchronously. See [Webhooks](/docs/webhooks.md).

**Waiting.** For scripts and small volumes, pass `wait` (up to 60 seconds) to
`POST /v1/extractions` or `GET /v1/extractions/{job_id}`: the call returns as soon as
the job finishes or the time is up. Repeat the `GET` while the status is `pending` or
`running`.

## 4. Read the result

```bash
curl "$PARSEFOX_BASE_URL/v1/extractions/job_ff48e2ee7a2e/result" \
  -H "Authorization: Bearer $PARSEFOX_API_KEY"
```

Final job statuses:

| Status | Meaning | What to do |
|---|---|---|
| `completed` | All fields were resolved. | Use `data`. |
| `completed_with_unresolved` | Finished, but some fields could not be verified or `data` does not pass the schema (`review_required: true`). | Use `data` and handle `unresolved_fields`. |
| `failed` | The job could not finish; `last_error` explains why. | Fix the cause, or retry with a new idempotency key. |
| `cancelled` | Cancelled with `POST /v1/extractions/{job_id}/cancel`. | Nothing. |

Asking for `/result` of a job that is not `completed` or `completed_with_unresolved`
(still running, failed or cancelled) returns `409 job_not_completed`. The result
format is described in [Concepts](/docs/concepts.md#results).

`POST /v1/extractions` always answers `202`, even when `?wait=` let the job finish
before the response; read `status` rather than the HTTP code.

## 5. Handle fields that need review

Parsefox never presents a guess as confirmed. When it cannot confirm a value in the
document, the field's JSON Pointer (such as `/total`) is listed in
`unresolved_fields`, and its entry in `fields` explains why in `unresolved_reason`.
`data` still holds the best candidate for that field (or `null`), so a reviewer can
start from it; do not use it unchecked.

```python
result = api.get(f"/extractions/{job_id}/result").json()
save(result["data"], needs_review=result["unresolved_fields"])
for pointer in result["unresolved_fields"]:
    field = result["fields"].get(pointer, {})
    queue_for_review(job_id, pointer, field.get("value"), field.get("unresolved_reason"))
```

The console's **Runs** page shows every job with the document and highlighted sources,
so reviewers can check values there.

## 6. Go live

1. Build everything with a **test key**. Use the file name or `metadata` to simulate
   review and failure outcomes ([Test mode](/docs/test-mode.md)).
2. Create a live key and a live webhook endpoint. Live webhooks must be public
   `https` URLs.
3. Swap the environment variables. Schemas are shared between modes; documents, jobs
   and webhooks are not.
4. Watch balance and spend in the console under **Usage**. Live extractions fail with
   `402 insufficient_credits` when the balance is used up.

## Large results

`/result` returns everything for typical documents. Very large results (hundreds of
line items) answer `413 result_requires_pagination`; then read `/result/manifest` and
`/result/collections`, or download `/result/export`. See the
[API reference](/docs/api).
