> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bbrands.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Signing

> Signing seen from digital contracting: how the documents are prepared, how each customer signer requests a code and signs, the order of company signers, the operations review and Maihue countersignature.

This page covers signing from the contract's side: when it starts, what the
customer does and what happens to the contract afterwards. How the engine
captures evidence and seals the PDFs belongs to the signature engine; see
[MaihueSigner — Signing flow](https://docs.bbrands.io/signature-engine/signing-flow.md) and
[Evidence and sealing](https://docs.bbrands.io/signature-engine/evidence-and-sealing.md).

## Overview

```mermaid theme={null}
sequenceDiagram
    actor C as Customer signer
    participant P as Portal wizard
    participant B as Contract backend
    participant W as Contract workers
    participant E as Signature engine
    actor O as Maihue operations

    B->>W: Payment confirmed → Worker A
    W->>W: Render contract and annex PDFs
    W->>E: Worker B opens the signature group
    W-->>P: Documents ready (realtime)
    C->>P: Read the documents
    P->>B: POST /signature/{id}/signer/{signer}/otp
    B->>E: Issue the code
    E-->>C: 6-digit code by email
    C->>P: Enter the code
    P->>B: POST /signature/{id}/signer/{signer}/sign
    B->>E: Register the gesture
    C->>P: Create password
    P-->>C: Customer portal, already signed in
    Note over B,O: Without the customer
    E-->>B: Last customer signed → pending_review
    O->>B: PATCH /contract/{id}/review (approve)
    B->>W: Enterprise worker
    W->>E: Maihue countersigns and seals
    W->>W: Worker C activates the contract
    W-->>C: Confirmation email
```

## Preparing the documents

Confirming the payment method starts the pipeline:

| Step | Who | Result |
| - | - | - |
| Render | Worker A (`contract-pdf-generation`) | The contract and annex PDFs, stored as originals in the private `contracts` bucket |
| Open the group | Worker B (`contract-send-to-signer`) | A signature group with every customer signer, in signing order, and Maihue's representatives |

The portal shows the document cards as soon as they are ready. Worker B does
not email the customer: the customer is already in the wizard. The **View**
button opens a preview that the portal serves itself; horizon-api also exposes
`GET /api/v3/contract/{id}/preview.pdf?kind=contract|annex`, which returns the
best PDF it has: sealed, else original, else a preliminary one generated on
the fly.
`POST /api/v3/contract/{id}/signature/send` lets the signature step start the
pipeline itself if it is not running yet. When the contract is already in the
signing pipeline it starts nothing and answers `{ stage }` or, once Worker B
opened the group, `documents`, `signing` and `stage`, republished as
`contract.signature_requested`; from `pending_review` or
`awaiting_enterprise_signature` it answers `409`.

## Requesting a code

`POST /api/v3/signature/{id}/signer/{signer}/otp` sends a 6-digit one-time
code to the signer's email and answers the masked address and the expiry
time. Codes expire, and both resends and wrong attempts are limited.

## The signing gesture

`POST /api/v3/signature/{id}/signer/{signer}/sign` with `action: "sign"` and
the `otp_code` signs **every document of the group at once**. The portal
sends only the code; when `document_number` is omitted, the backend uses the
signer's RUT verified within the contract window (for the primary signer with
no identity record at all, the RUT on their person account or, else, the RUT
of the contract's account) and records it, hashed, in the
evidence.

The API also accepts `action: "reject"` with a `rejection_reason`. The
portal does not send it; see
[Flows — Exceptions](https://docs.bbrands.io/digital-contracting/flows.md#exceptions).

Before the code is sent or the gesture is registered, the backend checks:

| Check | Answer when it fails |
| - | - |
| The signer seat belongs to the session user | `403` |
| A re-entry session has proved the card serial | `422 reentry-serial-required` |
| The previous company administrator has signed | `409 prior-signer-pending` |

## After each signature

| Situation | Contract | Next |
| - | - | - |
| Other customer signers are still pending | `partially_signed` | The next administrator receives their one-time link |
| The last customer signer signed (with `last`) | `pending_review` | Waits for Maihue operations |
| The group is complete and sealed (with `first`; with `last` the enterprise worker does it after the approval) | `signed` | Worker C (`contract-activated`) activates the contract |
| A signer rejected | `rejected` | The flow ends |

The signing milestones (signature requested, status changes, contract signed)
are also published on `private-contract-{id}`, so every open tab follows the
signature live. See
[Realtime — Channels & Events](https://docs.bbrands.io/realtime/channels-and-events.md).

## Company signers

Customer seats sign in their signing order. A person contract has a single
seat, so the order never applies.

* Administrator N can request a code or sign only after administrator N−1
  signed.
* Each administrator signs the **same PDFs**, after their own identity and
  summary steps.
* When an administrator signs and others are still pending, the next one
  receives their one-time link right away.

## Operations review

The customer does not wait for this step: right after signing, each signer
creates their portal password and leaves for the customer portal (see
[Session and access](https://docs.bbrands.io/digital-contracting/session-and-access.md)).

When every customer signer has signed, the contract waits in `pending_review`
until Maihue operations decide with `PATCH /api/v3/contract/{id}/review`
(permission `contract:review`):

* **Approve** moves the contract to `awaiting_enterprise_signature` and queues
  the enterprise worker (`contract-enterprise-sign`), which applies the
  countersignature of Maihue's representatives and seals the PDFs.
* **Reject** requires a reason and cancels the signature group and the
  contract. It is also allowed from `awaiting_enterprise_signature`.

Worker C then records the sealed PDFs (already uploaded when sealing) as
version 2, activates the contract and the subscription, and queues the
confirmation email.

> [!NOTE]
>
> Each workflow says whether Maihue signs after the customers (`last`, used by
> the seeded workflows) or before them (`first`). With `first`, the
> countersignature runs when the group opens and there is no review step.

## Evidence export

`GET /api/v3/contract/{id}/evidence` (permission `contract:evidence`) exports
the evidence package of a contract — the audit chain and the evidence of
each signer — for compliance.

## Code

| Concern | Path |
| - | - |
| Code and sign routes | `app/api/v3/signature/[document_id]/signer/[signer_id]/` |
| Signing service and order lock | `lib/endpoint/signature-signing/` |
| Pipeline start | `lib/endpoint/contract-signature/`, `lib/endpoint/contract-signature-send/` |
| Review | `app/api/v3/contract/[document_id]/review/`, `lib/endpoint/contract-review/` |
| Workers | `lib/queue/contract/handlers/` |
| Document preview | `lib/endpoint/contract-preview/` |
| Evidence export | `lib/endpoint/contract-evidence/` |
| Engine client | `lib/integration/signature-provider/internal-signer/` |
| Portal step | `frontend/portal-customer/app/components/digital-contract/steps/signature-step.tsx`, `signature-signing-modal.tsx` |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.