# Finalize a document

Source: https://amanucci.com/fr/docs/developers/api/documents/finalizeDocument

## POST /v1/entities/{entityId}/documents/{documentId}/finalize

Finalize a document

THE act. One transaction, in this order: validate the composition → mint `doc_no` gap-free from `document_counter` → render UBL + PDF → validate the UBL IN-PROCESS against EN 16931 and Peppol BIS → write both into `evidence/` through the artifact port → for an invoice or credit note, mint the `sale` event and hand the derived proposal to the SAME ledger door as everything else → flip `finalized 0→1`. A document that fails its own schematron never finalizes, and the counter mint rolls back with it. A quote takes its number and its PDF, no UBL: `entryId` is null and `files` holds its PDF alone in the 200, which is an answer rather than an absence. Finalize fires AT MOST ONCE per row — a retry returns the truth re-selected from the row, `alreadyFinalized: true`, with no second number minted.

Operation ID: `finalizeDocument`

### Parameters

```json
[
  {
    "schema": {
      "type": "string",
      "format": "uuid"
    },
    "required": true,
    "name": "documentId",
    "in": "path"
  },
  {
    "schema": {
      "type": "string",
      "format": "uuid"
    },
    "required": true,
    "name": "entityId",
    "in": "path"
  }
]
```

### Responses

#### 200 Finalized — the number, the artifacts, and the entry if one was booked.

```json
{
  "application/json": {
    "schema": {
      "$ref": "#/components/schemas/FinalizeResult"
    }
  }
}
```

#### 401 Dead or absent credential (`unauthenticated`).

```json
{
  "application/problem+json": {
    "schema": {
      "$ref": "#/components/schemas/Problem"
    }
  }
}
```

#### 403 Authenticated, present, and refused: a missing grant, a missing seat, or a missing step-up.

```json
{
  "application/problem+json": {
    "schema": {
      "$ref": "#/components/schemas/Problem"
    }
  }
}
```

#### 404 Absent, or hidden from this asker — the two are indistinguishable (`not-found`).

```json
{
  "application/problem+json": {
    "schema": {
      "$ref": "#/components/schemas/Problem"
    }
  }
}
```

#### 409 The world is not in the state the request assumed: a stale `expectedSeq`, or a transition already made.

```json
{
  "application/problem+json": {
    "schema": {
      "$ref": "#/components/schemas/Problem"
    }
  }
}
```

#### 422 The request did not satisfy the schema (`invalid-input`).

```json
{
  "application/problem+json": {
    "schema": {
      "$ref": "#/components/schemas/Problem"
    }
  }
}
```

#### 429 Too many requests on this door's bucket (`rate-limited`); refused before any handler ran, `Retry-After` says when to come back.

```json
{
  "application/problem+json": {
    "schema": {
      "$ref": "#/components/schemas/Problem"
    }
  }
}
```

#### 503 Temporarily unavailable; `Retry-After` says when to come back.

```json
{
  "application/problem+json": {
    "schema": {
      "$ref": "#/components/schemas/Problem"
    }
  }
}
```
