
# Forms

EGiB data requests and the water-permit application are DOCX forms. Use [authentication and write headers](actions.md#write-contract).

## EGiB requests

### Fill the applicant profile first

POST [`projects/profile/get`](https://tarvena.pl/api/v1/actions/projects/profile/get):

```json
{"projectId":"<projectId>"}
```

Returns `{"profile":{"revision":"<returned-revision>","investors":[]}}`.

Investor `address` accepts `street`, `buildingNo`, `postalCode`, `city` and `raw`. A nonempty `raw` replaces the parts. There is no apartment field; `buildingNo` can contain `12/3`. `street` contains the street name without "ul.". Read the existing profile and preserve fields outside the intended change.

POST [`projects/profile/mutate`](https://tarvena.pl/api/v1/actions/projects/profile/mutate):

```json
{"projectId":"<projectId>","requestId":"<fresh-uuid-3>","write":{"sessionId":"<fresh-uuid-4>","lane":"project-presentation","sequence":1},"expectedRevision":"<returned-revision>","operation":{"kind":"upsert_investor","investor":{"id":"<fresh-uuid-1>","kind":"organization","displayName":"<actual applicant name>","address":{"street":"<street>","buildingNo":"<number>","postalCode":"<postal code>","city":"<city>"},"email":"<actual email>","phone":"<actual phone>"}}}
```

Returns `{"profile":{"revision":"<returned-revision>"}}`.

Every profile write carries a fresh random UUID (v4) `requestId` and a `write` envelope: one random `sessionId` for your session, lane `project-presentation`, and a `sequence` that rises by one with each write of that session. After a lost or timed-out answer, resend the identical body; the server answers it once and replays that answer.

New investor IDs require random v4 UUIDs; retain stored IDs for edits, reordering and deletion. For a person, use their display name and optional `firstName`/`lastName`.

<!-- enum projects/profile/mutate investor.kind -->
Investor `kind`: `person`, `organization`.

Optional investor `nip` is 10 digits with a valid checksum; `krs` is 10 digits. Send both through `projects/profile/mutate`.

EGiB form overrides use `representative.name`, `representative.email` and `representative.phone`. Profile `professionals` entries have `firstName`, `lastName`, `licenseNo`, `specialization` and the role below.

<!-- enum projects/profile/mutate assignment.role -->
Professional `role`: `Projektant`, `Asystent projektanta`, `Sprawdzający`.

Generate a fresh random UUID (v4) for every new assignment ID, including new entries in `replace_professionals` and `newPrimaryDesignerAssignmentId` when defaults create a designer. Existing stored IDs remain valid for edits, replacement, reordering and removal. Identity collisions return a generic conflict; generate a fresh UUID and retry with the current revision. The first `Projektant` supplies designer fields. `add_professional` requires `specialization` (for example, `hydrotechniczna`). To add one, POST `projects/profile/mutate` with `{"projectId":"<projectId>","requestId":"<fresh-uuid>","write":{"sessionId":"<session-uuid>","lane":"project-presentation","sequence":2},"expectedRevision":"<returned-revision>","operation":{"kind":"add_professional","assignment":{"id":"<fresh-uuid-2>","role":"Projektant","firstName":"<first name>","lastName":"<last name>","licenseNo":"<licence number>","specialization":"hydrotechniczna"}}}`, using the current revision. To retain office details use `operation:{kind:"set_design_office",designOffice:{displayName,organizationName,address,email,phone}}`; names are strings, contact/address fields are optional and the address uses the investor’s shape.

### Preview and create the forms

POST [`location/egib-recipient-plan/build`](https://tarvena.pl/api/v1/actions/location/egib-recipient-plan/build):

```json
{"projectId":"<projectId>"}
```

Returns `{"plan":{"groups":[{"kodTeryt":"<recipientCode>","institutionName":"<authority>","parcelExternalIds":["060901_5.0010.278"]}],"unmatchedParcels":[]}}`.

POST [`forms/preview-batch-draft`](https://tarvena.pl/api/v1/actions/forms/preview-batch-draft):

```json
{"projectId":"<projectId>","templateKey":"egib.wniosek-jst"}
```

Returns `{"sourceFingerprint":"<sourceFingerprint>","drafts":[{"draftId":"<draftId>","values":{"recipient.name":"<authority>"},"warnings":[]}],"warnings":[]}`.

Review values and warnings, including applicant details. Copy `sourceFingerprint` into creation. Preview `draftId` identifies a draft, not a saved form.

POST [`forms/create-batch`](https://tarvena.pl/api/v1/actions/forms/create-batch):

```json
{"projectId":"<projectId>","templateKey":"egib.wniosek-jst","createRequestId":"egib-demo-1","sourceFingerprint":"<sourceFingerprint>"}
```

Returns `{"forms":[{"id":"<formId>","configRevision":"<returned-revision>","batch":{"id":"egib-demo-1"}}],"warnings":[]}`.

Per-draft overrides use `drafts:[{"draftId":"<draftId>","fieldValues":{"legalBasis.justification":"<purpose>"}}]`, covering every preview draft exactly once.

If parcels change after creation, the old batch keeps its recipients. POST `forms/delete` with `{"projectId":"<projectId>","formId":"<formId>","deleteRequestId":"<uuid>"}` for each of its forms (`forms/list` lists them), then preview and create again with a new `createRequestId`. If only applicant or profile values changed, POST `forms/refresh-computed-values` per form instead.

### Download any saved forms

POST [`forms/download`](https://tarvena.pl/api/v1/actions/forms/download):

```json
{"projectId":"<projectId>","forms":[{"formId":"<formId>","expectedRevision":"<returned-revision>"}]}
```

Returns `{"filename":"<filename.docx>","contentType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document","bytesBase64":"<base64>"}`.

To verify saved values, POST `forms/get` with `{"projectId":"<projectId>","formId":"<formId>","include":["values"]}`. Read `form.fieldValues` and `form.computedValuesSnapshot`; overrides win over computed values.

Use every returned saved `id` and its own **configRevision** as `expectedRevision`, never `updatedAt`. Decode `bytesBase64` into `filename`. Several forms produce a ZIP. Split downloads exceeding 8 MiB decoded.

### Save into project files

POST [`forms/publication/readiness`](https://tarvena.pl/api/v1/actions/forms/publication/readiness):

```json
{"projectId":"<projectId>"}
```

Returns `{"status":"ready","context":{"bindingRevision":"<returned-revision>"}}`.

For `needs_location` with Drive connected, use the existing folder-creation flow. POST `storage/connection/get` with `{}` → `connection.connectionId`; `storage/roots/get` with `{"organizationId":"<organizationId>"}` → `default.rootId` and `default.parentFolderId` (or select a registered `roots[].id` and its `folderId`). Use the current `project.revision` from `projects/get` with `{"projectId":"<projectId>"}`.

POST `storage/projects/creation/reserve` with `{"organizationId":"<organizationId>","projectId":"<projectId>","input":{"operationId":"<uuid>","expectedRevision":"<returned-revision>","connectionId":"<connectionId>","rootId":"<rootId>","parentFolderId":"<parentFolderId>","name":"<project folder name>"}}`, substituting the actual revision. On `reserved`, POST `storage/projects/creation/execute` with `{"organizationId":"<organizationId>","projectId":"<projectId>","input":{"operationId":"<same uuid>","expectedRevision":"<returned-revision>","connectionId":"<connectionId>"}}`, retaining the same revision. On `completed`, retry readiness. `needs_connection` or `grant_required` needs the browser consent flow; folder creation itself needs no picker. After uncertainty use `storage/projects/creation/status`, retaining the original operation ID.

Folder names are unique per parent, ignoring case: `400` `validationCode:"STORAGE_FOLDER_NAME_TAKEN"` (issue at `input.name`) means another project's folder there already has this name, so reserve again with another `name` and a fresh `operationId`. `validationCode:"STORAGE_PATH_FOLDER_NAME_TAKEN"` (issue at `input.parentFolderId`) means a folder above the new one clashes, ignoring case, with another project's folder beside it; the issue message names it, so choose another `parentFolderId`. `defaultAuthorizationId` is optional; send `default.automaticCreationAuthorization` from `storage/roots/get` only to require that the default has not changed.

POST [`forms/publication/save`](https://tarvena.pl/api/v1/actions/forms/publication/save):

```json
{"projectId":"<projectId>","formId":"<formId>","expectedRevision":"<returned-revision>","bindingRevision":"<returned-revision>","operationId":"<fresh-uuid-3>"}
```

Returns `{"status":"completed","operationId":"<fresh-uuid-3>"}`.

Use the form’s current `configRevision` and readiness’s `context.bindingRevision`; one operation UUID per form. POST `files/list` with `{"projectId":"<projectId>","folder":null}` to inspect output; follow `page.nextCursor` until complete.

Lost publication → `forms/publication/status` with `{"projectId":"<projectId>"}`. For `pending`/`recoverable`, resend its exact `input` to `save`; if cancellation is intended and `canCancel`, send that input to `forms/publication/cancel`. For `busy`, check later. Settle the old operation before creating another.

## Operat application

Creation `fieldValues` accepts `representative.enabled` (boolean) and string fields `representative.firstName`, `representative.lastName`, `representative.city`, `representative.street`, `representative.houseNumber`, `representative.apartmentNumber`, `representative.postalCode`.

`attachments.attach_1`–`attachments.attach_10` are booleans; `attachments.attach_11` is a string. Use the linked workflow to choose values.

### Preview and create

POST [`forms/preview-draft`](https://tarvena.pl/api/v1/actions/forms/preview-draft):

```json
{"projectId":"<projectId>","templateKey":"operat.wniosek","config":{"linkedOperatDocumentId":"operat-demo-1"}}
```

Returns `{"values":{"applicant.name":"<actual applicant name>","authority.name":"<Zarząd Zlewni>","authority.addressLine1":"<address>","request.subject":"<section 14 request scope>"},"warnings":[]}`.

POST [`forms/create`](https://tarvena.pl/api/v1/actions/forms/create):

```json
{"projectId":"<projectId>","templateKey":"operat.wniosek","title":"Wniosek o pozwolenie wodnoprawne","createRequestId":"operat-application-demo-1","config":{"linkedOperatDocumentId":"operat-demo-1"},"fieldValues":{}}
```

Returns `{"form":{"id":"operat-application-demo-1","configRevision":"<returned-revision>"}}`.

`createRequestId` is unique per account across all projects. Resending it with different input, or reusing it in another project, is refused with `validationCode:"DOCX_FORM_CREATE_IDENTITY_CONFLICT"` and an issue at `createRequestId`; use a new id for a new form.

After changing the Operat, applicant or evidence, POST `forms/refresh-computed-values` with `{"projectId":"<projectId>","formId":"operat-application-demo-1"}`; inspect the returned form and use its new `configRevision` for download. Omit `fieldValues`: saved overrides (representative, attachments) then remain. Sending `fieldValues` with `expectedFormRevision` replaces every saved override, so `fieldValues:{}` clears them all. To recompute an overridden field, include its key in `resetFieldKeys:["request.subject"]` (replace with the intended key).
