# Documents and Word files

A document is data, not a picture of a page: its blocks have ids, its fields have types, and a Word file keeps its structure one to one.


## Where a document comes from {#sources}

| Source | How |
| --- | --- |
| Word | Upload the `.docx` to `POST /v2/files`, then `POST /v2/documents` with `file`. Numbering, tables, styles, headers and footers stay as Word laid them out. |
| PDF | Upload the PDF and place fields on its pages (`placements`). The PDF is stamped, never re-rendered. |
| Markdown | `POST /v2/documents` with `markdown`. Headings, lists, tables and emphasis become Word paragraphs with real numbering. |
| Template | `POST /v2/documents` with `template` (and `template_version`). |

An agent with a terminal does not carry a file through its own context: `POST /v2/files/upload_links` gives a one-time address (15 minutes), the file goes there with `curl -X PUT --data-binary @file <url>`, and the answer is the file (MCP: `get_upload_link`). A wrong file does not spend the link.

## Photos in the text {#photos}

An inventory, a handover act, meter readings: photos belong in the contract itself. Upload each with purpose `image` (PNG or JPEG, up to 5 MB; an upload link takes `purpose: "image"` too), then place it in Markdown by its file id: `![Living room at handover](file_…)`. A photo is at most as wide as the text and 10 cm tall; a title asks for a share of the text width instead — `![Water meters](file_… "50%")`. It works in `markdown` when creating a document or template and in `replace`, `insert_after`, `insert_before` and `append` edits, and the photo stays in the preview, the DOCX, the PDF and the signed copy. Content as Markdown shows it as `![caption](file_…)`. An id that is not an uploaded picture of the account, or a web address, is refused (`image_not_found`, `image_invalid`) rather than dropped to its caption. Photos go in as taken: a sideways phone picture stays sideways.

## Blocks and revisions {#blocks}

Every paragraph and table has a stable id. Read them with `GET /v2/documents/{id}/content?format=blocks`; the same ids appear as `data-block-id` in the HTML the components show. Each change makes a new revision, and an edit names the revision it was made on, so two editors never overwrite each other silently.

Formats of `GET /v2/documents/{id}/content`:

- `markdown`: the text with field values (or markers),
- `html`: what the components render, with exact numbering,
- `blocks`: ids and plain text, for agents,
- `json`: the full model.

## Editing precisely {#editing}

`PATCH /v2/documents/{id}/content` takes the revision and a list of operations:

| Operation | Effect |
| --- | --- |
| `replace_text` | Replace exact text inside paragraphs, keeping the formatting of the run it starts in. Scope it with `block`. |
| `replace` | Replace a block with Markdown; plain text keeps the paragraph's style and numbering. |
| `insert_after`, `insert_before` | Add Markdown next to a block, taking its neighbour's formatting. |
| `append` | Add Markdown at the end. |
| `delete` | Remove a block. |

```json
{ "revision": 3, "operations": [{ "op": "replace_text", "block": "p4", "find": "12 months", "replace": "24 months" }] }
```

Prefer `replace_text` for small changes: it keeps bold, fonts and colour exactly where they were.

## Templates {#templates}

A template is read and changed like a document. `GET /v2/templates/{id}/content` gives a version's text (Markdown with field markers and conditions, or `format=html` to show it); `GET /v2/templates/{id}/versions` lists every version with who published it and why.

To change a template, open its working draft: `POST /v2/templates/{id}/draft` returns a draft document made from the current version — one per template, shared, edited with every document tool (fields, conditions, co-editing, comments). It never appears among documents and cannot be sent. `POST /v2/templates/{id}/publish` with the draft's id (and a `note`) makes it the next version and removes the draft; until then the template does not change, and documents made earlier keep their version. `DELETE /v2/templates/{id}` takes a template out of the list; documents made from it stay.

## Ready to send {#readiness}

A draft read with `GET /v2/documents/{id}` carries `readiness`: `ready`, and the `issues` that stand between it and sending (a signer missing for a role, fields to fill, a review not finished), the same ones sending would refuse with.

## Exports {#exports}

- `GET /v2/documents/{id}/docx`: the document as Word, fields filled.
- `GET /v2/documents/{id}/pdf`: the signed PDF with its certificate once completed; before that, a preview with the current values.
- `GET /v2/documents/{id}/audit_trail`: every event with its hash.
