# Upload, detect and fill step by step

> Keep the document in AppliFlo, see every field with its id, fill by id and download the filled PDF.

Use this flow when you want to see the fields before filling, fill the same document more than once, or send it for signature afterwards. It takes four calls.

You need a key with the `read` and `draft` scopes.

### Upload the document

Send a PDF or a Word file (DOCX) in the multipart field `file`, up to 20 MB. A DOCX is turned into a PDF.

**curl**

```bash
curl -X POST https://appliflo.com/api/v1/documents \
  -H "Authorization: Bearer $APPLIFLO_API_KEY" \
  -H "Idempotency-Key: upload-lease-7731" \
  -F "file=@lease-application.pdf"
```

**Node**

```js
import { readFile } from "node:fs/promises";

const form = new FormData();
form.append("file", new Blob([await readFile("lease-application.pdf")], { type: "application/pdf" }), "lease-application.pdf");

const res = await fetch("https://appliflo.com/api/v1/documents", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.APPLIFLO_API_KEY}`, "Idempotency-Key": "upload-lease-7731" },
  body: form,
});
const { data: doc } = await res.json();
const documentId = doc.id;
```

**Python**

```python
import os, requests

headers = {"Authorization": f"Bearer {os.environ['APPLIFLO_API_KEY']}"}
res = requests.post(
    "https://appliflo.com/api/v1/documents",
    headers={**headers, "Idempotency-Key": "upload-lease-7731"},
    files={"file": open("lease-application.pdf", "rb")},
)
document_id = res.json()["data"]["id"]
```

The reply is `201` with `data.id`, `data.name` and `data.status` (`draft`).

### Detect the fields

**curl**

```bash
curl -X POST https://appliflo.com/api/v1/documents/$DOCUMENT_ID/detect \
  -H "Authorization: Bearer $APPLIFLO_API_KEY"
```

**Node**

```js
const detect = await fetch(`https://appliflo.com/api/v1/documents/${documentId}/detect`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.APPLIFLO_API_KEY}` },
});
const { data: found } = await detect.json();
console.log(found.fields.length, "fields,", found.flagged_count, "to check");
```

**Python**

```python
found = requests.post(
    f"https://appliflo.com/api/v1/documents/{document_id}/detect",
    headers=headers,
).json()["data"]
print(len(found["fields"]), "fields,", found["flagged_count"], "to check")
```

The reply lists `data.fields` and the choice questions in `data.groups`. Each field has:

| Key | What it tells you |
| --- | --- |
| `id` | The id you fill and correct it by. |
| `label_text` | The label printed on the form. |
| `semantic_type` | What kind of value it takes, such as `date`, `email`, `checkbox` or `signature`. |
| `flagged` | `true` when AppliFlo is unsure about it. Check it before you fill. |
| `page`, `bbox` | Where it is. |

`data.flagged_count` says how many fields need a look. `data.vision_skipped` is `true` when the month's [AI pages](https://dev.appliflo.com/guides/plans) ran out and only built-in fillable fields were found. You can read the fields again later with `GET /api/v1/documents/{id}/fields`.

### Fill by field id

Send `values` keyed by field id. For a choice question, use its group id from `groups[].id` and give one option, or a list for a question that allows several.

**curl**

```bash
curl -X POST https://appliflo.com/api/v1/documents/$DOCUMENT_ID/fill \
  -H "Authorization: Bearer $APPLIFLO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"values":{"fld_full_name":"Lan Nguyen","fld_dob":"1991-04-17","grp_title":"Ms"}}'
```

**Node**

```js
const fill = await fetch(`https://appliflo.com/api/v1/documents/${documentId}/fill`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.APPLIFLO_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    values: {
      fld_full_name: "Lan Nguyen",
      fld_dob: "1991-04-17",
      grp_title: "Ms",
    },
  }),
});
const { data: filled } = await fill.json();
console.log(filled.results, filled.groups);
```

**Python**

```python
filled = requests.post(
    f"https://appliflo.com/api/v1/documents/{document_id}/fill",
    headers=headers,
    json={"values": {
        "fld_full_name": "Lan Nguyen",
        "fld_dob": "1991-04-17",
        "grp_title": "Ms",
    }},
).json()["data"]
print(filled["results"], filled["groups"])
```

Use the real ids from step 2 in place of `fld_full_name` and the others. `data.results` has one entry per field, with the same statuses as [one-shot fill](https://dev.appliflo.com/guides/fill-one-call#check-the-results). `data.groups` has one entry per choice question. If a choice answer is not one of the question's options, the call is refused with `422` and nothing is written.

### Download the filled PDF

Ask for a download link with `artifact=filled`. The link is short-lived, so fetch the file straight away.

**curl**

```bash
curl "https://appliflo.com/api/v1/documents/$DOCUMENT_ID/download?artifact=filled" \
  -H "Authorization: Bearer $APPLIFLO_API_KEY"
```

**Node**

```js
const link = await fetch(`https://appliflo.com/api/v1/documents/${documentId}/download?artifact=filled`, {
  headers: { Authorization: `Bearer ${process.env.APPLIFLO_API_KEY}` },
});
const { data: download } = await link.json();
const pdf = Buffer.from(await (await fetch(download.url)).arrayBuffer());
```

**Python**

```python
url = requests.get(
    f"https://appliflo.com/api/v1/documents/{document_id}/download?artifact=filled",
    headers=headers,
).json()["data"]["url"]
open("lease-filled.pdf", "wb").write(requests.get(url).content)
```

## Next

- Fix a field AppliFlo got wrong: [Correct fields](https://dev.appliflo.com/guides/correct-fields).
- Send the document for signature: [Send for signature](https://dev.appliflo.com/guides/send-for-signature).
- Use it again and again: save it as a template with `POST /api/v1/saved-templates`, then [send the template](https://dev.appliflo.com/guides/send-template).
