> ## 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.

# Consumer integration

> How a product uses MaihueSigner: preparing the group and its seats, calling the provider interface, routing signer gestures, driving the company phase and reading the sealed documents — with digital contracting as the worked example.

A product that needs documents signed is a **consumer** of the engine. The
consumer owns its business record and decides when to sign; the engine owns
the signature. This page lists what a consumer does, using digital
contracting as the example.

## What a consumer does

```mermaid theme={null}
sequenceDiagram
    participant C as Consumer
    participant E as MaihueSigner
    actor S as Signer
    participant J as Company-side job

    C->>E: resolveSignatureWorkflow
    C->>C: Create signature, documents and seats (with snapshot)
    C->>E: createSignatureRequest
    S->>C: Code request and gesture (signer routes)
    C->>E: sendSignerOtp / registerSignerAction
    E-->>C: Seat signed, group status
    C->>J: Start the company phase when due
    J->>E: applyUnattendedEnterpriseSignatures
    E-->>J: sealed
    C->>E: getSignedDocument
```

**1. Resolve the workflow**

Call `resolveSignatureWorkflow` with the scope (today the business unit)
and the workflow assigned to the record, if any. It returns the company
seats and the snapshot to store. See [Workflows](https://docs.bbrands.io/signature-engine/workflows.md).

**2. Create the group**

Write the `signature` row with the snapshot, one `signature_document` per
PDF (each pointing to a contract or an annex), and one `signature_signer`
per seat: customer seats with `side = customer` and `signing_mode =
        attended`, company seats from the snapshot with `side = enterprise`. Store
each original PDF and its SHA-256.

**3. Open the request**

Call `createSignatureRequest` with the documents (name, kind, storage path,
SHA-256) and the signers (name, email, order). The engine records
`request_created` and the provider request.

**4. Route the gestures**

Expose the signer's code request and gesture through the signer routes,
which check that the session user owns the seat, then call
`sendSignerOtp` and `registerSignerAction`. Pass the request IP and user
agent so they reach the evidence.

**5. Drive the company phase**

When the engine reports `awaitingEnterpriseSignature`, or right after
opening the group with `company_phase = first`, start a job that calls
`applyUnattendedEnterpriseSignatures`. The call is idempotent per seat and
seals the group when both sides are complete.

**6. Read the result**

When the engine reports `sealed: true` (the group is `signed`), read each
sealed PDF with `getSignedDocument` and update the business record. To
stop a group, call `cancelSignatureRequest` and set the group
`cancelled`.

## Digital contracting as the example

| Step | Where contracting does it |
| - | - |
| Resolve the workflow, create the group, open the request | Worker B (`contract-send-to-signer`) |
| Route the gestures | `POST /api/v3/signature/{id}/signer/{signer}/otp` and `…/sign`, through the signing service |
| Drive the company phase | After operations approve the contract, the enterprise worker (`contract-enterprise-sign`); with `first`, Worker B enqueues it directly |
| Read the result | Worker C (`contract-activated`) stores the sealed PDFs as version 2 |
| Cancel | The cancel, review-reject and rewind paths, and Worker B's compensation of a stale send, call `cancelSignatureRequest` |

The full contracting journey is in
[Digital contracting — Signing](https://docs.bbrands.io/digital-contracting/signing.md).

## Rules for a new consumer

* **Call the engine, not the tables.** Use the engine client
  (`internalSignerClient`, which implements `SignatureProviderClient` and adds
  `applyUnattendedEnterpriseSignatures`) and `resolveSignatureWorkflow`;
  never write codes or evidence directly.
* **Keep the workflow per scope.** Add workflows for the new scope instead of
  changing another consumer's workflows.
* **Check seat ownership.** The engine signs the seat it is given; the
  consumer's route must make sure the session user owns it.
* **Extend the document reference.** Today each `signature_document` must
  point to a contract or an annex. A new kind of document needs that
  reference extended first.

A synthetic test harness runs the workflow helpers end to end for a fake
consumer (`internal-signer/__tests__/signature-engine-e4.harness.test.ts`),
which shows the engine can be reused outside contracting.

## Code

| Concern | Path |
| - | - |
| Provider interface | `lib/integration/signature-provider/signature-provider-client.ts` |
| Engine client | `lib/integration/signature-provider/internal-signer/internal-signer.client.ts` |
| Workflow resolution | `lib/integration/signature-provider/internal-signer/signature-workflow.resolve.ts` |
| Contracting: group creation | `lib/queue/contract/handlers/contract-send-to-signer.handler.ts` |
| Contracting: company phase | `lib/queue/contract/handlers/contract-enterprise-sign.handler.ts` |
| Contracting: sealed PDFs | `lib/queue/contract/handlers/contract-activated.handler.ts` |

Paths are relative to `backend/horizon-api/`.


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